From ea05db0d91112e3c90274eee6ee6c59c89f49686 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 23 Nov 2024 16:12:40 +0100 Subject: [PATCH] add parameter date event --- app/routers/events.py | 116 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 108 insertions(+), 8 deletions(-) diff --git a/app/routers/events.py b/app/routers/events.py index 0decbc3..589afe2 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, current_datetime: datetime | 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, date_event: datetime |None = None): if limit < 1 or skip < 0 or limit < skip: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -22,6 +22,9 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per listEvents = [] event_repository = events.EventRepository(database=database.database) object_search = {} + if date_event is not None: + start_of_day = datetime.combine(date_event, datetime.min.time()) # 2024-11-23 00:00:00 + end_of_day = datetime.combine(date_event, datetime.max.time()) if current_datetime is not None: object_search ={ "$or": [{"start_date": {"$gte": current_datetime}}, # Upcoming events @@ -34,7 +37,12 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per ]} ] } - + if date_event is not None: + object_search ={ + "$and": [{"start_date": {"$gte": start_of_day}}, + {"start_date": {"$lte": end_of_day}} # Upcoming events + ] + } if status is not 1: object_search = {"status":{"$eq": status}} if current_datetime is not None: @@ -51,7 +59,12 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per ]} ] } - ] } + ]} + if date_event is not None: + object_search = { "$and": [ {"status":{"$eq": status}}, + {"start_date": {"$gte": start_of_day}}, + {"start_date": {"$lte": end_of_day}} + ]} if tags is not None: object_search = {"$and":[{"tags":{"$eq": tags}}, {"status":{"$eq":status}}]} if current_datetime is not None: @@ -71,6 +84,13 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per } ] } + if date_event is not None: + object_search = {"$and":[{"tags":{"$eq": tags}}, + {"status":{"$eq":status}}, + {"start_date": {"$gte": start_of_day}}, + {"start_date": {"$lte": end_of_day}} + ] + } if organizers is not None: object_search = {"$and":[{"organizers":{"$eq": organizers}}, {"status":{"$eq":status}}]} if current_datetime is not None: @@ -89,6 +109,12 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per ] } ]} + if date_event is not None: + object_search = {"$and":[{"organizers":{"$eq": organizers}}, + {"status":{"$eq":status}}, + {"start_date": {"$gte": start_of_day}}, + {"start_date": {"$lte": end_of_day}} + ]} if id_event is not None: eventid = ObjectId(id_event) object_search = {"$and":[{"id":{"$regex": eventid}}, {"status":{"$eq":status}}]} @@ -107,7 +133,13 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per ]} ] } - ]} + ]} + if date_event is not None: + object_search = {"$and":[{"id":{"$regex": eventid}}, + {"status":{"$eq":status}}, + {"start_date": {"$gte": start_of_day}}, + {"start_date": {"$lte": end_of_day}} + ]} if name is not None: object_search = {"$and":[{"name":{"$regex": name}}, {"status":{"$eq":status}}]} if current_datetime is not None: @@ -126,7 +158,12 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per ] } ]} - + if date_event is not None: + object_search = {"$and":[{"name":{"$regex": name}}, + {"status":{"$eq":status}}, + {"start_date": {"$gte": start_of_day}}, + {"start_date": {"$lte": end_of_day}} + ]} for event_index in event_repository.find_by(object_search, limit=limit, skip=skip): event = events.EventOut(id=event_index.id, tags=event_index.tags, imgUrl=event_index.imgUrl, name=event_index.name, description=event_index.description, place=event_index.place, status=event_index.status, start_date=event_index.start_date, end_date=event_index.end_date) @@ -135,7 +172,7 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per @router.get("/events/search", tags=["events"], response_model=list[events.EventOut]) -async def search_events(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20, item: str | None = None, status: int = 1, min_lat: float | None = None, max_lat: float | None = None, min_lon: float | None = None, max_lon: float | None = None, current_datetime: datetime | None = None): +async def search_events(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20, item: str | None = None, status: int = 1, min_lat: float | None = None, max_lat: float | None = None, min_lon: float | None = None, max_lon: float | None = None, current_datetime: datetime | None = None, date_event: datetime | None = None): if limit < 1 or skip < 0 or limit < skip: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -145,6 +182,9 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P listEvents = [] event_repository = events.EventRepository(database=database.database) object_search = {} + if date_event is not None: + start_of_day = datetime.combine(date_event, datetime.min.time()) # 2024-11-23 00:00:00 + end_of_day = datetime.combine(date_event, datetime.max.time()) if min_lat is not None and max_lat is not None and min_lon is not None and max_lon is not None: object_search = { "$and": [ @@ -193,7 +233,26 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P } ] } - + if date_event is not None: + object_search = { + "$and": [ + {"status": {"$eq": status}}, + { + "latitude": {"$gte": min_lat}, # Minimum latitude + }, + { + "latitude": {"$lte": max_lat}, # Maximum latitude + }, + { + "longitude": {"$gte": min_lon}, # Minimum longitude + }, + { + "longitude": {"$lte": max_lon}, # Maximum longitude + }, + {"start_date": {"$gte": start_of_day}}, + {"start_date": {"$lte": end_of_day}} + ] + } if item is not None: object_search = { "$and": [ @@ -232,6 +291,21 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P } ] } + if date_event is not None: + object_search = { + "$and": [ + { + "$or": [ + {"name": {"$regex": item}}, + {"tags": {"$regex": item}}, + {"organizers": {"$regex": item}} + ] + }, + {"status": {"$eq": status}}, + {"start_date": {"$gte": start_of_day}}, + {"start_date": {"$lte": end_of_day}} + ] + } if min_lat is not None and max_lat is not None and min_lon is not None and max_lon is not None: object_search = { "$and": [ @@ -294,7 +368,33 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P } ] } - + if date_event is not None: + object_search = { + "$and": [ + { + "$or": [ + {"name": {"$regex": item}}, + {"tags": {"$regex": item}}, + {"organizers": {"$regex": item}} + ] + }, + {"status": {"$eq": status}}, + { + "latitude": {"$gte": min_lat}, # Minimum latitude + }, + { + "latitude": {"$lte": max_lat}, # Maximum latitude + }, + { + "longitude": {"$gte": min_lon}, # Minimum longitude + }, + { + "longitude": {"$lte": max_lon}, # Maximum longitude + }, + {"start_date": {"$gte": start_of_day}}, + {"start_date": {"$lte": end_of_day}} + ] + } for event_index in event_repository.find_by(object_search, limit=limit, skip=skip): event = events.EventOut(id=event_index.id, tags=event_index.tags, imgUrl=event_index.imgUrl, name=event_index.name, description=event_index.description, place=event_index.place, status=event_index.status, start_date=event_index.start_date, end_date=event_index.end_date) listEvents.append(event)