-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'game-api-questions-v1' into game-api-v1
- Loading branch information
Showing
13 changed files
with
187 additions
and
40 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...rations/versions/2023_11_20_1248-a2f543f8e4c6_переносит_ответственность_за_правильный_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""переносит ответственность за правильный вариант ответа на вопроса на ответ | ||
Revision ID: a2f543f8e4c6 | ||
Revises: f074fb69a98a | ||
Create Date: 2023-11-20 12:48:48.808979 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = 'a2f543f8e4c6' | ||
down_revision: Union[str, None] = 'f074fb69a98a' | ||
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.drop_column('questions', 'correct_answer_id') | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column('questions', sa.Column('correct_answer_id', sa.UUID(), autoincrement=False, nullable=False)) | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from enum import Enum, auto | ||
from enum import auto, StrEnum | ||
|
||
|
||
class QuestionTypes(Enum): | ||
class QuestionTypes(StrEnum): | ||
SingleChoice = auto() | ||
MultipleChoice = auto() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from uuid import UUID | ||
|
||
from fastapi import APIRouter | ||
|
||
from game.units.tasks.questions.schemas import QuestionRead, QuestionCreate, QuestionUpdate | ||
from utils.types import QuestionServiceType | ||
|
||
router = APIRouter(tags=["Questions"]) | ||
|
||
|
||
@router.get('/questions/', tags=["Dev"]) | ||
async def root(question_service: QuestionServiceType) -> list[QuestionRead]: | ||
return await question_service.get_all() | ||
|
||
|
||
@router.post('/maps/{map_id}/modules/{module_id}/levels/{level_id}/tasks/{task_id}/') | ||
async def post_question_to_task_unit(map_id: UUID, | ||
module_id: UUID, | ||
level_id: UUID, | ||
task_id: UUID, | ||
question_create: QuestionCreate, | ||
question_service: QuestionServiceType) -> QuestionRead: | ||
return await question_service.create_one(task_id, question_create) | ||
|
||
|
||
@router.delete("/maps/{map_id}/modules/{module_id}/levels/{level_id}/tasks/{task_id}/questions/{question_id}/") | ||
async def delete_question_from_task_unit(map_id: UUID, | ||
module_id: UUID, | ||
level_id: UUID, | ||
task_id: UUID, | ||
question_id: UUID, | ||
question_service: QuestionServiceType) -> QuestionRead: | ||
return await question_service.delete_one(question_id) | ||
|
||
|
||
@router.patch("/maps/{map_id}/modules/{module_id}/levels/{level_id}/tasks/{task_id}/questions/{question_id}/") | ||
async def update_question_in_task_unit(map_id: UUID, | ||
module_id: UUID, | ||
level_id: UUID, | ||
task_id: UUID, | ||
question_id: UUID, | ||
question_update: QuestionUpdate, | ||
question_service: QuestionServiceType) -> QuestionRead: | ||
return await question_service.update_one(question_id, question_update) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import typing | ||
import uuid | ||
|
||
from sqlalchemy import insert | ||
|
||
from database import async_session_maker | ||
from game.units.tasks.questions import Question | ||
from game.units.tasks.questions.answers import AnswerOption | ||
from repository.sqlalchemy_repository import SQLAlchemyRepository | ||
|
||
|
||
class QuestionRepository(SQLAlchemyRepository): | ||
model = Question | ||
|
||
async def add_one(self, model: dict[str, typing.Any]) -> Question: | ||
async with async_session_maker() as session: | ||
possible_answers_list = model.pop('possible_answers') | ||
question_to_add = Question(**model) | ||
|
||
possible_answers = [AnswerOption(**answer_option_dict, question_id=question_to_add.id) for | ||
answer_option_dict in possible_answers_list] | ||
for answer_option in possible_answers: | ||
question_to_add.possible_answers.append(answer_option) | ||
|
||
session.add(question_to_add) | ||
await session.commit() | ||
await session.refresh(question_to_add) | ||
return question_to_add |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import uuid | ||
|
||
from game.units.tasks.questions.schemas import QuestionRead, QuestionCreate, QuestionUpdate | ||
from repository.abstract import AbstractRepository | ||
|
||
|
||
class QuestionService: | ||
__question_repo: AbstractRepository | ||
|
||
def __init__(self, map_repo: type[AbstractRepository]): | ||
self.__question_repo = map_repo() | ||
|
||
async def create_one(self, task_id: uuid.UUID, schema_create: QuestionCreate) -> QuestionRead: | ||
schema_dict = schema_create.model_dump(include={'type', 'possible_answers', 'question'}) | ||
schema_dict['task_id'] = task_id | ||
return await self.__question_repo.add_one(schema_dict) | ||
|
||
async def get_all(self) -> list[QuestionRead]: | ||
models = await self.__question_repo.find_all() | ||
return [model.to_read_schema() for model in models] | ||
|
||
async def get_one(self, id: uuid.UUID) -> QuestionRead: | ||
res = await self.__question_repo.get_one(id) | ||
return res.to_read_schema() | ||
|
||
async def delete_one(self, id: uuid.UUID) -> QuestionRead: | ||
res = await self.__question_repo.delete_one(id) | ||
return res.to_read_schema() | ||
|
||
async def update_one(self, id: uuid.UUID, schema_update: QuestionUpdate) -> QuestionRead: | ||
schema_dict = schema_update.model_dump() | ||
res = await self.__question_repo.update_one(id, schema_dict) | ||
return res.to_read_schema() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters