69 lines
2.5 KiB
Python
69 lines
2.5 KiB
Python
from typing import Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.application.answer_options_repository import AnswerOptionsRepository
|
|
from app.domain.entities.answer_options_entity import AnswerOptionEntity
|
|
from app.domain.models.answer_options import AnswerOption
|
|
|
|
|
|
class AnswerOptionsService:
|
|
def __init__(self, db: Session):
|
|
self.answer_options_repository = AnswerOptionsRepository(db)
|
|
|
|
def get_all(self) -> list[AnswerOptionEntity]:
|
|
options = self.answer_options_repository.get_all()
|
|
return [
|
|
AnswerOptionEntity(
|
|
id=option.id,
|
|
task_id=option.task_id,
|
|
text=option.text,
|
|
is_correct=option.is_correct,
|
|
) for option in options
|
|
]
|
|
|
|
def get_by_id(self, answer_option_id: int) -> Optional[AnswerOptionEntity]:
|
|
option = self.answer_options_repository.get_by_id(answer_option_id)
|
|
if option:
|
|
return AnswerOptionEntity(
|
|
id=option.id,
|
|
task_id=option.task_id,
|
|
text=option.text,
|
|
is_correct=option.is_correct,
|
|
)
|
|
|
|
return None
|
|
|
|
def create(self, answer_option: AnswerOptionEntity) -> AnswerOptionEntity:
|
|
option_model = AnswerOption(
|
|
task_id=answer_option.task_id,
|
|
text=answer_option.text,
|
|
is_correct=answer_option.is_correct,
|
|
)
|
|
created_option = self.answer_options_repository.create(option_model)
|
|
return AnswerOptionEntity(
|
|
id=created_option.id,
|
|
task_id=created_option.task_id,
|
|
text=created_option.text,
|
|
is_correct=created_option.is_correct,
|
|
)
|
|
|
|
def update(self, answer_option_id: int, answer_option: AnswerOptionEntity) -> Optional[AnswerOptionEntity]:
|
|
option_model = self.answer_options_repository.get_by_id(answer_option_id)
|
|
if option_model:
|
|
option_model.task_id = answer_option.task_id
|
|
option_model.text = answer_option.text
|
|
option_model.is_correct = answer_option.is_correct
|
|
updated_option = self.answer_options_repository.update(option_model)
|
|
return AnswerOptionEntity(
|
|
id=updated_option.id,
|
|
task_id=updated_option.task_id,
|
|
text=updated_option.text,
|
|
is_correct=updated_option.is_correct,
|
|
)
|
|
|
|
return None
|
|
|
|
def delete(self, answer_option_id: int) -> bool:
|
|
return self.answer_options_repository.delete(answer_option_id) is not None
|