Welcome to the Library Management API, a demonstration project that implements Domain-Driven Design (DDD) principles with JWT authentication. This application allows users to interact with a library system, borrow books, and manage their library collection securely.
- Domain-Driven Design (DDD) for clean architecture.
- JWT Authentication for secure access.
- PostgreSQL Database integration for data persistence.
- Swagger for API documentation and testing.
- Memory Caching to enhance performance.
- .NET 8
- Entity Framework Core
- PostgreSQL
- JWT Authentication
- Swashbuckle (Swagger)
This project follows the Clean Architecture approach, with the following layers:
- API: Exposes endpoints for user interaction.
- Application: Contains business logic (e.g.,
JwtTokenService). - Infrastructure: Handles database interaction and repository implementation.
- Domain: Core domain models and rules.
Replace the connection string and JWT key with your own values:
"ConnectionStrings": {
"LibraryDatabase": "Host=localhost;Port=5432;Database=LibraryManagement;Username=postgres;Password=yourpassword"
},
"Jwt": {
"Key": "your-secure-jwt-key"
}Ensure you have PostgreSQL installed and running locally. Create the database schema using EF Core migrations:
dotnet ef database updateStart the API:
dotnet runThe API will be available at:
https://localhost:7082
-
Open Swagger at:
https://localhost:7082/swagger -
Login Endpoint:
- Use
/api/auth/loginto generate a JWT token. - Copy the token.
- Use
-
Authorize:
- Click the Authorize button in Swagger.
- Paste the token in the format:
<your-token>.
-
Access secured endpoints like
/api/books.
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/auth/login |
Generates a JWT token. |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/books |
Retrieves all books. |
| POST | /api/books/{id}/borrow |
Borrows a book by ID. |
- Use strong, secret keys for JWT to ensure security.
- Store tokens securely (e.g., HttpOnly cookies for web apps).
- Always validate the JWT token on the server side.
- Add user roles and claims for role-based authorization.
- Implement refresh tokens for long-lived sessions.
- Add search and filtering options for books.
Feel free to fork the repository, create pull requests, or raise issues for enhancements or bugs. Contributions are always welcome! 💻