Add endpoint for leaving invited events
This commit is contained in:
parent
83b55d43ff
commit
d19e2a5473
|
@ -320,6 +320,30 @@ async def delete_event(event_id: PydanticObjectId, user: CurrentUserDep) -> Resp
|
||||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
|
@events_router.delete("/{event_id}/invited")
|
||||||
|
async def delete_invited_event(event_id: PydanticObjectId, user: CurrentUserDep) -> Response:
|
||||||
|
"""Remove yourself as an attendee of 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")
|
||||||
|
|
||||||
|
for attendee in event.attendees:
|
||||||
|
if cast(User, attendee).id == user.id:
|
||||||
|
break
|
||||||
|
else: # nobreak
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_403_FORBIDDEN,
|
||||||
|
detail="You can only access events you attend",
|
||||||
|
)
|
||||||
|
|
||||||
|
event.attendees.remove(attendee)
|
||||||
|
_ = await event.replace()
|
||||||
|
|
||||||
|
# NOTE: We might want to notify the event owner about the attendee leaving
|
||||||
|
|
||||||
|
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
@events_router.post("")
|
@events_router.post("")
|
||||||
async def create_event(
|
async def create_event(
|
||||||
user: CurrentUserDep,
|
user: CurrentUserDep,
|
||||||
|
|
Loading…
Reference in a new issue