refactor: entity not found exceptions handler
This commit is contained in:
@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user