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

81 lines
2.8 KiB
Python

from typing import Optional
from sqlalchemy.orm import Session
from app.application.appeals_repository import AppealsRepository
from app.domain.entities.appeals_entity import AppealEntity
from app.domain.models.appeals import Appeal
class AppealsService:
def __init__(self, db: Session):
self.appeals_repository = AppealsRepository(db)
def get_all(self) -> list[AppealEntity]:
appeals = self.appeals_repository.get_all()
return [
AppealEntity(
id=appeal.id,
message=appeal.message,
created_date=appeal.created_date,
status=appeal.status,
topic_id=appeal.topic_id,
user_id=appeal.user_id,
) for appeal in appeals
]
def get_by_id(self, appeal_id: int) -> Optional[AppealEntity]:
appeal = self.appeals_repository.get_by_id(appeal_id)
if appeal:
return AppealEntity(
id=appeal.id,
message=appeal.message,
created_date=appeal.created_date,
status=appeal.status,
topic_id=appeal.topic_id,
user_id=appeal.user_id,
)
return None
def create(self, appeal: AppealEntity) -> AppealEntity:
appeal_model = Appeal(
message=appeal.message,
created_date=appeal.created_date,
status=appeal.status,
topic_id=appeal.topic_id,
user_id=appeal.user_id,
)
created_appeal = self.appeals_repository.create(appeal_model)
return AppealEntity(
id=created_appeal.id,
message=created_appeal.message,
created_date=created_appeal.created_date,
status=created_appeal.status,
topic_id=created_appeal.topic_id,
user_id=created_appeal.user_id,
)
def update(self, appeal_id: int, appeal: AppealEntity) -> Optional[AppealEntity]:
appeal_model = self.appeals_repository.get_by_id(appeal_id)
if appeal_model:
appeal_model.message = appeal.message
appeal_model.created_date = appeal.created_date
appeal_model.status = appeal.status
appeal_model.topic_id = appeal.topic_id
appeal_model.user_id = appeal.user_id
updated_appeal = self.appeals_repository.update(appeal_model)
return AppealEntity(
id=updated_appeal.id,
message=updated_appeal.message,
created_date=updated_appeal.created_date,
status=updated_appeal.status,
topic_id=updated_appeal.topic_id,
user_id=updated_appeal.user_id,
)
return None
def delete(self, appeal_id: int) -> bool:
return self.appeals_repository.delete(appeal_id) is not None