Validate uniqueness of invitation codes during guest creation to prevent duplicates. Automatically generate an 8-character code if none is provided, ensuring consistent data handling. Updated tests and schemas to support these changes.
65 lines
2.5 KiB
Python
65 lines
2.5 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)):
|
|
if guest_in.invitation_code:
|
|
existing_guest = guest_crud.get_by_invitation_code(db, guest_in.invitation_code)
|
|
if existing_guest:
|
|
raise HTTPException(
|
|
status_code=400, detail="Guest with this invitation code already exists"
|
|
)
|
|
else:
|
|
guest_in.invitation_code = str(uuid.uuid4())[:8]
|
|
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 |