From 436e6d68ddbf2cc769ef11eaf3d9ee25fe427244 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Wed, 23 Oct 2024 21:44:33 +0200 Subject: [PATCH] add search --- app/routers/events.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/routers/events.py b/app/routers/events.py index 1a6ba61..0d462f3 100644 --- a/app/routers/events.py +++ b/app/routers/events.py @@ -46,6 +46,25 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per return listEvents +@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 | None = None): + if limit < 1 or skip < 0 or limit < skip: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="skip should be greater than 0 and limit should be greater than 1. Limit should be greater than skip" + ) + limit = limit + skip + listEvents = [] + event_repository = events.EventRepository(database=database.database) + object_search = {} + if item is not None: + object_search = {"$or":[{"name":{"$regex": item}}, {"tags":{"$regex":item}},{"organizers":{"$regex": item}}]} + 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) + return listEvents + + @router.get("/events/me",tags=["events"]) async def read_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): event_repository = events.EventRepository(database=database.database) -- 2.47.1