This is the backend implementation of a minimal chat application using ASP.NET Core and Entity Framework. The goal of this project is to provide a set of APIs that enable user registration, authentication, conversation management, and message handling.
- ASP.NET Core 7
- EF Core 7 with Code-First approach
- SQL Server Express
- JWT-based Authentication
The project is organized as follows:
-
Controllers: Contains API controllers for user registration, login, user listing, message management, and log retrieval.
-
Models: Defines the data models used in the application, such as User and Message.
-
Services: Contains the business logic for user registration, authentication, message handling, and logging.
-
Data: Includes the database context and migration files for Entity Framework.
-
Middleware: Contains the custom request-logging middleware for capturing API request details.
-
Repositories: Implements the repository pattern for data access.
- API Endpoint: POST /api/register
- Request Parameters: email, name, password
- Responses: 200 OK, 400 Bad Request, 409 Conflict
- Response Body: userId, name, email (in case of success), error (in case of failure)
- Passwords are securely hashed and stored in the database.
- API Endpoint: POST /api/login
- Request Parameters: email, password
- Responses: 200 OK, 400 Bad Request, 401 Unauthorized
- Response Body: token (JWT token), profile (user details), error (in case of failure)
- JWT token is generated upon successful login and used for authentication.
- API Endpoint: GET /api/users
- Responses: 200 OK, 401 Unauthorized
- Response Body: users (array of user objects), error (in case of failure)
- The list of users does not include the user making the request.
- API Endpoint: POST /api/messages
- Request Parameters: receiverId, content
- Request Headers: Authorization (Bearer token)
- Responses: 200 OK, 400 Bad Request, 401 Unauthorized
- Response Body: messageId, senderId, receiverId, content, timestamp (in case of success), error (in case of failure)
- API Endpoint: PUT /api/messages/{messageId}
- Request Parameters: messageId, content
- Request Headers: Authorization (Bearer token)
- Responses: 200 OK, 400 Bad Request, 401 Unauthorized, 404 Not Found
- Response Body: error (in case of failure)
- Users can only edit their own messages.
- API Endpoint: DELETE /api/messages/{messageId}
- Request Parameters: messageId
- Request Headers: Authorization (Bearer token)
- Responses: 200 OK, 401 Unauthorized, 404 Not Found
- Response Body: error (string): Error message indicating the cause of the failure
Note: Users can only delete messages sent by them and not by other users.
- API Endpoint: GET /api/messages
- Request Parameters: userId, before, count, sort
- Request Headers: Authorization (Bearer token)
- Responses: 200 OK, 400 Bad Request, 401 Unauthorized, 404 Not Found
- Response Body: messages (array of message objects), error (in case of failure)
- Custom middleware logs API requests with details like IP, request body, time, and username (if authenticated).
- API to fetch logs:
- Endpoint: GET /api/log
- Request Parameters: EndTime, StartTime
- Request Headers: Authorization (Bearer token)
- Responses: 200 OK, 400 Bad Request, 401 Unauthorized, 404 Not Found
- Response Body: Logs (array of log objects), error (in case of failure)
- Clone this repository to your local machine.
- Set up the database connection string in
appsettings.json
to SQL Server. - Run the Entity Framework migrations to create the database:
dotnet ef database update
. - Build and run the application using
dotnet run
. - Access the API endpoints as described above.
- Fork the repository.
- Create a feature branch (
git checkout -b feature/your-feature-name
). - Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature/your-feature-name
). - Create a new Pull Request.
To review the code, please check the dev
branch. All code should be merged into the dev
branch for testing and review before being merged into the main
branch for production deployment.
Your feedback and contributions are highly appreciated!
This project is licensed under the MIT License.