From 4c2d4646ce17739dd2308bcdd46c4e0ee51fd918 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 4 Nov 2024 22:57:53 +0100 Subject: [PATCH 1/2] add lat and long as parameter --- app/routers/events.py | 53 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/app/routers/events.py b/app/routers/events.py index 18e9fc5..52610e8 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -42,7 +42,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): +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): if limit < 1 or skip < 0 or limit < skip: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -52,6 +52,31 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P listEvents = [] event_repository = events.EventRepository(database=database.database) object_search = {} + 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": [ + { + "$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 + } + ] + } if item is not None: object_search = { "$and": [ @@ -65,6 +90,32 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P {"status": {"$eq": status}} ] } + 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": [ + { + "$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 + } + ] + } + 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, zip_code=event_index.zip_code, city=event_index.city, country=event_index.country, status=event_index.status, start_date=event_index.start_date, end_date=event_index.end_date) listEvents.append(event) From 115a5acb6faa6ba842f2d34414c4c2c2849ab90c Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 4 Nov 2024 22:58:51 +0100 Subject: [PATCH 2/2] add another object_search --- app/routers/events.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/app/routers/events.py b/app/routers/events.py index 52610e8..73e24a3 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -55,13 +55,6 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P 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": [ - { - "$or": [ - {"name": {"$regex": item}}, - {"tags": {"$regex": item}}, - {"organizers": {"$regex": item}} - ] - }, {"status": {"$eq": status}}, { "latitude": {"$gte": min_lat}, # Minimum latitude