from typing import Optional from sqlalchemy.orm import Session from app.application.steps_repository import StepsRepository from app.domain.entities.steps_entity import StepEntity from app.domain.models.steps import Step class StepsService: def __init__(self, db: Session): self.steps_repository = StepsRepository(db) def get_all(self) -> list[StepEntity]: steps = self.steps_repository.get_all() return [ StepEntity( id=step.id, title=step.title, lesson_id=step.lesson_id, type_id=step.type_id ) for step in steps ] def get_by_id(self, step_id: int) -> Optional[StepEntity]: step = self.steps_repository.get_by_id(step_id) if step: return StepEntity( id=step.id, title=step.title, lesson_id=step.lesson_id, type_id=step.type_id ) return None def create(self, step: StepEntity) -> StepEntity: step_model = Step( title=step.title, lesson_id=step.lesson_id, type_id=step.type_id ) created_step = self.steps_repository.create(step_model) return StepEntity( id=created_step.id, title=created_step.title, lesson_id=created_step.lesson_id, type_id=created_step.type_id ) def update(self, step_id: int, step: StepEntity) -> Optional[StepEntity]: step_model = self.steps_repository.get_by_id(step_id) if step_model: step_model.title = step.title step_model.lesson_id = step.lesson_id step_model.type_id = step.type_id updated_step = self.steps_repository.update(step_model) return StepEntity( id=updated_step.id, title=updated_step.title, lesson_id=updated_step.lesson_id, type_id=updated_step.type_id ) return None def delete(self, step_id: int) -> bool: return self.steps_repository.delete(step_id) is not None