refactor: entity not found exceptions handler

This commit is contained in:
2025-09-16 16:56:10 +04:00
parent 276626d6f7
commit 98d3026e0d
24 changed files with 56 additions and 49 deletions

View File

@ -8,7 +8,6 @@ class BoardService(
ServiceCrudMixin[Board, BoardSchema, CreateBoardSchema, UpdateBoardSchema]
):
schema_class = BoardSchema
entity_not_found_msg = "Доска не найдена"
entity_deleted_msg = "Доска успешно удалена"
entity_updated_msg = "Доска успешно обновлена"
entity_created_msg = "Доска успешно создана"

View File

@ -13,7 +13,6 @@ class DealService(
ServiceDeleteMixin[Deal],
):
schema_class = DealSchema
entity_not_found_msg = "Сделка не найдена"
entity_deleted_msg = "Сделка успешно удалена"
entity_updated_msg = "Сделка успешно обновлена"
entity_created_msg = "Сделка успешно создана"

View File

@ -1,7 +1,3 @@
from typing import Generic
from fastapi import HTTPException
from repositories.mixins import *
from schemas.base_crud import *
@ -50,15 +46,12 @@ class ServiceUpdateMixin(
Generic[EntityType, UpdateRequestType],
ServiceBaseMixin[RepUpdateMixin | RepGetByIdMixin],
):
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)
@ -67,7 +60,6 @@ class ServiceDeleteMixin(
Generic[EntityType],
ServiceBaseMixin[RepDeleteMixin | RepGetByIdMixin],
):
entity_not_found_msg = "Entity not found"
entity_deleted_msg = "Entity deleted"
async def is_soft_delete(self, entity: EntityType) -> bool:
@ -75,11 +67,7 @@ class ServiceDeleteMixin(
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)

View File

@ -8,7 +8,6 @@ class ProjectService(
ServiceCrudMixin[Project, ProjectSchema, CreateProjectSchema, UpdateProjectSchema]
):
schema_class = ProjectSchema
entity_not_found_msg = "Проект не найден"
entity_deleted_msg = "Проект успешно удален"
entity_updated_msg = "Проект успешно обновлен"
entity_created_msg = "Проект успешно создан"

View File

@ -8,7 +8,6 @@ class StatusService(
ServiceCrudMixin[Status, StatusSchema, CreateStatusRequest, UpdateStatusRequest]
):
schema_class = StatusSchema
entity_not_found_msg = "Статус не найден"
entity_deleted_msg = "Статус успешно удален"
entity_updated_msg = "Статус успешно обновлен"
entity_created_msg = "Статус успешно создан"