Add event-specific guest gift reservations endpoint
Introduced a function `get_event_guest_gift_reservations` in the CRUD layer to fetch gift reservations filtered by event ID. Updated the API endpoint to optionally accept an `event_id` query parameter for retrieving reservations specific to an event.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
from typing import List, Optional, Dict, Any
|
from typing import List, Optional, Dict, Any
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Path
|
from fastapi import APIRouter, Depends, HTTPException, Path, Query
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from app.api.dependencies.auth import get_current_active_user, get_current_user, get_optional_current_user
|
from app.api.dependencies.auth import get_current_active_user, get_current_user, get_optional_current_user
|
||||||
@@ -1050,19 +1050,25 @@ def read_gift_purchases_by_guest(
|
|||||||
return gift_purchase_crud.get_gift_reservations_by_guest(db, guest_id=guest_id)
|
return gift_purchase_crud.get_gift_reservations_by_guest(db, guest_id=guest_id)
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
"/purchases/guest/all",
|
"/reservations/guests",
|
||||||
response_model=List[GiftPurchase],
|
response_model=List[GiftPurchase],
|
||||||
operation_id="read_guests_gift_purchases"
|
operation_id="read_guests_gift_reservations",
|
||||||
)
|
)
|
||||||
def read_all_guest_gift_reservations(
|
def read_all_guest_gift_reservations(
|
||||||
*,
|
*,
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
|
event_id: Optional[str] = Query(None, description="Optional event ID to filter reservations by event"),
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""
|
"""
|
||||||
Retrieve all guest gift reservations.
|
Retrieve all guest gift reservations, optionally filtered by event ID.
|
||||||
"""
|
"""
|
||||||
reservations = gift_purchase_crud.get_all_guest_gift_reservations(db=db)
|
if event_id:
|
||||||
|
event_id = UUID(event_id)
|
||||||
|
reservations = gift_purchase_crud.get_event_guest_gift_reservations(db=db, event_id=event_id)
|
||||||
|
else:
|
||||||
|
reservations = gift_purchase_crud.get_all_guest_gift_reservations(db=db)
|
||||||
|
|
||||||
if not reservations:
|
if not reservations:
|
||||||
reservations = []
|
reservations = []
|
||||||
|
|
||||||
return reservations
|
return reservations
|
||||||
|
|||||||
@@ -387,6 +387,41 @@ class CRUDGiftPurchase(CRUDBase[GiftPurchase, GiftPurchaseCreate, GiftPurchaseUp
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def get_event_guest_gift_reservations(self, db: Session, event_id: UUID | str) -> List[GiftPurchase]:
|
||||||
|
"""Retrieve all gift reservations for guests belonging to a specific event."""
|
||||||
|
event_id = event_id if isinstance(event_id, UUID) else UUID(event_id)
|
||||||
|
|
||||||
|
stmt = (
|
||||||
|
select(Guest)
|
||||||
|
.where(Guest.event_id == event_id)
|
||||||
|
.options(joinedload(Guest.gifts))
|
||||||
|
)
|
||||||
|
|
||||||
|
# Correct: Call unique() on the RESULT, not on the stmt
|
||||||
|
guests = db.execute(stmt).unique().scalars().all()
|
||||||
|
|
||||||
|
results = []
|
||||||
|
for guest in guests:
|
||||||
|
for gift in guest.gifts:
|
||||||
|
reservation_stmt = select(GuestGifts).where(
|
||||||
|
(GuestGifts.c.guest_id == guest.id) &
|
||||||
|
(GuestGifts.c.gift_id == gift.id)
|
||||||
|
)
|
||||||
|
|
||||||
|
reservation = db.execute(reservation_stmt).first()
|
||||||
|
|
||||||
|
if reservation:
|
||||||
|
purchase = GiftPurchase(
|
||||||
|
id=UUID('00000000-0000-0000-0000-000000000000'),
|
||||||
|
gift_id=gift.id,
|
||||||
|
guest_id=guest.id,
|
||||||
|
quantity=1,
|
||||||
|
purchased_at=reservation.reserved_at,
|
||||||
|
notes=reservation.notes
|
||||||
|
)
|
||||||
|
results.append(purchase)
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
# Create CRUD instances
|
# Create CRUD instances
|
||||||
gift_item_crud = CRUDGiftItem(GiftItem)
|
gift_item_crud = CRUDGiftItem(GiftItem)
|
||||||
|
|||||||
Reference in New Issue
Block a user