diff --git a/app/routers/events.py b/app/routers/events.py index 589afe2..ad5ae5f 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, date_event: 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, start_date: datetime | None = None, end_date: datetime | None = None): if limit < 1 or skip < 0 or limit < skip: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -22,9 +22,15 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per listEvents = [] event_repository = events.EventRepository(database=database.database) object_search = {} + date_selected: bool = False 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()) + end_of_day = datetime.combine(date_event, datetime.max.time()) + date_selected = True + if start_date is not None and end_date is not None: + start_of_day = datetime.combine(start_date, datetime.min.time()) # 2024-11-23 00:00:00 + end_of_day = datetime.combine(end_date, datetime.max.time()) + date_selected = True if current_datetime is not None: object_search ={ "$or": [{"start_date": {"$gte": current_datetime}}, # Upcoming events @@ -37,7 +43,7 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per ]} ] } - if date_event is not None: + if date_selected is True: object_search ={ "$and": [{"start_date": {"$gte": start_of_day}}, {"start_date": {"$lte": end_of_day}} # Upcoming events @@ -60,7 +66,7 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per ] } ]} - if date_event is not None: + if date_selected is True: object_search = { "$and": [ {"status":{"$eq": status}}, {"start_date": {"$gte": start_of_day}}, {"start_date": {"$lte": end_of_day}} @@ -84,7 +90,7 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per } ] } - if date_event is not None: + if date_selected is True: object_search = {"$and":[{"tags":{"$eq": tags}}, {"status":{"$eq":status}}, {"start_date": {"$gte": start_of_day}}, @@ -109,7 +115,7 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per ] } ]} - if date_event is not None: + if date_selected is True: object_search = {"$and":[{"organizers":{"$eq": organizers}}, {"status":{"$eq":status}}, {"start_date": {"$gte": start_of_day}}, @@ -134,7 +140,7 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per ] } ]} - if date_event is not None: + if date_selected is True: object_search = {"$and":[{"id":{"$regex": eventid}}, {"status":{"$eq":status}}, {"start_date": {"$gte": start_of_day}}, @@ -158,7 +164,7 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per ] } ]} - if date_event is not None: + if date_selected is True: object_search = {"$and":[{"name":{"$regex": name}}, {"status":{"$eq":status}}, {"start_date": {"$gte": start_of_day}}, @@ -172,7 +178,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, date_event: 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, start_date: datetime | None = None, end_date: datetime | None = None): if limit < 1 or skip < 0 or limit < skip: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -182,9 +188,15 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P listEvents = [] event_repository = events.EventRepository(database=database.database) object_search = {} + date_selected: bool = False 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()) + end_of_day = datetime.combine(date_event, datetime.max.time()) + date_selected = True + if start_date is not None and end_date is not None: + start_of_day = datetime.combine(start_date, datetime.min.time()) # 2024-11-23 00:00:00 + end_of_day = datetime.combine(dateend_date_event, datetime.max.time()) + date_selected = True 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": [ @@ -233,7 +245,7 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P } ] } - if date_event is not None: + if date_selected is True: object_search = { "$and": [ {"status": {"$eq": status}}, @@ -291,7 +303,7 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P } ] } - if date_event is not None: + if date_selected is True: object_search = { "$and": [ { @@ -368,7 +380,7 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P } ] } - if date_event is not None: + if date_selected is True: object_search = { "$and": [ {