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 by months route
Browse files Browse the repository at this point in the history
  • Loading branch information
dantetemplar committed Nov 23, 2024
1 parent d6b0ae8 commit cfa8ae6
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion backend/src/modules/events/routes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from datetime import datetime

from fastapi import APIRouter
from pydantic import BaseModel

from src.api.exceptions import IncorrectCredentialsException
from src.modules.events.repository import events_repository
from src.modules.events.schemas import Filters, Pagination, Sort
from src.modules.events.schemas import DateFilter, Filters, Pagination, Sort
from src.modules.sports.repository import sports_repository
from src.storages.mongo.events import Event

Expand Down Expand Up @@ -82,6 +84,30 @@ async def count_events(filters: Filters) -> int:
return count


@router.post("/search/count-by-month", responses={200: {"description": "Count events by months"}})
async def count_events_by_month(filters: Filters) -> dict[str, int]:
"""
Count filtered events by months.
"""

counts = {}
current_year = datetime.now().year
for i in range(1, 13):
date_filter = DateFilter()
date_filter.start_date = datetime(current_year, month=i, day=1)
if i == 12:
date_filter.end_date = datetime(current_year + 1, month=1, day=1)
else:
date_filter.end_date = datetime(current_year, month=i + 1, day=1)
filters.date = date_filter
count = await events_repository.read_with_filters(
filters, Sort(), Pagination(page_size=0, page_no=0), count=True
)
counts[f"{current_year}-{i:02d}"] = count

return counts


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

0 comments on commit cfa8ae6

Please sign in to comment.