from fastapi import HTTPException from sqlalchemy.ext.asyncio import AsyncSession from repositories import StatusRepository from schemas.board import UpdateBoardResponse from schemas.status import * class StatusService: def __init__(self, session: AsyncSession): self.repository = StatusRepository(session) async def get_statuses(self, board_id: int) -> GetStatusesResponse: statuses = await self.repository.get_all(board_id) return GetStatusesResponse( items=[StatusSchema.model_validate(status) for status in statuses] ) async def create_status(self, request: CreateStatusRequest) -> CreateStatusResponse: status_id = await self.repository.create(request.entity) status = await self.repository.get_by_id(status_id) return CreateStatusResponse( entity=StatusSchema.model_validate(status), message="Статус успешно создан", ) async def update_status(self, status_id: int, request: UpdateStatusRequest): status = await self.repository.get_by_id(status_id) if not status: raise HTTPException(status_code=404, detail="Статус не найден") await self.repository.update(status, request.entity) return UpdateBoardResponse(message="Статус успешно обновлен") async def delete_status(self, status_id: int) -> DeleteStatusResponse: board = await self.repository.get_by_id(status_id) if not board: raise HTTPException(status_code=404, detail="Статус не найден") deals_count = await self.repository.get_deals_count(status_id) is_soft_needed: bool = deals_count > 0 await self.repository.delete(board, is_soft_needed) return DeleteStatusResponse(message="Статус успешно удален")