Introduce a new API for managing event guests, including endpoints for creating, reading, updating, deleting, and changing guest status. Added corresponding Pydantic schemas, database CRUD logic, and extensive test coverage to validate functionality. Integrated the guests API under the events router.
57 lines
2.1 KiB
Python
57 lines
2.1 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from app.schemas.guests import GuestCreate, GuestUpdate, GuestRead
|
|
from app.crud.guest import guest_crud
|
|
from typing import List
|
|
import uuid
|
|
|
|
from app.core.database import get_db
|
|
from app.models import GuestStatus
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/", response_model=GuestRead, operation_id="create_guest")
|
|
def create_guest(guest_in: GuestCreate, db: Session = Depends(get_db)):
|
|
guest = guest_crud.create(db, obj_in=guest_in)
|
|
return guest
|
|
|
|
|
|
@router.get("/{guest_id}", response_model=GuestRead, operation_id="get_guest")
|
|
def read_guest(guest_id: uuid.UUID, db: Session = Depends(get_db)):
|
|
guest = guest_crud.get(db, guest_id)
|
|
if not guest:
|
|
raise HTTPException(status_code=404, detail="Guest not found")
|
|
return guest
|
|
|
|
|
|
@router.get("/", response_model=List[GuestRead], operation_id="get_guests")
|
|
def read_guests(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|
guests = guest_crud.get_multi(db, skip=skip, limit=limit)
|
|
return guests
|
|
|
|
|
|
@router.put("/{guest_id}", response_model=GuestRead, operation_id="update_guest")
|
|
def update_guest(guest_id: uuid.UUID, guest_in: GuestUpdate, db: Session = Depends(get_db)):
|
|
guest = guest_crud.get(db, guest_id)
|
|
if not guest:
|
|
raise HTTPException(status_code=404, detail="Guest not found")
|
|
guest = guest_crud.update(db, db_obj=guest, obj_in=guest_in)
|
|
return guest
|
|
|
|
|
|
@router.delete("/{guest_id}", response_model=GuestRead, operation_id="delete_guest")
|
|
def delete_guest(guest_id: uuid.UUID, db: Session = Depends(get_db)):
|
|
guest = guest_crud.get(db, guest_id)
|
|
if not guest:
|
|
raise HTTPException(status_code=404, detail="Guest not found")
|
|
guest = guest_crud.remove(db, guest_id=str(guest_id))
|
|
return guest
|
|
|
|
|
|
@router.patch("/{guest_id}/status", response_model=GuestRead)
|
|
def set_guest_status(guest_id: uuid.UUID, status: GuestStatus, db: Session = Depends(get_db)):
|
|
guest = guest_crud.update_status(db, guest_id=guest_id, status=status)
|
|
if not guest:
|
|
raise HTTPException(status_code=404, detail="Guest not found")
|
|
return guest |