refactor: mixins for services
This commit is contained in:
@ -1,43 +1,25 @@
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from models import Status
|
||||
from repositories import StatusRepository
|
||||
from schemas.board import UpdateBoardResponse
|
||||
from schemas.status import *
|
||||
from services.mixins import *
|
||||
|
||||
|
||||
class StatusService:
|
||||
class StatusService(
|
||||
ServiceGetAllMixin[Status, StatusSchema],
|
||||
ServiceCreateMixin[Status, CreateStatusRequest, StatusSchema],
|
||||
ServiceUpdateMixin[Status, UpdateStatusRequest],
|
||||
ServiceDeleteMixin[Status],
|
||||
):
|
||||
schema_class = StatusSchema
|
||||
entity_not_found_msg = "Статус не найден"
|
||||
entity_deleted_msg = "Статус успешно удален"
|
||||
entity_updated_msg = "Статус успешно обновлен"
|
||||
entity_created_msg = "Статус успешно создан"
|
||||
|
||||
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)
|
||||
async def is_soft_delete(self, status: StatusSchema) -> bool:
|
||||
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="Статус успешно удален")
|
||||
return is_soft_needed
|
||||
|
||||
Reference in New Issue
Block a user