refactor: mixins for services
This commit is contained in:
@ -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
|
||||
|
||||
@ -1,18 +1,27 @@
|
||||
import math
|
||||
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from models import Deal
|
||||
from repositories import DealRepository
|
||||
from schemas.base import PaginationSchema, SortingSchema
|
||||
from schemas.deal import *
|
||||
from services.mixins import *
|
||||
|
||||
|
||||
class DealService:
|
||||
class DealService(
|
||||
ServiceCreateMixin[Deal, CreateDealRequest, DealSchema],
|
||||
ServiceUpdateMixin[Deal, UpdateDealRequest],
|
||||
ServiceDeleteMixin[Deal],
|
||||
):
|
||||
schema_class = DealSchema
|
||||
entity_not_found_msg = "Сделка не найдена"
|
||||
entity_deleted_msg = "Сделка успешно удалена"
|
||||
entity_updated_msg = "Сделка успешно обновлена"
|
||||
entity_created_msg = "Сделка успешно создана"
|
||||
|
||||
def __init__(self, session: AsyncSession):
|
||||
self.repository = DealRepository(session)
|
||||
|
||||
async def get_deals(
|
||||
async def get_all(
|
||||
self,
|
||||
pagination: PaginationSchema,
|
||||
sorting: SortingSchema,
|
||||
@ -36,27 +45,3 @@ class DealService:
|
||||
total_pages=total_pages, total_items=total_items
|
||||
),
|
||||
)
|
||||
|
||||
async def create_deal(self, request: CreateDealRequest) -> CreateDealResponse:
|
||||
deal_id = await self.repository.create(request.entity)
|
||||
deal = await self.repository.get_by_id(deal_id)
|
||||
return CreateDealResponse(
|
||||
entity=DealSchema.model_validate(deal),
|
||||
message="Сделка успешно создана",
|
||||
)
|
||||
|
||||
async def update_deal(self, deal_id: int, request: UpdateDealRequest):
|
||||
deal = await self.repository.get_by_id(deal_id)
|
||||
if not deal:
|
||||
raise HTTPException(status_code=404, detail="Сделка не найдена")
|
||||
|
||||
await self.repository.update(deal, request.entity)
|
||||
return UpdateDealResponse(message="Сделка успешно обновлена")
|
||||
|
||||
async def delete_deal(self, deal_id: int) -> DeleteDealResponse:
|
||||
deal = await self.repository.get_by_id(deal_id)
|
||||
if not deal:
|
||||
raise HTTPException(status_code=404, detail="Сделка не найдена")
|
||||
|
||||
await self.repository.delete(deal, True)
|
||||
return DeleteDealResponse(message="Сделка успешно удалена")
|
||||
84
services/mixins.py
Normal file
84
services/mixins.py
Normal file
@ -0,0 +1,84 @@
|
||||
from typing import Generic
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
from repositories.mixins import *
|
||||
from schemas.base_crud import *
|
||||
|
||||
RepositoryMixin = TypeVar("RepositoryMixin")
|
||||
|
||||
EntityType = TypeVar("EntityType")
|
||||
SchemaType = TypeVar("SchemaType", bound=BaseSchema)
|
||||
UpdateRequestType = TypeVar("UpdateRequestType", bound=BaseSchema)
|
||||
CreateRequestType = TypeVar("CreateRequestType", bound=BaseSchema)
|
||||
|
||||
|
||||
class ServiceBaseMixin(Generic[RepositoryMixin]):
|
||||
repository: RepositoryMixin
|
||||
|
||||
|
||||
class ServiceCreateMixin(
|
||||
Generic[EntityType, CreateRequestType, SchemaType],
|
||||
ServiceBaseMixin[RepCreateMixin | RepGetByIdMixin],
|
||||
):
|
||||
entity_created_msg = "Entity created"
|
||||
schema_class: type[SchemaType]
|
||||
|
||||
async def create(self, request: CreateRequestType) -> BaseCreateResponse:
|
||||
entity_id = await self.repository.create(request.entity)
|
||||
entity = await self.repository.get_by_id(entity_id)
|
||||
return BaseCreateResponse(
|
||||
entity=self.schema_class.model_validate(entity),
|
||||
message=self.entity_created_msg,
|
||||
)
|
||||
|
||||
|
||||
class ServiceGetAllMixin(
|
||||
ServiceBaseMixin[RepGetAllMixin],
|
||||
Generic[EntityType, SchemaType],
|
||||
):
|
||||
schema_class: type[SchemaType]
|
||||
|
||||
async def get_all(self, *args) -> BaseGetAllResponse[SchemaType]:
|
||||
entities = await self.repository.get_all(*args)
|
||||
return BaseGetAllResponse[SchemaType](
|
||||
items=[self.schema_class.model_validate(entity) for entity in entities]
|
||||
)
|
||||
|
||||
|
||||
class ServiceUpdateMixin(
|
||||
ServiceBaseMixin[RepUpdateMixin | RepGetByIdMixin],
|
||||
Generic[EntityType, UpdateRequestType],
|
||||
):
|
||||
entity_not_found_msg = "Entity not found"
|
||||
entity_updated_msg = "Entity updated"
|
||||
|
||||
async def update(
|
||||
self, entity_id: int, request: UpdateRequestType
|
||||
) -> BaseUpdateResponse:
|
||||
entity = await self.repository.get_by_id(entity_id)
|
||||
if not entity:
|
||||
raise HTTPException(status_code=404, detail=self.entity_not_found_msg)
|
||||
await self.repository.update(entity, request.entity)
|
||||
return BaseUpdateResponse(message=self.entity_updated_msg)
|
||||
|
||||
|
||||
class ServiceDeleteMixin(
|
||||
ServiceBaseMixin[RepDeleteMixin | RepGetByIdMixin],
|
||||
Generic[EntityType],
|
||||
):
|
||||
entity_not_found_msg = "Entity not found"
|
||||
entity_deleted_msg = "Entity deleted"
|
||||
|
||||
async def is_soft_delete(self, entity: EntityType) -> bool:
|
||||
return hasattr(entity, "is_deleted")
|
||||
|
||||
async def delete(self, entity_id: int) -> BaseDeleteResponse:
|
||||
entity = await self.repository.get_by_id(entity_id)
|
||||
if not entity:
|
||||
raise HTTPException(status_code=404, detail=self.entity_not_found_msg)
|
||||
|
||||
is_soft = await self.is_soft_delete(entity)
|
||||
|
||||
await self.repository.delete(entity, is_soft)
|
||||
return BaseDeleteResponse(message=self.entity_deleted_msg)
|
||||
@ -1,45 +1,23 @@
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from models import Project
|
||||
from repositories import ProjectRepository
|
||||
from schemas.project import *
|
||||
from services.mixins import *
|
||||
|
||||
|
||||
class ProjectService:
|
||||
class ProjectService(
|
||||
ServiceGetAllMixin[Project, ProjectSchema],
|
||||
ServiceCreateMixin[Project, CreateProjectRequest, ProjectSchema],
|
||||
ServiceUpdateMixin[Project, UpdateProjectRequest],
|
||||
ServiceDeleteMixin[Project],
|
||||
):
|
||||
schema_class = ProjectSchema
|
||||
entity_not_found_msg = "Проект не найден"
|
||||
entity_deleted_msg = "Проект успешно удален"
|
||||
entity_updated_msg = "Проект успешно обновлен"
|
||||
entity_created_msg = "Проект успешно создан"
|
||||
|
||||
def __init__(self, session: AsyncSession):
|
||||
self.repository = ProjectRepository(session)
|
||||
|
||||
async def get_projects(self) -> GetProjectsResponse:
|
||||
projects = await self.repository.get_all()
|
||||
return GetProjectsResponse(
|
||||
items=[ProjectSchema.model_validate(project) for project in projects]
|
||||
)
|
||||
|
||||
async def create_project(
|
||||
self, request: CreateProjectRequest
|
||||
) -> CreateProjectResponse:
|
||||
project_id = await self.repository.create(request.entity)
|
||||
project = await self.repository.get_by_id(project_id)
|
||||
return CreateProjectResponse(
|
||||
entity=ProjectSchema.model_validate(project),
|
||||
message="Проект успешно создан",
|
||||
)
|
||||
|
||||
async def update_project(
|
||||
self, project_id: int, request: UpdateProjectRequest
|
||||
) -> UpdateProjectResponse:
|
||||
project = await self.repository.get_by_id(project_id)
|
||||
if not project:
|
||||
raise HTTPException(status_code=404, detail="Проект не найден")
|
||||
|
||||
await self.repository.update(project, request.entity)
|
||||
return UpdateProjectResponse(message="Проект успешно обновлен")
|
||||
|
||||
async def delete_project(self, project_id: int) -> DeleteProjectResponse:
|
||||
project = await self.repository.get_by_id(project_id)
|
||||
if not project:
|
||||
raise HTTPException(status_code=404, detail="Проект не найден")
|
||||
|
||||
is_soft_needed: bool = len(project.boards) > 0
|
||||
await self.repository.delete(project, is_soft_needed)
|
||||
return DeleteProjectResponse(message="Проект успешно удален")
|
||||
async def is_soft_delete(self, project: ProjectSchema) -> bool:
|
||||
return len(project.boards) > 0
|
||||
|
||||
@ -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