Skip to content

Commit

Permalink
Исправлен баг с получением модулей конкретной карты
Browse files Browse the repository at this point in the history
  • Loading branch information
codEnjoyer committed Dec 3, 2023
1 parent ef5691a commit 9b288ef
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion backend/src/game/modules/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def root(module_service: ModuleServiceType) -> list[ModuleRead]:
@router.get("/maps/{map_id}/modules/")
async def get_map_modules(map_id: UUID,
module_service: ModuleServiceType) -> list[ModuleRead]:
return await module_service.get_all()
return await module_service.get_all_linked_to_map(map_id)


@router.get("/maps/{map_id}/modules/{module_id}/")
Expand Down
9 changes: 9 additions & 0 deletions backend/src/repository/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import uuid
from abc import ABC, abstractmethod

from sqlalchemy import ColumnElement
from sqlalchemy.sql._typing import _HasClauseElement
from sqlalchemy.sql.elements import SQLCoreOperations
from sqlalchemy.sql.roles import ExpressionElementRole


class AbstractRepository(ABC):
@abstractmethod
Expand All @@ -12,6 +17,10 @@ async def add_one(self, model: dict[str, typing.Any]):
async def find_all(self):
pass

@abstractmethod
async def find_all_with_condition(self, *whereclause: ColumnElement[bool]):
pass

@abstractmethod
async def get_one(self, id: uuid.UUID):
pass
Expand Down
24 changes: 20 additions & 4 deletions backend/src/repository/sqlalchemy_repository.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import typing
import uuid

from sqlalchemy import insert, select, delete, update
from sqlalchemy import insert, select, delete, update, ColumnElement, LambdaElement
from sqlalchemy.sql._typing import _HasClauseElement
from sqlalchemy.sql.elements import SQLCoreOperations
from sqlalchemy.sql.roles import ExpressionElementRole

from database import async_session_maker
from repository.abstract import AbstractRepository
Expand All @@ -12,7 +15,9 @@ class SQLAlchemyRepository(AbstractRepository):

async def add_one(self, model: dict[str, typing.Any]) -> model:
async with async_session_maker() as session:
stmt = insert(self.model).values(**model).returning(self.model)
stmt = insert(self.model) \
.values(**model) \
.returning(self.model)
res = await session.execute(stmt)
await session.commit()
return res.scalar_one()
Expand All @@ -23,6 +28,12 @@ async def find_all(self) -> list[model]:
res = await session.execute(stmt)
return res.scalars()

async def find_all_with_condition(self, *whereclause: ColumnElement[bool]) -> list[model]:
async with async_session_maker() as session:
stmt = select(self.model).where(*whereclause)
res = await session.execute(stmt)
return res.scalars()

async def get_one(self, id: uuid.UUID) -> model:
async with async_session_maker() as session:
stmt = select(self.model).where(self.model.id == id)
Expand All @@ -31,15 +42,20 @@ async def get_one(self, id: uuid.UUID) -> model:

async def delete_one(self, id: uuid.UUID) -> model:
async with async_session_maker() as session:
stmt = delete(self.model).where(self.model.id == id).returning(self.model)
stmt = delete(self.model) \
.where(self.model.id == id) \
.returning(self.model)
res = await session.execute(stmt)
await session.commit()
return res.scalar_one()

async def update_one(self, id: uuid.UUID, model: dict[str, typing.Any]) -> model:
model = {key: value for key, value in model.items() if value}
async with async_session_maker() as session:
stmt = update(self.model).where(self.model.id == id).values(**model).returning(self.model)
stmt = update(self.model) \
.where(self.model.id == id) \
.values(**model) \
.returning(self.model)
res = await session.execute(stmt)
await session.commit()
return res.scalar_one()
5 changes: 5 additions & 0 deletions backend/src/services/game/module_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import uuid

from game.modules import Module
from game.modules.schemas import ModuleRead, ModuleCreate, ModuleUpdate
from repository.abstract import AbstractRepository

Expand Down Expand Up @@ -30,3 +31,7 @@ async def update_one(self, id: uuid.UUID, schema_update: ModuleUpdate) -> Module
schema_dict = schema_update.model_dump()
res = await self.__module_repo.update_one(id, schema_dict)
return res.to_read_schema()

async def get_all_linked_to_map(self, map_id: uuid.UUID) -> list[ModuleRead]:
res = await self.__module_repo.find_all_with_condition(Module.map_id == map_id)
return [model.to_read_schema() for model in res]
6 changes: 3 additions & 3 deletions backend/src/users/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ class UserCreate(BaseUserCreate, __CustomUser):
pass


class UserUpdate(BaseUserUpdate, __CustomUser):
username: str | None = None
role: UserRoles | None = None
class UserUpdate(UserCreate):
email: EmailStr | None
role: UserRoles | None

0 comments on commit 9b288ef

Please sign in to comment.