Skip to content
This repository has been archived by the owner on Dec 15, 2024. It is now read-only.

Commit

Permalink
feat: show current events firstly, then future, and then past
Browse files Browse the repository at this point in the history
  • Loading branch information
dantetemplar committed Nov 23, 2024
1 parent 566c76e commit 616122f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions backend/src/modules/events/repository.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__all__ = ["events_repository"]


from beanie import PydanticObjectId, SortDirection
from beanie.odm.operators.find.comparison import GTE, LTE, Eq, In
from beanie.odm.operators.find.logical import And, Or
Expand Down Expand Up @@ -105,13 +106,15 @@ async def read_with_filters(
query = query.sort(
("start_date", SortDirection.ASCENDING if sort.date == "asc" else SortDirection.DESCENDING)
)

if sort.participant_count:
query = query.sort(
(
"participant_count",
SortDirection.ASCENDING if sort.participant_count == "asc" else SortDirection.DESCENDING,
)
)

if sort.age:
query = query.sort(("age_min", SortDirection.ASCENDING if sort.age == "asc" else SortDirection.DESCENDING))

Expand Down
15 changes: 14 additions & 1 deletion backend/src/modules/events/routes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import UTC, datetime, timedelta

import icalendar
from beanie import PydanticObjectId
Expand Down Expand Up @@ -72,7 +72,20 @@ async def search_events(filters: Filters, sort: Sort, pagination: Pagination) ->
"""
Search events.
"""
now_ = datetime.now(UTC)

def key(event: Event):
if event.start_date <= now_ <= event.end_date:
return 0
if event.start_date > now_:
return 1
return 2

events = await events_repository.read_with_filters(filters, sort, pagination)
if not sort.date:
# sort default: current events, future events, past events
events = sorted(events, key=key)

return SearchEventsResponse(
filters=filters,
sort=sort,
Expand Down

0 comments on commit 616122f

Please sign in to comment.