refactor: mixins for services

This commit is contained in:
2025-09-08 10:59:06 +04:00
parent 67634836dc
commit d73748deab
16 changed files with 207 additions and 205 deletions

View File

@ -1,43 +1,23 @@
from fastapi import HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from models import Board
from repositories import BoardRepository
from schemas.board import *
from services.mixins import *
class BoardService:
class BoardService(
ServiceGetAllMixin[Board, BoardSchema],
ServiceCreateMixin[Board, CreateBoardRequest, BoardSchema],
ServiceUpdateMixin[Board, UpdateBoardRequest],
ServiceDeleteMixin[Board],
):
schema_class = BoardSchema
entity_not_found_msg = "Доска не найдена"
entity_deleted_msg = "Доска успешно удалена"
entity_updated_msg = "Доска успешно обновлена"
entity_created_msg = "Доска успешно создана"
def __init__(self, session: AsyncSession):
self.repository = BoardRepository(session)
async def get_boards(self, project_id: int) -> GetBoardsResponse:
boards = await self.repository.get_all(project_id)
return GetBoardsResponse(
items=[BoardSchema.model_validate(board) for board in boards]
)
async def create_board(self, request: CreateBoardRequest) -> CreateBoardResponse:
board_id = await self.repository.create(request.entity)
board = await self.repository.get_by_id(board_id)
return CreateBoardResponse(
entity=BoardSchema.model_validate(board),
message="Доска успешно создана",
)
async def update_board(
self, board_id: int, request: UpdateBoardRequest
) -> UpdateBoardResponse:
board = await self.repository.get_by_id(board_id)
if not board:
raise HTTPException(status_code=404, detail="Доска не найдена")
await self.repository.update(board, request.entity)
return UpdateBoardResponse(message="Доска успешно обновлена")
async def delete_board(self, board_id: int) -> DeleteBoardResponse:
board = await self.repository.get_by_id(board_id)
if not board:
raise HTTPException(status_code=404, detail="Доска не найдена")
is_soft_needed: bool = len(board.deals) > 0
await self.repository.delete(board, is_soft_needed)
return DeleteBoardResponse(message="Доска успешно удалена")
async def is_soft_delete(self, board: BoardSchema) -> bool:
return len(board.deals) > 0