
- 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
68 lines
2.2 KiB
Python
68 lines
2.2 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.destination import Destination
|
|
from app.schemas.destination import (
|
|
Destination as DestinationSchema,
|
|
DestinationCreate,
|
|
DestinationUpdate,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[DestinationSchema])
|
|
def get_destinations(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|
destinations = db.query(Destination).offset(skip).limit(limit).all()
|
|
return destinations
|
|
|
|
|
|
@router.get("/{destination_id}", response_model=DestinationSchema)
|
|
def get_destination(destination_id: int, db: Session = Depends(get_db)):
|
|
destination = db.query(Destination).filter(Destination.id == destination_id).first()
|
|
if destination is None:
|
|
raise HTTPException(status_code=404, detail="Destination not found")
|
|
return destination
|
|
|
|
|
|
@router.post("/", response_model=DestinationSchema)
|
|
def create_destination(destination: DestinationCreate, db: Session = Depends(get_db)):
|
|
db_destination = Destination(**destination.dict())
|
|
db.add(db_destination)
|
|
db.commit()
|
|
db.refresh(db_destination)
|
|
return db_destination
|
|
|
|
|
|
@router.put("/{destination_id}", response_model=DestinationSchema)
|
|
def update_destination(
|
|
destination_id: int, destination: DestinationUpdate, db: Session = Depends(get_db)
|
|
):
|
|
db_destination = (
|
|
db.query(Destination).filter(Destination.id == destination_id).first()
|
|
)
|
|
if db_destination is None:
|
|
raise HTTPException(status_code=404, detail="Destination not found")
|
|
|
|
update_data = destination.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(db_destination, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(db_destination)
|
|
return db_destination
|
|
|
|
|
|
@router.delete("/{destination_id}")
|
|
def delete_destination(destination_id: int, db: Session = Depends(get_db)):
|
|
db_destination = (
|
|
db.query(Destination).filter(Destination.id == destination_id).first()
|
|
)
|
|
if db_destination is None:
|
|
raise HTTPException(status_code=404, detail="Destination not found")
|
|
|
|
db.delete(db_destination)
|
|
db.commit()
|
|
return {"message": "Destination deleted successfully"}
|