list events only enabled #20

Merged
v4l3n71n merged 1 commits from feature/search into master 2024-10-23 21:02:25 +00:00

View File

@ -11,7 +11,7 @@ router = APIRouter()
@router.get("/events", tags=["events"], response_model=list[events.EventOut]) @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 | None = None, 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):
if limit < 1 or skip < 0 or limit < skip: if limit < 1 or skip < 0 or limit < skip:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, status_code=status.HTTP_400_BAD_REQUEST,
@ -21,23 +21,18 @@ async def read_events(authorize: Annotated[bool, Depends(permissions_checker.Per
listEvents = [] listEvents = []
event_repository = events.EventRepository(database=database.database) event_repository = events.EventRepository(database=database.database)
object_search = {} object_search = {}
if status is not None: if status is not 1:
object_search = {"status":{"$eq": status}} object_search = {"status":{"$eq": status}}
if tags is not None: if tags is not None:
object_search = {"tags":{"$eq": tags}} object_search = {"$and":[{"tags":{"$eq": tags}}, {"status":{"$eq":status}}]}
if organizers is not None: if organizers is not None:
object_search = {"organizers":{"$eq": organizers}} object_search = {"$and":[{"organizers":{"$eq": organizers}}, {"status":{"$eq":status}}]}
if id_event is not None: if id_event is not None:
eventid = ObjectId(id_event) eventid = ObjectId(id_event)
object_search = {"id": {"$regex": userid}} object_search = {"$and":[{"id":{"$regex": eventid}}, {"status":{"$eq":status}}]}
if status is not None:
object_search = {"$and":[{"id":{"$regex": eventid}}, {"status":{"$eq":status}}]}
if name is not None: if name is not None:
object_search = {"name": {"$regex": name}} object_search = {"$and":[{"name":{"$regex": name}}, {"status":{"$eq":status}}]}
if status is not None:
object_search = {"$and":[{"name":{"$regex": name}}, {"status":{"$eq":status}}]}
for event_index in event_repository.find_by(object_search, limit=limit, skip=skip): for event_index in event_repository.find_by(object_search, limit=limit, skip=skip):
@ -58,7 +53,8 @@ async def search_events(authorize: Annotated[bool, Depends(permissions_checker.P
event_repository = events.EventRepository(database=database.database) event_repository = events.EventRepository(database=database.database)
object_search = {} object_search = {}
if item is not None: if item is not None:
object_search = {"$or":[{"name":{"$regex": item}}, {"tags":{"$regex":item}},{"organizers":{"$regex": item}}]} object_search = { "$and": [{"$or":[{"name":{"$regex": item}}, {"tags":{"$regex":item}},{"organizers":{"$regex": item}}]},{"status":{"$eq":status}}]}
for event_index in event_repository.find_by(object_search, limit=limit, skip=skip): 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) 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) listEvents.append(event)