from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from typing import List from uuid import UUID from app.core.database import get_db from app.schemas.rsvp import RSVPSchema, RSVPSchemaCreate, RSVPSchemaUpdate from app.crud.rsvp import crud_rsvp router = APIRouter() @router.post("/", response_model=RSVPSchema, operation_id="create_rsvp") def create_rsvp( rsvp_in: RSVPSchemaCreate, db: Session = Depends(get_db) ): # First, check if RSVP already exists to prevent duplicates existing_rsvp = crud_rsvp.get_rsvp_by_event_and_guest( db, event_id=rsvp_in.event_id, guest_id=rsvp_in.guest_id ) if existing_rsvp: raise HTTPException( status_code=400, detail="RSVP already exists for this event and guest" ) rsvp = crud_rsvp.create(db=db, obj_in=rsvp_in) return rsvp @router.get("/{rsvp_id}", response_model=RSVPSchema, operation_id="get_rsvp") def read_rsvp(rsvp_id: UUID, db: Session = Depends(get_db)): rsvp = crud_rsvp.get(db=db, id=str(rsvp_id)) if not rsvp: raise HTTPException(status_code=404, detail="RSVP not found") return rsvp @router.get("/", response_model=List[RSVPSchema], operation_id="get_rsvps") def read_rsvps(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): rsvps = crud_rsvp.get_multi(db=db, skip=skip, limit=limit) return rsvps @router.put("/{rsvp_id}", response_model=RSVPSchema, operation_id="update_rsvp") def update_rsvp( rsvp_id: UUID, rsvp_in: RSVPSchemaUpdate, db: Session = Depends(get_db) ): rsvp = crud_rsvp.get(db, id=str(rsvp_id)) if not rsvp: raise HTTPException(status_code=404, detail="RSVP not found") updated_rsvp = crud_rsvp.update(db, db_obj=rsvp, obj_in=rsvp_in) return updated_rsvp @router.delete("/{rsvp_id}", response_model=RSVPSchema, operation_id="delete_rsvp") def delete_rsvp(rsvp_id: UUID, db: Session = Depends(get_db)): rsvp = crud_rsvp.get(db, id=str(rsvp_id)) if not rsvp: raise HTTPException(status_code=404, detail="RSVP not found") rsvp = crud_rsvp.remove(db, id=str(rsvp_id)) return rsvp @router.patch("/{rsvp_id}/status", response_model=RSVPSchema, operation_id="update_rsvp_status") def update_rsvp_status( rsvp_id: UUID, status: RSVPSchemaUpdate, db: Session = Depends(get_db) ): rsvp = crud_rsvp.get(db, id=str(rsvp_id)) if not rsvp: raise HTTPException(status_code=404, detail="RSVP not found") # Validation of provided fields specifically for status update if not status.status: raise HTTPException(status_code=400, detail="Status field required for update.") updated_rsvp = crud_rsvp.update_rsvp_status( db, db_obj=rsvp, status=status.status, number_of_guests=status.number_of_guests, response_message=status.response_message, dietary_requirements=status.dietary_requirements, additional_info=status.additional_info ) return updated_rsvp