A Django-based railway ticket reservation system that handles berth allocation, RAC, and waiting list management.
The system is designed to manage railway ticket reservations, including berth allocation, RAC (Reservation Against Cancellation), and waiting list management. It ensures that passengers are allocated berths based on priority rules and availability.
The Railway Ticket Reservation System follows a typical Django-based architecture with a PostgreSQL database. Below is an overview of the system architecture:
- Django Application:
- Models: Define the database schema for passengers, tickets, berths, and ticket history.
- Views: Handle HTTP requests and responses for booking, canceling, and retrieving tickets.
- Serializers: Convert complex data types like querysets and model instances to native Python datatypes.
- URLs: Route URLs to the appropriate views.
- Services: Contain business logic for booking and canceling tickets.
- Error Handlers: Manage custom error responses for various scenarios.
- PostgreSQL Database:
- Stores data for passengers, tickets, berths, and ticket history.
- Utilizes Django's ORM for database interactions.
- Docker:
- Dockerfile: Defines the environment for the Django application.
- docker-compose.yml: Manages multi-container Docker applications, including the Django app and PostgreSQL database.
- API Documentation:
- Swagger UI: Provides interactive API documentation.
- Redoc UI: Alternative API documentation interface.
- Booking a Ticket:
- The user sends a POST request to the
/tickets/book/endpoint with passenger details. - The request is processed by the
BookTicketView, which calls theBookingService. - The
BookingServicevalidates the request, determines ticket type and berth allocation, and creates the ticket. - The response includes the booking details and allocated berths.
- Canceling a Ticket:
- The user sends a POST request to the
/tickets/cancel/{ticket_id}/endpoint. - The request is processed by the
CancelTicketView, which calls thecancel_ticketservice. - The service updates the ticket status to canceled and handles any necessary promotions for RAC and waiting list tickets.
- The response confirms the cancellation.
- Retrieving Booked Tickets:
- The user sends a GET request to the
/tickets/booked/endpoint. - The request is processed by the
GetBookedTicketsView, which retrieves all booked tickets from the database. - The response includes a list of booked tickets with passenger and berth details.
- Retrieving Available Tickets:
- The user sends a GET request to the
/tickets/available/endpoint. - The request is processed by the
GetAvailableTicketsView, which retrieves available berths and quota information. - The response includes the count and details of available berths.
+---------------------+ +---------------------+
| User | | Admin |
+---------+-----------+ +---------+-----------+
| |
| |
v v
+---------+-----------+ +---------+-----------+
| Django Views | | Django Admin |
| (Book, Cancel, | | Interface |
| Retrieve Tickets) | +---------+-----------+
+---------+-----------+ |
| |
v v
+---------+-----------+ +---------+-----------+
| Django Services | | Django Models |
| (Booking, | | (Passenger, Ticket,|
| Availability) | | Berth, History) |
+---------+-----------+ +---------+-----------+
| |
v v
+---------+-----------+ +---------+-----------+
| PostgreSQL Database| | API Documentation |
| (Data Storage) | | (Swagger, Redoc) |
+---------------------+ +---------------------+
| Field | Type | Description |
|---|---|---|
| name | CharField | Full name of the passenger |
| age | IntegerField | Age of the passenger |
| is_child | BooleanField | Indicates if passenger is under 5 years old |
| gender | CharField | Gender of the passenger |
| Field | Type | Description |
|---|---|---|
| ticket_type | CharField | Type of ticket (Confirmed/RAC/Waiting List) |
| status | CharField | Current status of the ticket |
| passenger | ForeignKey | Passenger this ticket belongs to |
| berth_allocation | CharField | Berth allocated to this ticket |
| created_at | DateTimeField | Timestamp when ticket was created |
| Field | Type | Description |
|---|---|---|
| berth_type | CharField | Type of berth (Lower/Upper/Side) |
| availability_status | CharField | Current availability status of the berth |
| Field | Type | Description |
|---|---|---|
| ticket | ForeignKey | Ticket whose history is being tracked |
| action | CharField | Action performed on the ticket |
| timestamp | DateTimeField | When the action was performed |
To run the Railway Ticket Reservation System application, follow these steps:
Ensure you have the following installed:
- Docker
- Docker Compose
- Clone the Repository:
git clone git@github.com:prince-dsd/brisk.git
cd brisk- Create a
.envFile: Create a.envfile in the root directory and add the following environment variables:
SECRET_KEY=<your-secret-key>
DATABASE_URL=postgres://user:password@db:5432/railway_ticket_db- Build and Run the Docker Containers:
docker-compose up --build- Apply Migrations: Open a new terminal and run the following command to apply database migrations:
docker-compose exec app python manage.py migrate- Create a Superuser: Create a superuser to access the Django admin interface:
docker-compose exec app python manage.py createsuperuser- Access the Application:
- The application will be available at
http://localhost:8000 - The Django admin interface will be available at
http://localhost:8000/admin
The API documentation is available at:
- Swagger UI:
http://localhost:8000/swagger/ - Redoc UI:
http://localhost:8000/redoc/
To run tests, use the following command:
docker-compose exec app python manage.py testEndpoint: POST /tickets/book/
Request:
{
"passengers": [
{
"name": "John Doe",
"age": 30,
"is_child": false,
"gender": "Male"
},
{
"name": "Jane Doe",
"age": 28,
"is_child": false,
"gender": "Female"
}
]
}Response:
{
"booking_id": "12345",
"status": "Confirmed",
"tickets": [
{
"ticket_id": "1",
"passenger": "John Doe",
"berth_allocation": "Lower"
},
{
"ticket_id": "2",
"passenger": "Jane Doe",
"berth_allocation": "Upper"
}
]
}Endpoint: POST /tickets/cancel/{ticket_id}/
Request:
{}Response:
{
"message": "Ticket canceled successfully."
}Endpoint: GET /tickets/booked/
Request:
{}Response:
[
{
"ticket_id": "1",
"passenger": "John Doe",
"status": "Confirmed",
"berth_allocation": "Lower"
},
{
"ticket_id": "2",
"passenger": "Jane Doe",
"status": "Confirmed",
"berth_allocation": "Upper"
}
]Endpoint: GET /tickets/available/
Request:
{}Response:
{
"available_berths": {
"Lower": 10,
"Upper": 5,
"Side": 3
},
"quota_info": {
"General": 15,
"Ladies": 3
}
}