diff --git a/backend/app/api/routes/events/router.py b/backend/app/api/routes/events/router.py index f6a7c62..d02c209 100644 --- a/backend/app/api/routes/events/router.py +++ b/backend/app/api/routes/events/router.py @@ -22,11 +22,12 @@ from app.schemas.events import ( ) from app.api.routes.events import guests - +from app.api.routes.events import rsvps logger = logging.getLogger(__name__) events_router = APIRouter() events_router.include_router(guests.router, prefix="/guests", tags=["guests"]) +events_router.include_router(rsvps.router, prefix="/rsvps", tags=["rsvps"]) def validate_event_access( *, diff --git a/backend/app/api/routes/events/rsvps.py b/backend/app/api/routes/events/rsvps.py new file mode 100644 index 0000000..f738071 --- /dev/null +++ b/backend/app/api/routes/events/rsvps.py @@ -0,0 +1,90 @@ +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 \ No newline at end of file