
- 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
151 lines
3.7 KiB
Python
151 lines
3.7 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
from app.db.session import get_db
|
|
from app.models.user import User
|
|
from app.models.trip import Trip, Itinerary
|
|
from app.schemas.trip import (
|
|
TripCreate,
|
|
TripUpdate,
|
|
TripResponse,
|
|
ItineraryCreate,
|
|
ItineraryResponse,
|
|
)
|
|
from app.api.routes.auth import get_current_user
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/", response_model=TripResponse)
|
|
def create_trip(
|
|
trip: TripCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
db_trip = Trip(**trip.dict(), user_id=current_user.id)
|
|
db.add(db_trip)
|
|
db.commit()
|
|
db.refresh(db_trip)
|
|
return db_trip
|
|
|
|
|
|
@router.get("/", response_model=List[TripResponse])
|
|
def get_user_trips(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
trips = (
|
|
db.query(Trip)
|
|
.filter(Trip.user_id == current_user.id)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
return trips
|
|
|
|
|
|
@router.get("/{trip_id}", response_model=TripResponse)
|
|
def get_trip(
|
|
trip_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
trip = (
|
|
db.query(Trip)
|
|
.filter(Trip.id == trip_id, Trip.user_id == current_user.id)
|
|
.first()
|
|
)
|
|
if not trip:
|
|
raise HTTPException(status_code=404, detail="Trip not found")
|
|
return trip
|
|
|
|
|
|
@router.put("/{trip_id}", response_model=TripResponse)
|
|
def update_trip(
|
|
trip_id: int,
|
|
trip_update: TripUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
trip = (
|
|
db.query(Trip)
|
|
.filter(Trip.id == trip_id, Trip.user_id == current_user.id)
|
|
.first()
|
|
)
|
|
if not trip:
|
|
raise HTTPException(status_code=404, detail="Trip not found")
|
|
|
|
update_data = trip_update.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(trip, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(trip)
|
|
return trip
|
|
|
|
|
|
@router.delete("/{trip_id}")
|
|
def delete_trip(
|
|
trip_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
trip = (
|
|
db.query(Trip)
|
|
.filter(Trip.id == trip_id, Trip.user_id == current_user.id)
|
|
.first()
|
|
)
|
|
if not trip:
|
|
raise HTTPException(status_code=404, detail="Trip not found")
|
|
|
|
db.delete(trip)
|
|
db.commit()
|
|
return {"message": "Trip deleted successfully"}
|
|
|
|
|
|
@router.post("/{trip_id}/itinerary", response_model=ItineraryResponse)
|
|
def add_itinerary_item(
|
|
trip_id: int,
|
|
itinerary: ItineraryCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
trip = (
|
|
db.query(Trip)
|
|
.filter(Trip.id == trip_id, Trip.user_id == current_user.id)
|
|
.first()
|
|
)
|
|
if not trip:
|
|
raise HTTPException(status_code=404, detail="Trip not found")
|
|
|
|
db_itinerary = Itinerary(**itinerary.dict(), trip_id=trip_id)
|
|
db.add(db_itinerary)
|
|
db.commit()
|
|
db.refresh(db_itinerary)
|
|
return db_itinerary
|
|
|
|
|
|
@router.get("/{trip_id}/itinerary", response_model=List[ItineraryResponse])
|
|
def get_trip_itinerary(
|
|
trip_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
trip = (
|
|
db.query(Trip)
|
|
.filter(Trip.id == trip_id, Trip.user_id == current_user.id)
|
|
.first()
|
|
)
|
|
if not trip:
|
|
raise HTTPException(status_code=404, detail="Trip not found")
|
|
|
|
itinerary = (
|
|
db.query(Itinerary)
|
|
.filter(Itinerary.trip_id == trip_id)
|
|
.order_by(Itinerary.day_number, Itinerary.start_time)
|
|
.all()
|
|
)
|
|
return itinerary
|