69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
from typing import Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.application.lessons_repository import LessonsRepository
|
|
from app.domain.entities.lessons_entity import LessonEntity
|
|
from app.domain.models.lessons import Lesson
|
|
|
|
|
|
class LessonsService:
|
|
def __init__(self, db: Session):
|
|
self.lessons_repository = LessonsRepository(db)
|
|
|
|
def get_all(self) -> list[LessonEntity]:
|
|
lessons = self.lessons_repository.get_all()
|
|
return [
|
|
LessonEntity(
|
|
id=lesson.id,
|
|
title=lesson.title,
|
|
description=lesson.description,
|
|
course_id=lesson.course_id,
|
|
) for lesson in lessons
|
|
]
|
|
|
|
def get_by_id(self, lesson_id: int) -> Optional[LessonEntity]:
|
|
lesson = self.lessons_repository.get_by_id(lesson_id)
|
|
if lesson:
|
|
return LessonEntity(
|
|
id=lesson.id,
|
|
title=lesson.title,
|
|
description=lesson.description,
|
|
course_id=lesson.course_id,
|
|
)
|
|
|
|
return None
|
|
|
|
def create(self, lesson: LessonEntity) -> LessonEntity:
|
|
lesson_model = Lesson(
|
|
title=lesson.title,
|
|
description=lesson.description,
|
|
course_id=lesson.course_id,
|
|
)
|
|
created_lesson = self.lessons_repository.create(lesson_model)
|
|
return LessonEntity(
|
|
id=created_lesson.id,
|
|
title=created_lesson.title,
|
|
description=created_lesson.description,
|
|
course_id=created_lesson.course_id,
|
|
)
|
|
|
|
def update(self, lesson_id: int, lesson: LessonEntity) -> Optional[LessonEntity]:
|
|
lesson_model = self.lessons_repository.get_by_id(lesson_id)
|
|
if lesson_model:
|
|
lesson_model.title = lesson.title
|
|
lesson_model.description = lesson.description
|
|
lesson_model.course_id = lesson.course_id
|
|
updated_lesson = self.lessons_repository.update(lesson_model)
|
|
return LessonEntity(
|
|
id=updated_lesson.id,
|
|
title=updated_lesson.title,
|
|
description=updated_lesson.description,
|
|
course_id=updated_lesson.course_id,
|
|
)
|
|
|
|
return None
|
|
|
|
def delete(self, lesson_id: int) -> bool:
|
|
return self.lessons_repository.delete(lesson_id) is not None
|