API_logistics/app/core/usecases/status_service.py
2024-10-05 13:57:01 +05:00

60 lines
2.1 KiB
Python

from typing import List, Optional
from sqlalchemy.orm import Session
from app.core.entities.status import StatusEntity
from app.infrastructure.database.models.statuses import Status
from app.infrastructure.database.repository.status_repository import StatusesRepository
class StatusesService:
def __init__(self, db: Session):
self.repository = StatusesRepository(db)
def get_all_statuses(self) -> List[StatusEntity]:
statuses = self.repository.get_all()
return [
StatusEntity(
id=status.id,
name=status.name,
orders=[order.id for order in status.total_orders] if status.total_orders else [],
)
for status in statuses
]
def get_status_by_id(self, status_id: int) -> Optional[StatusEntity]:
status = self.repository.get_by_id(status_id)
if status:
return StatusEntity(
id=status.id,
name=status.name,
orders=[order.id for order in status.total_orders] if status.total_orders else [],
)
return None
def create_status(self, entity: StatusEntity) -> StatusEntity:
status_model = Status(
name=entity.name
)
created_status = self.repository.create(status_model)
return StatusEntity(
id=created_status.id,
name=created_status.name,
orders=[],
)
def update_status(self, status_id: int, entity: StatusEntity) -> Optional[StatusEntity]:
status_model = self.repository.get_by_id(status_id)
if status_model:
status_model.name = entity.name
self.repository.update(status_model)
return StatusEntity(
id=status_model.id,
name=status_model.name,
orders=[order.id for order in status_model.total_orders] if status_model.total_orders else [],
)
return None
def delete_status(self, status_id: int) -> bool:
return self.repository.delete(status_id) is not None