This project is a Book Reservation System implemented using FastAPI, SQLAlchemy, Alembic, MongoDB and Redis follows principles of Domain-Driven Design (DDD), CQRS (Command Query Responsibility Segregation), and Event-Driven Architecture. The system allows users to register, browse books, make reservations, and manage user data efficiently with an event-driven architecture that process events asynchronously using RabbitMQ.
- User authentication and management
- Book catalog with search functionality
- Reservation system with status tracking
- Role-based access control (Admin, Author, Customer)
- Caching with Redis for improved performance
- Asynchronous database operations using SQLAlchemy
- Database migrations handled by Alembic
- MongoDB integration for book-related metadata
-
FastAPI for API development
-
SQLAlchemy for PostgreSQL database management
-
Redis for caching and OTP management
-
RabbitMQ for event-driven message processing
-
MongoDB for NoSQL storage of book metadata
-
Alembic for database migrations
-
Docker for containerized deployment
-
Pydantic for data validation and serialization
-
Docker Containerization for easy deployment
This system is designed based on CQRS & DDD, with clear separation of Commands, Queries, and Events.
- Commands handle data modifications (e.g., creating a reservation)
- Queries fetch data (e.g., retrieving book details)
- Events handle background tasks (e.g., sending reminders)
- Domain Layer: Business logic & entity definitions
- Application Layer: Service classes managing use cases
- Infrastructure Layer: Database interactions (SQLAlchemy, Redis, RabbitMQ, MongoDB)
- Presentation Layer: FastAPI routes (controllers)
- Messaging Layer: Handles asynchronous communication via RabbitMQ
id: Unique identifierusername: Unique usernameemail: Email addresspassword: Hashed passwordrole: User role (admin, customer, author)is_active: Status flag
id: Unique identifiertitle: Book titleisbn: Unique ISBNprice: Book pricegenre_id: Associated genredescription: Book descriptionunits: Total stockreserved_units: Reserved stockauthors: List of associated authors
id: Unique identifiercustomer_id: Associated customerbook_id: Reserved bookstart_of_reservation: Start dateend_of_reservation: End dateprice: Reservation pricestatus: Reservation status (pending, active, completed)queue_position: Position in queue if waiting
- RabbitMQ is used to handle event-based communication:
book_createdbook_updatedbook_deletedreservation_createdreservation_ends_soonotp_generated
- Book data is cached to reduce DB queries
- OTPs are stored with expiration time for authentication
- User session management for quick authentication validation
- OAuth2 password flow for authentication
- Role-based access control for different operations
- Rate-limiting for OTP generation (5 attempts in 2 minutes, 10 in an hour)
- Dockerized setup with separate services for PostgreSQL, Redis, RabbitMQ, and MongoDB
docker-compose.ymlfor easy container management- Environment variables for configuration management
Ensure you have the following installed:
- Python 3.10+
- PostgreSQL
- Redis
- RabbitMQ
- MongoDB
- Docker (optional, for containerized deployment)
- Clone the repository:
git clone https://github.com/your-repo.git . - Create a virtual environment and activate it:
python3 -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
- Install dependencies:
pip install -r requirements.txt
- Set up the database:
alembic upgrade head
- Run the application:
uvicorn app.main:app --reload
- Build and start the services:
docker-compose up --build
- Access the API at:
http://localhost:8000/docs
| Method | Endpoint | Description |
|---|---|---|
| POST | /auth/signup/ |
Register a new user |
| POST | /auth/login/ |
Authenticate user and get access token |
| POST | /auth/logout/ |
Logout user |
| POST | /books/ |
Create a new book |
| GET | /books/ |
Get list of books |
| GET | /books/{id} |
Get book details |
| PATCH | /books/{id} |
Update a book |
| DELETE | /books/{id} |
Delete a book |
| POST | /customer/ |
Create a new customer |
| GET | /customer/ |
Get list of customers |
| GET | /customer/{id} |
Get customer details |
| PATCH | /customer/{id} |
Update a customer |
| DELETE | /customer/{id} |
Delete a customer |
| POST | /reservations/ |
Create a reservation |
| DELETE | /reservations/cancel/{reservation_id}/ |
Create a reservation |
Create a .env file and add:
DATABASE_URL="postgresql+asyncpg://user:password@localhost:5432/"
SECRET_KEY="your-secret-key-here"
ALGORITHM="HS256"
ACCESS_TOKEN_EXPIRE_MINUTES=10080
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DB0=0
REDIS_DB1=1
REDIS_DB2=2
POSTGRES_USER=user
POSTGRES_PASSWORD=password
POSTGRES_DB=db2
MONGO_USER=user
MONGO_PASSWORD=password
RABBITMQ_USER="guest"
RABBITMQ_PASSWORD="guest"- Fork the repository
- Create a feature branch (
git checkout -b feature-name) - Commit your changes (
git commit -m 'Add feature') - Push to the branch (
git push origin feature-name) - Open a Pull Request
This project is licensed under the MIT License.