
- User authentication with JWT tokens - Trip management with itineraries - Destination database with search functionality - Booking management for flights, hotels, car rentals, activities - SQLite database with Alembic migrations - Health monitoring endpoint - CORS enabled for all origins - Complete API documentation at /docs and /redoc - Environment variable support for SECRET_KEY Requirements for production: - Set SECRET_KEY environment variable
117 lines
3.2 KiB
Python
117 lines
3.2 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Optional
|
|
from app.db.session import get_db
|
|
from app.models.user import User
|
|
from app.models.booking import Booking
|
|
from app.schemas.booking import BookingCreate, BookingUpdate, BookingResponse
|
|
from app.api.routes.auth import get_current_user
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/", response_model=BookingResponse)
|
|
def create_booking(
|
|
booking: BookingCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
db_booking = Booking(**booking.dict(), user_id=current_user.id)
|
|
db.add(db_booking)
|
|
db.commit()
|
|
db.refresh(db_booking)
|
|
return db_booking
|
|
|
|
|
|
@router.get("/", response_model=List[BookingResponse])
|
|
def get_user_bookings(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
booking_type: Optional[str] = Query(None),
|
|
status: Optional[str] = Query(None),
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
query = db.query(Booking).filter(Booking.user_id == current_user.id)
|
|
|
|
if booking_type:
|
|
query = query.filter(Booking.booking_type == booking_type)
|
|
if status:
|
|
query = query.filter(Booking.status == status)
|
|
|
|
bookings = query.offset(skip).limit(limit).all()
|
|
return bookings
|
|
|
|
|
|
@router.get("/{booking_id}", response_model=BookingResponse)
|
|
def get_booking(
|
|
booking_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
booking = (
|
|
db.query(Booking)
|
|
.filter(Booking.id == booking_id, Booking.user_id == current_user.id)
|
|
.first()
|
|
)
|
|
if not booking:
|
|
raise HTTPException(status_code=404, detail="Booking not found")
|
|
return booking
|
|
|
|
|
|
@router.put("/{booking_id}", response_model=BookingResponse)
|
|
def update_booking(
|
|
booking_id: int,
|
|
booking_update: BookingUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
booking = (
|
|
db.query(Booking)
|
|
.filter(Booking.id == booking_id, Booking.user_id == current_user.id)
|
|
.first()
|
|
)
|
|
if not booking:
|
|
raise HTTPException(status_code=404, detail="Booking not found")
|
|
|
|
update_data = booking_update.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(booking, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(booking)
|
|
return booking
|
|
|
|
|
|
@router.delete("/{booking_id}")
|
|
def delete_booking(
|
|
booking_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
booking = (
|
|
db.query(Booking)
|
|
.filter(Booking.id == booking_id, Booking.user_id == current_user.id)
|
|
.first()
|
|
)
|
|
if not booking:
|
|
raise HTTPException(status_code=404, detail="Booking not found")
|
|
|
|
db.delete(booking)
|
|
db.commit()
|
|
return {"message": "Booking deleted successfully"}
|
|
|
|
|
|
@router.get("/trip/{trip_id}", response_model=List[BookingResponse])
|
|
def get_trip_bookings(
|
|
trip_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
bookings = (
|
|
db.query(Booking)
|
|
.filter(Booking.trip_id == trip_id, Booking.user_id == current_user.id)
|
|
.all()
|
|
)
|
|
return bookings
|