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

Commit

Permalink
feat: add /search types
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemSBulgakov committed Nov 22, 2024
1 parent 788e240 commit 5ec02c2
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
35 changes: 35 additions & 0 deletions backend/src/modules/events/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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.storages.mongo.events import Event

router = APIRouter(
Expand All @@ -27,3 +29,36 @@ async def create_many_events(events: list[Event]) -> bool:
Create multiple events.
"""
return await events_repository.create_many(events)


class SearchEventsResponse(BaseModel):
filters: Filters
"Заданные фильтры"
sort: Sort
"Заданная сортировка"
pagination: Pagination
"Заданная пагинация"

page: int
"Текущая страница"
pages_total: int
"Всего страниц"

events: list[Event]
"Результат поиска"


@router.post("/search", responses={200: {"description": "Search events"}})
async def search_events(filters: Filters, sort: Sort, pagination: Pagination) -> SearchEventsResponse:
"""
Search events.
"""
events = await events_repository.read_all()
return SearchEventsResponse(
filters=filters,
sort=sort,
pagination=pagination,
page=1,
pages_total=1,
events=events,
)
82 changes: 82 additions & 0 deletions backend/src/modules/events/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import datetime
from enum import StrEnum

from beanie import PydanticObjectId
from pydantic import BaseModel


class DateFilter(BaseModel):
start_date: datetime.datetime | None = None
"Не раньше даты"
end_date: datetime.datetime | None = None
"Не позже даты"


class DisciplineFilter(BaseModel):
sport_id: PydanticObjectId
"Идентификатор вида спорта"
discipline_id: PydanticObjectId | None = None
"Идентификатор дисциплины"


class LocationFilter(BaseModel):
country_id: PydanticObjectId
"Идентификатор страны"
city_id: PydanticObjectId | None = None
"Идентификатор города"


class MinMaxFilter(BaseModel):
min: int | None = None
"Не менее"
max: int | None = None
"Не более"


class Gender(StrEnum):
male = "male"
"Мужской пол"
female = "female"
"Женский пол"


class Filters(BaseModel):
"""Список фильтров, которые применяются через И"""

query: str | None = None
"Текстовый запрос, чтобы фильтровать по любому полю"
date: DateFilter | None = None
"Фильтр по дате"
discipline: list[DisciplineFilter] | None = None
"Фильтр по спортивным дисциплинам (применяется через ИЛИ)"
location: list[LocationFilter] | None = None
"Фильтр по локации (применяется через ИЛИ)"
gender: Gender | None = None
"Фильтр по полу участников"
age: MinMaxFilter | None = None
"Фильтр по возрасту участников"
participant_count: MinMaxFilter | None = None
"Фильтр по количеству участников соревнования"


class Order(StrEnum):
asc = "asc"
"По возрастанию"
desc = "desc"
"По убыванию"


class Sort(BaseModel):
date: Order | None = None
"Сортировка по дате"
age: Order | None = None
"Сортировка по возрасту"
participant_count: Order | None = None
"Сортировка по количеству участников"


class Pagination(BaseModel):
page_size: int
"Количество элементов на странице"
page_no: int
"Номер страницы"

0 comments on commit 5ec02c2

Please sign in to comment.