Simplified IRCTC-style backend built with Django + DRF, using:
- MySQL for transactional data (users, trains, bookings)
- MongoDB for API request logs + analytics
- JWT authentication (SimpleJWT)
This repository implements the exact APIs required in the assignment.
POST /api/register/→ create user, return JWT tokensPOST /api/login/→ login by email+password, return JWT tokens- JWT required for all other endpoints (
Authorization: Bearer <token>) GET /api/trains/search/→ search trains (+ filtersdate,limit,offset)- Train search logs to MongoDB with
endpoint,params,user_id,execution_time_ms POST /api/trains/→ admin-only train create/update (upsert bytrain_number)POST /api/bookings/→ atomic seat booking, validates availability, deducts seatsGET /api/bookings/my/→ returns user bookings with train detailsGET /api/analytics/top-routes/→ top 5 searched routes from Mongo logs
python -m venv .venv
\.\.venv\Scripts\Activate.ps1
pip install -r requirements.txtCopy .env.example → .env and fill in values:
MYSQL_*(database credentials)MONGO_URI(MongoDB connection string)
If MYSQL_NAME is empty, the project falls back to SQLite for quick smoke tests.
If MongoDB is installed as a Windows service (PowerShell as Admin):
net start MongoDBConfirm it’s listening:
netstat -ano | findstr 27017If you get MySQL error 1045 Access denied, create a dedicated user:
CREATE DATABASE IF NOT EXISTS irctc_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER IF NOT EXISTS 'irctc_user'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON irctc_db.* TO 'irctc_user'@'localhost';
FLUSH PRIVILEGES;Then set in .env:
MYSQL_NAME=irctc_db
MYSQL_USER=irctc_user
MYSQL_PASSWORD=your_strong_password
python manage.py makemigrations
python manage.py migratepython manage.py createsuperuserpython manage.py runserverNote: / intentionally returns 404. Use /admin/ or /api/*.
All endpoints (except register/login) require:
Authorization: Bearer <access_token>
curl -X POST http://127.0.0.1:8000/api/register/ \
-H "Content-Type: application/json" \
-d "{\"name\":\"Aman\",\"email\":\"aman@example.com\",\"password\":\"Secret123!\"}"curl -X POST http://127.0.0.1:8000/api/login/ \
-H "Content-Type: application/json" \
-d "{\"email\":\"aman@example.com\",\"password\":\"Secret123!\"}"curl -X POST http://127.0.0.1:8000/api/trains/ \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <ADMIN_ACCESS>" \
-d "{\"train_number\":\"12951\",\"name\":\"Rajdhani\",\"source\":\"DEL\",\"destination\":\"BOM\",\"travel_date\":\"2026-02-25\",\"departure_time\":\"09:00:00\",\"arrival_time\":\"21:00:00\",\"total_seats\":200,\"available_seats\":200,\"is_active\":true}"curl "http://127.0.0.1:8000/api/trains/search/?source=DEL&destination=BOM&date=2026-02-25&limit=10&offset=0" \
-H "Authorization: Bearer <ACCESS>"curl -X POST http://127.0.0.1:8000/api/bookings/ \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <ACCESS>" \
-d "{\"train_id\":1,\"seats\":2}"curl http://127.0.0.1:8000/api/bookings/my/ \
-H "Authorization: Bearer <ACCESS>"curl http://127.0.0.1:8000/api/analytics/top-routes/ \
-H "Authorization: Bearer <ACCESS>"Each train-search request inserts a document shaped like:
{
"ts": "2026-02-24T12:00:00Z",
"endpoint": "/api/trains/search/",
"method": "GET",
"user_id": 1,
"params": {"source": ["DEL"], "destination": ["BOM"], "limit": ["10"], "offset": ["0"]},
"execution_time_ms": 12,
"source": "DEL",
"destination": "BOM"
}Optional sample logs are included in mongo_log_samples.json.
- Booking is protected against overselling using a DB transaction + row lock (
select_for_update()). - MongoDB logging is best-effort (never breaks train search). Analytics requires MongoDB running.