
- 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
101 lines
3.1 KiB
Python
101 lines
3.1 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.destination import Destination
|
|
from app.schemas.destination import (
|
|
DestinationCreate,
|
|
DestinationUpdate,
|
|
DestinationResponse,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/", response_model=DestinationResponse)
|
|
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.get("/", response_model=List[DestinationResponse])
|
|
def get_destinations(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
country: Optional[str] = Query(None),
|
|
category: Optional[str] = Query(None),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
query = db.query(Destination)
|
|
|
|
if country:
|
|
query = query.filter(Destination.country.ilike(f"%{country}%"))
|
|
if category:
|
|
query = query.filter(Destination.category == category)
|
|
|
|
destinations = query.offset(skip).limit(limit).all()
|
|
return destinations
|
|
|
|
|
|
@router.get("/{destination_id}", response_model=DestinationResponse)
|
|
def get_destination(destination_id: int, db: Session = Depends(get_db)):
|
|
destination = db.query(Destination).filter(Destination.id == destination_id).first()
|
|
if not destination:
|
|
raise HTTPException(status_code=404, detail="Destination not found")
|
|
return destination
|
|
|
|
|
|
@router.put("/{destination_id}", response_model=DestinationResponse)
|
|
def update_destination(
|
|
destination_id: int,
|
|
destination_update: DestinationUpdate,
|
|
db: Session = Depends(get_db),
|
|
):
|
|
destination = db.query(Destination).filter(Destination.id == destination_id).first()
|
|
if not destination:
|
|
raise HTTPException(status_code=404, detail="Destination not found")
|
|
|
|
update_data = destination_update.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(destination, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(destination)
|
|
return destination
|
|
|
|
|
|
@router.delete("/{destination_id}")
|
|
def delete_destination(destination_id: int, db: Session = Depends(get_db)):
|
|
destination = db.query(Destination).filter(Destination.id == destination_id).first()
|
|
if not destination:
|
|
raise HTTPException(status_code=404, detail="Destination not found")
|
|
|
|
db.delete(destination)
|
|
db.commit()
|
|
return {"message": "Destination deleted successfully"}
|
|
|
|
|
|
@router.get("/search/", response_model=List[DestinationResponse])
|
|
def search_destinations(
|
|
q: str = Query(..., description="Search query"),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
):
|
|
destinations = (
|
|
db.query(Destination)
|
|
.filter(
|
|
(Destination.name.ilike(f"%{q}%"))
|
|
| (Destination.city.ilike(f"%{q}%"))
|
|
| (Destination.country.ilike(f"%{q}%"))
|
|
| (Destination.description.ilike(f"%{q}%"))
|
|
)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
return destinations
|