Skip to content

Commit

Permalink
Нерабочее состояние, работа в прогрессе
Browse files Browse the repository at this point in the history
  • Loading branch information
codEnjoyer committed Nov 18, 2023
1 parent cf1ef36 commit 2ba21e0
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""меняет в модели уровня name на title
Revision ID: da06417476fd
Revises: 29ed19cdffbf
Create Date: 2023-11-19 00:24:59.794292
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'da06417476fd'
down_revision: Union[str, None] = '29ed19cdffbf'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('levels', sa.Column('title', sa.String(length=255), nullable=False))
op.alter_column('levels', 'module_id',
existing_type=sa.UUID(),
nullable=True)
op.drop_column('levels', 'name')
op.alter_column('modules', 'map_id',
existing_type=sa.UUID(),
nullable=True)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('modules', 'map_id',
existing_type=sa.UUID(),
nullable=False)
op.add_column('levels', sa.Column('name', sa.VARCHAR(length=255), autoincrement=False, nullable=False))
op.alter_column('levels', 'module_id',
existing_type=sa.UUID(),
nullable=False)
op.drop_column('levels', 'title')
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""меняет в модели теории theme на title
Revision ID: 5bbffa09568b
Revises: da06417476fd
Create Date: 2023-11-19 00:49:49.197801
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '5bbffa09568b'
down_revision: Union[str, None] = 'da06417476fd'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('theory_blocks', sa.Column('title', sa.String(), nullable=False))
op.drop_column('theory_blocks', 'theme')
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('theory_blocks', sa.Column('theme', sa.VARCHAR(), autoincrement=False, nullable=False))
op.drop_column('theory_blocks', 'title')
# ### end Alembic commands ###
5 changes: 5 additions & 0 deletions backend/src/game/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from repository.level_repository import LevelRepository
from repository.map_repository import MapRepository
from repository.models_repository import ModuleRepository
from services.level_service import LevelService
from services.map_service import MapService
from services.module_service import ModuleService

Expand All @@ -11,5 +13,8 @@ def map_service() -> MapService:
def module_service() -> ModuleService:
return ModuleService(ModuleRepository)


def level_service() -> LevelService:
return LevelService(LevelRepository)
# def users_service():
# return UsersService(UsersRepository)
21 changes: 17 additions & 4 deletions backend/src/game/levels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

from sqlalchemy import String, Boolean, UUID, ForeignKey, Enum
from sqlalchemy.dialects import postgresql
from sqlalchemy.ext.associationproxy import AssociationProxy, association_proxy
from sqlalchemy.orm import Mapped, mapped_column, relationship

from database import BaseModel
from game.levels.schemas import LevelRead

if typing.TYPE_CHECKING:
from game.modules.models import Module
Expand All @@ -24,12 +26,23 @@ class Level(BaseModel):
__tablename__ = 'levels'

id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4)
module_id: Mapped[uuid.UUID] = mapped_column(UUID, ForeignKey('modules.id'), nullable=False)
name: Mapped[str] = mapped_column(String(length=255), nullable=False)
module_id: Mapped[uuid.UUID] = mapped_column(UUID, ForeignKey('modules.id'), nullable=True)
title: Mapped[str] = mapped_column(String(length=255), nullable=False)

module: Mapped["Module"] = relationship(back_populates='levels')
# theory: Mapped[list["TheoryUnit"]] = relationship(secondary="level_theory_blocks", back_populates='level')
# tasks: Mapped[list["TaskUnit"]] = relationship(secondary="level_tasks", back_populates='level')

theory_units: Mapped[list["TheoryUnit"]] = relationship(
secondary="level_theory_blocks", back_populates='level', lazy='selectin')

task_units: Mapped[list["TaskUnit"]] = relationship(
secondary="level_tasks", back_populates='level', lazy='selectin')

def to_read_schema(self) -> LevelRead:
return LevelRead(id=self.id,
module_id=self.module_id,
title=self.title,
theory_units_ids=[unit.to_read_schema() for unit in self.theory_units],
task_units_ids=[unit.to_read_schema() for unit in self.task_units])


class EmployeesLevel(BaseModel):
Expand Down
25 changes: 15 additions & 10 deletions backend/src/game/levels/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@
from fastapi import APIRouter

from game.levels.schemas import LevelRead, LevelCreate, LevelUpdate
from utils.types import LevelServiceType

router = APIRouter(prefix="/levels", tags=["Levels"])


@router.get("/")
async def root() -> list[LevelRead]:
return []
async def root(level_service: LevelServiceType) -> list[LevelRead]:
return await level_service.get_all()


@router.get("/{id}")
async def get_level(id: UUID) -> LevelRead:
return LevelRead(id=id, is_accomplished=False)
async def get_level(id: UUID,
level_service: LevelServiceType) -> LevelRead:
return await level_service.get_one(id)


@router.post("/")
async def post_level(level_create: LevelCreate) -> LevelRead:
return LevelRead()
async def post_level(level_create: LevelCreate,
level_service: LevelServiceType) -> LevelRead:
return await level_service.create_one(level_create)


@router.delete("/{id}")
async def delete_level(id: UUID) -> LevelRead:
return LevelRead(id=id, is_accomplished=False)
async def delete_level(id: UUID,
level_service: LevelServiceType) -> LevelRead:
return await level_service.delete_one(id)


@router.patch("/{id}")
async def update_level(id: UUID,
level_update: LevelUpdate) -> LevelRead:
return LevelRead(id=id, is_accomplished=True)
level_update: LevelUpdate,
level_service: LevelServiceType) -> LevelRead:
return await level_service.update_one(id, level_update)
14 changes: 7 additions & 7 deletions backend/src/game/levels/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

from pydantic import BaseModel, ConfigDict

from game.units.tasks.schemas import TaskUnitRead
from game.units.theory.schemas import TheoryUnitRead


class __LevelBase(BaseModel):
module_id: UUID
title: str
theory_units_id: list[UUID] | None
task_units_id: list[UUID] | None

model_config = ConfigDict(from_attributes=True)


class LevelRead(__LevelBase):
id: UUID
theory_units_id: list[UUID]
task_units_id: list[UUID]
theory_units: list[TheoryUnitRead]
task_units: list[TaskUnitRead]


class LevelCreate(__LevelBase):
pass
theory_units: list[TheoryUnitRead] | None
task_units: list[TaskUnitRead] | None


class LevelUpdate(__LevelBase):
module_id: UUID | None = None
title: str | None = None
theory_units_id: list[UUID] | None = None
task_units_id: list[UUID] | None = None
12 changes: 6 additions & 6 deletions backend/src/game/modules/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

from database import BaseModel
from game.modules.schemas import ModuleRead
from game.levels import Level
from game.map import Map

if typing.TYPE_CHECKING:
from game.levels import Level
from game.map import Map


class Module(BaseModel):
__tablename__ = 'modules'

id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4)
title: Mapped[str] = mapped_column(String(length=255), nullable=False)
map_id: Mapped[uuid.UUID] = mapped_column(UUID, ForeignKey('maps.id'))
map_id: Mapped[uuid.UUID] = mapped_column(UUID, ForeignKey('maps.id'), nullable=True)
previous_module_id: Mapped[uuid.UUID] = mapped_column(UUID, ForeignKey('modules.id'), nullable=True)
next_module_id: Mapped[uuid.UUID] = mapped_column(UUID, ForeignKey('modules.id'), nullable=True)

map: Mapped["Map"] = relationship(back_populates='modules', lazy='selectin')

levels: Mapped[list["Level"]] = relationship(back_populates='module', lazy='selectin')
levels_ids: AssociationProxy[list[uuid.UUID]] = association_proxy('levels', 'id')

# next_module: Mapped["Module"] = relationship(back_populates='previous_module')
# previous_module: Mapped["Module"] = relationship(back_populates='next_module')

def to_read_schema(self) -> ModuleRead:
return ModuleRead(id=self.id,
title=self.title,
Expand Down
1 change: 1 addition & 0 deletions backend/src/game/units/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .models import TaskUnit, TaskTypes, TaskStates, EmployeesTask
20 changes: 14 additions & 6 deletions backend/src/game/units/tasks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
import enum
from typing import TYPE_CHECKING

from sqlalchemy import UUID, Integer, Enum, ForeignKey, Boolean
from sqlalchemy import UUID, Integer, ForeignKey, Boolean
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import Mapped, mapped_column, relationship

from database import BaseModel
from .schemas import TaskUnitRead

if TYPE_CHECKING:
from game.levels.models import Level
from questions.models import Question


class TaskTypes(enum.Enum):
Test = enum.auto()

Expand All @@ -28,17 +28,25 @@ class TaskUnit(BaseModel):
__tablename__ = 'tasks'

id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4)
type: Mapped[TaskTypes] = mapped_column(postgresql.ENUM(TaskTypes, name='task_types'), nullable=False, default=TaskTypes.Test)
type: Mapped[TaskTypes] = mapped_column(postgresql.ENUM(TaskTypes, name='task_types'), nullable=False,
default=TaskTypes.Test)
requires_review: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
score_reward: Mapped[int] = mapped_column(Integer, nullable=False, default=1)

questions: Mapped[list["Question"]] = relationship()
level: Mapped[list["Level"]] = relationship(secondary="level_tasks")
# questions: Mapped[list["Question"]] = relationship()
level: Mapped[list["Level"]] = relationship(secondary="level_tasks", back_populates='task_units')

def to_read_schema(self) -> TaskUnitRead:
return TaskUnitRead(id=self.id,
type=self.type,
requires_review=self.requires_review,
score_reward=self.score_reward)


class EmployeesTask(BaseModel):
__tablename__ = 'task_employees'

task_id: Mapped[uuid.UUID] = mapped_column(UUID, ForeignKey('tasks.id'), primary_key=True)
employee_id: Mapped[uuid.UUID] = mapped_column(UUID, ForeignKey('employees.id'), primary_key=True)
state: Mapped[TaskStates] = mapped_column(postgresql.ENUM(TaskStates, name='task_states'), nullable=False, default=TaskStates.NotViewed)
state: Mapped[TaskStates] = mapped_column(postgresql.ENUM(TaskStates, name='task_states'), nullable=False,
default=TaskStates.NotViewed)
6 changes: 3 additions & 3 deletions backend/src/game/units/tasks/schemas.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import typing
from uuid import UUID

from pydantic import BaseModel, ConfigDict

from game.units.tasks.models import TaskTypes
from game.units.tasks.questions.schemas import QuestionRead, QuestionCreate, QuestionUpdate
from game.units.tasks import TaskTypes


class __TaskUnitBase(BaseModel):
questions: list[QuestionRead]
# questions: list[QuestionRead]
type: TaskTypes
score_reward: int
requires_review: bool
Expand All @@ -28,4 +29,3 @@ class TaskUnitUpdate(__TaskUnitBase):
type: TaskTypes | None = None
score_reward: int | None = None
requires_review: bool | None = None

10 changes: 8 additions & 2 deletions backend/src/game/units/theory/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sqlalchemy_utils import URLType

from database import BaseModel
from game.units.theory.schemas import TheoryUnitRead

if TYPE_CHECKING:
from game.levels.models import Level
Expand All @@ -15,11 +16,16 @@ class TheoryUnit(BaseModel):
__tablename__ = 'theory_blocks'

id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4)
theme: Mapped[str] = mapped_column(String, nullable=False)
title: Mapped[str] = mapped_column(String, nullable=False)
content: Mapped[str] = mapped_column(Text, nullable=False, default="Текст теории")

# videos: Mapped[list["TheoryVideo"]] = relationship(back_populates="theory_block")
level: Mapped[list["Level"]] = relationship(secondary="level_theory_blocks")
level: Mapped[list["Level"]] = relationship(secondary="level_theory_blocks", back_populates='theory_units')

def to_read_model(self) -> TheoryUnitRead:
return TheoryUnitRead(id=self.id,
title=self.title,
content=self.content)


class TheoryVideo(BaseModel):
Expand Down
6 changes: 6 additions & 0 deletions backend/src/repository/level_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from game.levels import Level
from repository.sqlalchemy_repository import SQLAlchemyRepository


class LevelRepository(SQLAlchemyRepository):
model = Level
32 changes: 32 additions & 0 deletions backend/src/services/level_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import uuid

from game.levels.schemas import LevelRead, LevelCreate, LevelUpdate
from repository.abstract import AbstractRepository


class LevelService:
__level_repo: AbstractRepository

def __init__(self, level_repo: type[AbstractRepository]):
self.__level_repo = level_repo()

async def create_one(self, level_create: LevelCreate) -> LevelRead:
level_dict = level_create.model_dump()
return await self.__level_repo.add_one(level_dict)

async def get_all(self) -> list[LevelRead]:
models = await self.__level_repo.find_all()
return [model.to_read_schema() for model in models]

async def get_one(self, id: uuid.UUID) -> LevelRead:
res = await self.__level_repo.get_one(id)
return res.to_read_schema()

async def delete_one(self, id: uuid.UUID) -> LevelRead:
res = await self.__level_repo.delete_one(id)
return res.to_read_schema()

async def update_one(self, id: uuid.UUID, level_update: LevelUpdate) -> LevelRead:
level_dict = level_update.model_dump()
res = await self.__level_repo.update_one(id, level_dict)
return res.to_read_schema()
Loading

0 comments on commit 2ba21e0

Please sign in to comment.