From 49a22bc567a7867a82fdab6a14228ddaf6804137 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 9 Nov 2024 16:19:52 +0100 Subject: [PATCH] add current_datetime in events --- app/routers/events.py | 83 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/app/routers/events.py b/app/routers/events.py index cdff7b8..66ac51c 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -12,7 +12,7 @@ router = APIRouter() @router.get("/events", tags=["events"], response_model=list[events.EventOut]) -async def read_events(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20, id_event: str | None = None, name: str | None = None, status: int = 1, tags: str | None = None, organizers: str | None = None): +async def read_events(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20, id_event: str | None = None, name: str | None = None, status: int = 1, tags: str | None = None, organizers: str | None = None, current_datetime: datetime | None = None): if limit < 1 or skip < 0 or limit < skip: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -24,16 +24,95 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per object_search = {} if status is not 1: object_search = {"status":{"$eq": status}} - + if current_datetime is not None: + object_search = { "$and": [ {"status":{"$eq": status}}, + { + "$or": [ + {"start_date": {"$gte": current_datetime}}, # Upcoming events + {"$and": [ # Ongoing events + {"start_date": {"$lte": current_datetime}}, # Already started + {"$or": [ + {"end_date": {"$gte": current_datetime}}, # End date in the future + {"end_date": None} # No end date set + ]} + ]} + ] + } + ] } if tags is not None: object_search = {"$and":[{"tags":{"$eq": tags}}, {"status":{"$eq":status}}]} + if current_datetime is not None: + object_search = {"$and":[{"tags":{"$eq": tags}}, + {"status":{"$eq":status}}, + { + "$or": [ + {"start_date": {"$gte": current_datetime}}, # Upcoming events + {"$and": [ # Ongoing events + {"start_date": {"$lte": current_datetime}}, # Already started + {"$or": [ + {"end_date": {"$gte": current_datetime}}, # End date in the future + {"end_date": None} # No end date set + ]} + ]} + ] + } + ] + } if organizers is not None: object_search = {"$and":[{"organizers":{"$eq": organizers}}, {"status":{"$eq":status}}]} + if current_datetime is not None: + object_search = {"$and":[{"organizers":{"$eq": organizers}}, + {"status":{"$eq":status}}, + { + "$or": [ + {"start_date": {"$gte": current_datetime}}, # Upcoming events + {"$and": [ # Ongoing events + {"start_date": {"$lte": current_datetime}}, # Already started + {"$or": [ + {"end_date": {"$gte": current_datetime}}, # End date in the future + {"end_date": None} # No end date set + ]} + ]} + ] + } + ]} if id_event is not None: eventid = ObjectId(id_event) object_search = {"$and":[{"id":{"$regex": eventid}}, {"status":{"$eq":status}}]} + if current_datetime is not None: + object_search = {"$and":[{"id":{"$regex": eventid}}, + {"status":{"$eq":status}}, + { + "$or": [ + {"start_date": {"$gte": current_datetime}}, # Upcoming events + {"$and": [ # Ongoing events + {"start_date": {"$lte": current_datetime}}, # Already started + {"$or": [ + {"end_date": {"$gte": current_datetime}}, # End date in the future + {"end_date": None} # No end date set + ]} + ]} + ] + } + ]} if name is not None: object_search = {"$and":[{"name":{"$regex": name}}, {"status":{"$eq":status}}]} + if current_datetime is not None: + object_search = {"$and":[{"name":{"$regex": name}}, + {"status":{"$eq":status}}, + { + "$or": [ + {"start_date": {"$gte": current_datetime}}, # Upcoming events + {"$and": [ # Ongoing events + {"start_date": {"$lte": current_datetime}}, # Already started + {"$or": [ + {"end_date": {"$gte": current_datetime}}, # End date in the future + {"end_date": None} # No end date set + ]} + ]} + ] + } + ]} for event_index in event_repository.find_by(object_search, limit=limit, skip=skip):