diff --git a/backend/src/api/app.py b/backend/src/api/app.py index fe1f1b8..59937d7 100644 --- a/backend/src/api/app.py +++ b/backend/src/api/app.py @@ -76,7 +76,9 @@ def generate_unique_operation_id(route: APIRoute) -> str: ) from src.modules.events.routes import router as router_events # noqa: E402 +from src.modules.sports.routes import router as router_sports # noqa: E402 from src.modules.users.routes import router as router_users # noqa: E402 app.include_router(router_users) app.include_router(router_events) +app.include_router(router_sports) diff --git a/backend/src/modules/sports/__init__.py b/backend/src/modules/sports/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/src/modules/sports/repository.py b/backend/src/modules/sports/repository.py new file mode 100644 index 0000000..658bc55 --- /dev/null +++ b/backend/src/modules/sports/repository.py @@ -0,0 +1,19 @@ +__all__ = ["sports_repository"] + +from src.storages.mongo.sports import Sport + + +# noinspection PyMethodMayBeStatic +class SportsRepository: + async def read_one(self, id: str) -> Sport | None: + return await Sport.get(id) + + async def read_all(self) -> list[Sport] | None: + return await Sport.all().to_list() + + async def create_many(self, events: list[Sport]) -> bool: + res = await Sport.insert_many(events) + return res.acknowledged + + +sports_repository: SportsRepository = SportsRepository() diff --git a/backend/src/modules/sports/routes.py b/backend/src/modules/sports/routes.py new file mode 100644 index 0000000..f00f780 --- /dev/null +++ b/backend/src/modules/sports/routes.py @@ -0,0 +1,37 @@ +from fastapi import APIRouter + +from src.api.exceptions import IncorrectCredentialsException +from src.modules.sports.repository import sports_repository +from src.storages.mongo import Sport + +router = APIRouter( + prefix="/sports", + tags=["Sports"], + responses={ + **IncorrectCredentialsException.responses, + }, +) + + +@router.get("/", responses={200: {"description": "Info about all sports"}}) +async def get_all_sports() -> list[Sport]: + """ + Get info about all sports. + """ + return await sports_repository.read_all() + + +@router.get("/{id}", responses={200: {"description": "Info about sport"}}) +async def get_sport(id: str) -> Sport: + """ + Get info about one sport. + """ + return await sports_repository.read_one(id) + + +@router.post("/", responses={200: {"description": "Create many sports"}}) +async def create_many_sports(sports: list[Sport]) -> bool: + """ + Create multiple sports. + """ + return await sports_repository.create_many(sports) diff --git a/backend/src/storages/mongo/__init__.py b/backend/src/storages/mongo/__init__.py index 2dcdb27..4174609 100644 --- a/backend/src/storages/mongo/__init__.py +++ b/backend/src/storages/mongo/__init__.py @@ -3,9 +3,10 @@ from beanie import Document, View from src.storages.mongo.events import Event +from src.storages.mongo.sports import Sport from src.storages.mongo.users import User document_models = cast( list[type[Document] | type[View] | str], - [User, Event], + [User, Event, Sport], ) diff --git a/backend/src/storages/mongo/sports.py b/backend/src/storages/mongo/sports.py new file mode 100644 index 0000000..75aadf4 --- /dev/null +++ b/backend/src/storages/mongo/sports.py @@ -0,0 +1,16 @@ +__all__ = ["Sport", "SportSchema"] + +from src.pydantic_base import BaseSchema +from src.storages.mongo.__base__ import CustomDocument + + +class SportSchema(BaseSchema): + sport: str + "Название вида спорта" + disciplines: list[str] + "Названия дисциплин" + + +class Sport(SportSchema, CustomDocument): + class Settings: + pass