Add endpoint to get all event categories

This commit is contained in:
Peter Vacho 2024-12-31 01:26:49 +01:00
parent 1a3cca9712
commit b0bbc7e81a
Signed by: school
GPG key ID: 8CFC3837052871B4

View file

@ -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."""