Skip to content

Commit

Permalink
Улучшает код
Browse files Browse the repository at this point in the history
  • Loading branch information
codEnjoyer committed Nov 20, 2023
1 parent 54f4b0c commit 21f222a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions backend/src/repository/question_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ class QuestionRepository(SQLAlchemyRepository):

async def add_one(self, model: dict[str, typing.Any]) -> Question:
async with async_session_maker() as session:
question_to_add = Question(type=model['type'],
question=model['question'],
task_id=model['task_id'])
possible_answers = [AnswerOption(**answer_option_schema, question_id=question_to_add.id) for
answer_option_schema in model['possible_answers']]
for ap in possible_answers:
question_to_add.possible_answers.append(ap)
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)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/services/question_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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()
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)

Expand Down

0 comments on commit 21f222a

Please sign in to comment.