
- FastAPI application with user management, destinations, and trip planning - SQLite database with SQLAlchemy ORM - Database models for Users, Destinations, and Trips - Pydantic schemas for request/response validation - Full CRUD API endpoints for all resources - Alembic migrations setup - Health check endpoint - CORS configuration for development - Comprehensive documentation and README
93 lines
3.0 KiB
Python
93 lines
3.0 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.trip import Trip
|
|
from app.models.user import User
|
|
from app.models.destination import Destination
|
|
from app.schemas.trip import Trip as TripSchema, TripCreate, TripUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[TripSchema])
|
|
def get_trips(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|
trips = db.query(Trip).offset(skip).limit(limit).all()
|
|
return trips
|
|
|
|
|
|
@router.get("/{trip_id}", response_model=TripSchema)
|
|
def get_trip(trip_id: int, db: Session = Depends(get_db)):
|
|
trip = db.query(Trip).filter(Trip.id == trip_id).first()
|
|
if trip is None:
|
|
raise HTTPException(status_code=404, detail="Trip not found")
|
|
return trip
|
|
|
|
|
|
@router.get("/user/{user_id}", response_model=List[TripSchema])
|
|
def get_user_trips(
|
|
user_id: int, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)
|
|
):
|
|
user = db.query(User).filter(User.id == user_id).first()
|
|
if user is None:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
|
|
trips = (
|
|
db.query(Trip).filter(Trip.user_id == user_id).offset(skip).limit(limit).all()
|
|
)
|
|
return trips
|
|
|
|
|
|
@router.post("/", response_model=TripSchema)
|
|
def create_trip(trip: TripCreate, user_id: int, db: Session = Depends(get_db)):
|
|
user = db.query(User).filter(User.id == user_id).first()
|
|
if user is None:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
|
|
destination = (
|
|
db.query(Destination).filter(Destination.id == trip.destination_id).first()
|
|
)
|
|
if destination is None:
|
|
raise HTTPException(status_code=404, detail="Destination not found")
|
|
|
|
db_trip = Trip(**trip.dict(), user_id=user_id)
|
|
db.add(db_trip)
|
|
db.commit()
|
|
db.refresh(db_trip)
|
|
return db_trip
|
|
|
|
|
|
@router.put("/{trip_id}", response_model=TripSchema)
|
|
def update_trip(trip_id: int, trip: TripUpdate, db: Session = Depends(get_db)):
|
|
db_trip = db.query(Trip).filter(Trip.id == trip_id).first()
|
|
if db_trip is None:
|
|
raise HTTPException(status_code=404, detail="Trip not found")
|
|
|
|
update_data = trip.dict(exclude_unset=True)
|
|
if "destination_id" in update_data:
|
|
destination = (
|
|
db.query(Destination)
|
|
.filter(Destination.id == update_data["destination_id"])
|
|
.first()
|
|
)
|
|
if destination is None:
|
|
raise HTTPException(status_code=404, detail="Destination not found")
|
|
|
|
for field, value in update_data.items():
|
|
setattr(db_trip, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(db_trip)
|
|
return db_trip
|
|
|
|
|
|
@router.delete("/{trip_id}")
|
|
def delete_trip(trip_id: int, db: Session = Depends(get_db)):
|
|
db_trip = db.query(Trip).filter(Trip.id == trip_id).first()
|
|
if db_trip is None:
|
|
raise HTTPException(status_code=404, detail="Trip not found")
|
|
|
|
db.delete(db_trip)
|
|
db.commit()
|
|
return {"message": "Trip deleted successfully"}
|