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

Commit

Permalink
feat: add counter route
Browse files Browse the repository at this point in the history
  • Loading branch information
dantetemplar committed Nov 23, 2024
1 parent e2aa2ff commit d6b0ae8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion backend/src/modules/events/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ async def create_many(self, events: list[Event]) -> bool:
return False
return True

async def read_with_filters(self, filters: Filters, sort: Sort, pagination: Pagination) -> list[Event]:
async def read_with_filters(
self, filters: Filters, sort: Sort, pagination: Pagination, count: bool = False
) -> list[Event] | int:
query = Event.all()

# Apply filters
Expand Down Expand Up @@ -88,6 +90,9 @@ async def read_with_filters(self, filters: Filters, sort: Sort, pagination: Pagi
if filters.query:
query = query.find({"$text": {"$search": filters.query}})

if count:
return await query.count()

# Apply sorting
if sort.date:
query = query.sort(
Expand Down
9 changes: 9 additions & 0 deletions backend/src/modules/events/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ async def search_events(filters: Filters, sort: Sort, pagination: Pagination) ->
)


@router.post("/search/count", responses={200: {"description": "Count events"}})
async def count_events(filters: Filters) -> int:
"""
Count filtered events.
"""
count = await events_repository.read_with_filters(filters, Sort(), Pagination(page_size=0, page_no=0), count=True)
return count


class RegionsFilterVariants(BaseModel):
region: str | None
"Название региона"
Expand Down

0 comments on commit d6b0ae8

Please sign in to comment.