Skip to content

Commit

Permalink
♻️ Refactor past events filtering and pagination
Browse files Browse the repository at this point in the history
This commit will:
- Refactor the `get_past_events` endpoint to use a fixed query limit
- Now uses localtime instead of utctime in `get_past_events` endpoint
- Refactor the 'get_past_events' endpoint to use the same mongodb-query logic as the rest of the functions for consistency
- Update pagination parameters in the test suite to reflect the new fixed limit and validate functionality.
- Ensure tests verify filtering and pagination logic with updated query constraints.
  • Loading branch information
TorgrimRL committed Dec 13, 2024
1 parent f4d105d commit 4d6f5ed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
22 changes: 14 additions & 8 deletions app/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,31 @@ def get_upcoming_events(request: Request, token: AccessTokenPayload = Depends(op


@router.get('/past-events/count')
def get_past_events_count(request: Request, token: AccessTokenPayload = Depends(optional_authentication)):
def get_past_events_count(request: Request,
token: AccessTokenPayload = Depends(optional_authentication)):
"""Get count of past events"""
db = get_database(request)

# Get current UTC datetime
current_datetime = datetime.now(timezone.utc)
current_datetime = datetime.now()

# Apply search filter according to role
search_filter = {"date": {"$lt": current_datetime}}
if token and token.role != Role.admin:
search_filter["public"] = True
if token and token.role == Role.admin:
search_filter = {
'date' : {"$lt": current_datetime}
}
else :
search_filter = {
"$and": [{'date' : {"$lt": current_datetime}}, {"public" : {"$eq": True}}]
}

# Count the number of documents that match the filter
count = db.events.count_documents(search_filter)
return {"count": count}

@router.get('/past-events')
def get_past_events(request: Request, token: AccessTokenPayload = Depends(optional_authentication),
skip: int = Query(0, ge=0),
limit: int = Query(10,ge=1,le=50)):
skip: int = Query(0, ge=0)):
""" Get last events that have passed """
# TODO: Expand endpoint to accept custom amount?
db = get_database(request)
Expand All @@ -129,12 +134,13 @@ def get_past_events(request: Request, token: AccessTokenPayload = Depends(option
if token and token.role == Role.admin:
search_filter = {'date': {"$lt": date}}

fixed_limit = 10
# Get the last events
pipeline = [
{"$match": search_filter},
{"$sort": {"date": -1}},
{"$skip": skip},
{"$limit": limit},
{"$limit": fixed_limit},
]

res = db.events.aggregate(pipeline)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_endpoints/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ def test_get_past_events_with_pagination(client):
total_past_events = response.json()["count"]

# Set pagination parameters
limit = 5
limit = 10
total_pages = (total_past_events + limit - 1) // limit

# Iterate through pages
for page in range(total_pages):
skip = page * limit
response = client.get(f'/api/event/past-events?skip={skip}&limit={limit}')
response = client.get(f'/api/event/past-events?skip={skip}')
assert response.status_code == 200
past_events = response.json()

Expand Down

0 comments on commit 4d6f5ed

Please sign in to comment.