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

57 lines
2.1 KiB
Python

from typing import Optional
from sqlalchemy.orm import Session
from app.application.appeals_topics_repository import AppealsTopicsRepository
from app.domain.entities.appeals_topics_entity import AppealTopicEntity
from app.domain.models.appeals_topics import AppealsTopic
class AppealsTopicService:
def __init__(self, db: Session):
self.appeals_topics_repository = AppealsTopicsRepository(db)
def get_all(self) -> list[AppealTopicEntity]:
appeals_topics = self.appeals_topics_repository.get_all()
return [
AppealTopicEntity(
id=appeals_topic.id,
title=appeals_topic.title,
) for appeals_topic in appeals_topics
]
def get_by_id(self, appeals_topic_id: int) -> Optional[AppealTopicEntity]:
appeals_topic = self.appeals_topics_repository.get_by_id(appeals_topic_id)
if appeals_topic:
return AppealTopicEntity(
id=appeals_topic.id,
title=appeals_topic.title,
)
return None
def create(self, appeals_topic: AppealTopicEntity) -> AppealTopicEntity:
appeals_model = AppealsTopic(
title=appeals_topic.title,
)
created_appeals_topic = self.appeals_topics_repository.create(appeals_model)
return AppealTopicEntity(
id=created_appeals_topic.id,
title=created_appeals_topic.title,
)
def update(self, appeals_topic_id: int, appeals_topic: AppealTopicEntity) -> Optional[AppealTopicEntity]:
appeals_topic_model = self.appeals_topics_repository.get_by_id(appeals_topic_id)
if appeals_topic_model:
appeals_topic_model.title = appeals_topic.title
updated_appeals_topic = self.appeals_topics_repository.update(appeals_topic_model)
return AppealTopicEntity(
id=updated_appeals_topic.id,
title=updated_appeals_topic.title,
)
return None
def delete(self, appeals_topic_id: int) -> bool:
return self.appeals_topics_repository.delete(appeals_topic_id) is not None