skill-forge/API/app/infrastructure/lectures_service.py

75 lines
2.5 KiB
Python

from typing import Optional
from sqlalchemy.orm import Session
from app.application.lectures_repository import LecturesRepository
from app.domain.entities.lectures_entity import LectureEntity
from app.domain.models.lectures import Lecture
class LecturesService:
def __init__(self, db: Session):
self.lectures_repository = LecturesRepository(db)
def get_all(self) -> list[LectureEntity]:
lectures = self.lectures_repository.get_all()
return [
LectureEntity(
id=lecture.id,
text=lecture.text,
image=lecture.image,
number=lecture.number,
step_id=lecture.step_id
) for lecture in lectures
]
def get_by_id(self, lecture_id: int) -> Optional[LectureEntity]:
lecture = self.lectures_repository.get_by_id(lecture_id)
if lecture:
return LectureEntity(
id=lecture.id,
text=lecture.text,
image=lecture.image,
number=lecture.number,
step_id=lecture.step_id
)
return None
def create(self, lecture: LectureEntity) -> LectureEntity:
lecture_model = Lecture(
text=lecture.text,
image=lecture.image,
number=lecture.number,
step_id=lecture.step_id
)
created_lecture = self.lectures_repository.create(lecture_model)
return LectureEntity(
id=created_lecture.id,
text=created_lecture.text,
image=created_lecture.image,
number=created_lecture.number,
step_id=created_lecture.step_id
)
def update(self, lecture_id: int, lecture: LectureEntity) -> Optional[LectureEntity]:
lecture_model = self.lectures_repository.get_by_id(lecture_id)
if lecture_model:
lecture_model.text = lecture.text
lecture_model.image = lecture.image
lecture_model.number = lecture.number
lecture_model.step_id = lecture.step_id
updated_lecture = self.lectures_repository.update(lecture_model)
return LectureEntity(
id=updated_lecture.id,
text=updated_lecture.text,
image=updated_lecture.image,
number=updated_lecture.number,
step_id=updated_lecture.step_id
)
return None
def delete(self, lecture_id: int) -> bool:
return self.lectures_repository.delete(lecture_id) is not None