From b0bbc7e81a743696f3dca6b0f4aa5b6a3dd47669 Mon Sep 17 00:00:00 2001 From: Peter Vacho Date: Tue, 31 Dec 2024 01:26:49 +0100 Subject: [PATCH] Add endpoint to get all event categories --- src/api/categories.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/api/categories.py b/src/api/categories.py index d9717ec..abc2d84 100644 --- a/src/api/categories.py +++ b/src/api/categories.py @@ -125,6 +125,22 @@ async def get_user_categories(user_id: PydanticObjectId, user: CurrentUserDep) - return [CategoryData.from_category(category) for category in categories] +@base_router.get("/events/{event_id}/categories") +async def get_event_categories(event_id: PydanticObjectId, user: CurrentUserDep) -> list[CategoryData]: + """Get all categories that belong to given event.""" + event = await Event.get(event_id, fetch_links=True) + if event is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Event with given id doesn't exist") + + if cast(User, event.user).id != user.id: + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail="You can only access your own events", + ) + + return [CategoryData.from_category(category) for category in cast(list[Category], event.categories)] + + @categories_router.get("{category_id}") async def get_category(category_id: PydanticObjectId, user: CurrentUserDep) -> CategoryData: """Get a category by ID."""