70 lines
2.5 KiB
Python
70 lines
2.5 KiB
Python
from typing import Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.application.task_files_repository import TaskFilesRepository
|
|
from app.domain.entities.task_files_entity import TaskFileEntity
|
|
from app.domain.models.task_files import TaskFile
|
|
|
|
|
|
class TaskFilesService:
|
|
def __init__(self, db: Session):
|
|
self.task_files_repository = TaskFilesRepository(db)
|
|
|
|
def get_all(self) -> list[TaskFileEntity]:
|
|
task_files = self.task_files_repository.get_all()
|
|
return [
|
|
TaskFileEntity(
|
|
id=task_file.id,
|
|
file_path=task_file.file_path,
|
|
file_title=task_file.file_title,
|
|
task_id=task_file.task_id
|
|
)
|
|
for task_file in task_files
|
|
]
|
|
|
|
def get_by_id(self, task_file_id: int) -> Optional[TaskFileEntity]:
|
|
task_file = self.task_files_repository.get_by_id(task_file_id)
|
|
if task_file:
|
|
return TaskFileEntity(
|
|
id=task_file.id,
|
|
file_path=task_file.file_path,
|
|
file_title=task_file.file_title,
|
|
task_id=task_file.task_id
|
|
)
|
|
|
|
return None
|
|
|
|
def create(self, task_file: TaskFileEntity) -> TaskFileEntity:
|
|
task_file_model = TaskFile(
|
|
file_path=task_file.file_path,
|
|
file_title=task_file.file_title,
|
|
task_id=task_file.task_id
|
|
)
|
|
created_task_file = self.task_files_repository.create(task_file_model)
|
|
return TaskFileEntity(
|
|
id=created_task_file.id,
|
|
file_path=created_task_file.file_path,
|
|
file_title=created_task_file.file_title,
|
|
task_id=created_task_file.task_id
|
|
)
|
|
|
|
def update(self, task_file_id: int, task_file: TaskFileEntity) -> Optional[TaskFileEntity]:
|
|
task_file_model = self.task_files_repository.get_by_id(task_file_id)
|
|
if task_file_model:
|
|
task_file_model.file_path = task_file.file_path
|
|
task_file_model.file_title = task_file.file_title
|
|
task_file_model.task_id = task_file.task_id
|
|
updated_task_file = self.task_files_repository.update(task_file_model)
|
|
return TaskFileEntity(
|
|
id=updated_task_file.id,
|
|
file_path=updated_task_file.file_path,
|
|
file_title=updated_task_file.file_title,
|
|
task_id=updated_task_file.task_id
|
|
)
|
|
|
|
return None
|
|
|
|
def delete(self, task_file_id: int) -> bool:
|
|
return self.task_files_repository.delete(task_file_id) is not None
|