refactor: entity not found exceptions handler
This commit is contained in:
@ -10,6 +10,7 @@ from modules.fulfillment_base.schemas.deal_service import (
|
||||
)
|
||||
from repositories.base import BaseRepository
|
||||
from repositories.mixins import RepGetAllMixin, RepUpdateMixin
|
||||
from utils.exceptions import ObjectNotFoundException
|
||||
|
||||
|
||||
class DealServiceRepository(
|
||||
@ -29,14 +30,18 @@ class DealServiceRepository(
|
||||
.order_by(DealService.service_id)
|
||||
)
|
||||
|
||||
async def get_by_id(self, deal_id: int, service_id: int) -> Optional[DealService]:
|
||||
async def get_by_id(
|
||||
self, deal_id: int, service_id: int, raise_if_not_found: Optional[bool] = True
|
||||
) -> Optional[DealService]:
|
||||
stmt = (
|
||||
select(DealService)
|
||||
.options(joinedload(DealService.service))
|
||||
.where(DealService.deal_id == deal_id, DealService.service_id == service_id)
|
||||
)
|
||||
result = await self.session.execute(stmt)
|
||||
return result.scalar_one_or_none()
|
||||
result = (await self.session.execute(stmt)).scalar_one_or_none()
|
||||
if result is None and raise_if_not_found:
|
||||
raise ObjectNotFoundException("Связь сделки с услугой не найдена")
|
||||
return result
|
||||
|
||||
async def create(self, data: CreateDealServiceSchema):
|
||||
deal_service = DealService(**data.model_dump())
|
||||
|
||||
Reference in New Issue
Block a user