refactor: entity not found exceptions handler
This commit is contained in:
@ -12,6 +12,7 @@ from modules.fulfillment_base.schemas.deal_product import (
|
||||
)
|
||||
from repositories.base import BaseRepository
|
||||
from repositories.mixins import RepGetAllMixin, RepUpdateMixin
|
||||
from utils.exceptions import ObjectNotFoundException
|
||||
|
||||
|
||||
class DealProductRepository(
|
||||
@ -34,7 +35,9 @@ class DealProductRepository(
|
||||
.order_by(DealProduct.product_id)
|
||||
)
|
||||
|
||||
async def get_by_id(self, deal_id: int, product_id: int) -> Optional[DealProduct]:
|
||||
async def get_by_id(
|
||||
self, deal_id: int, product_id: int, raise_if_not_found: Optional[bool] = True
|
||||
) -> Optional[DealProduct]:
|
||||
stmt = (
|
||||
select(DealProduct)
|
||||
.options(
|
||||
@ -45,8 +48,10 @@ class DealProductRepository(
|
||||
)
|
||||
.where(DealProduct.deal_id == deal_id, DealProduct.product_id == product_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: CreateDealProductSchema):
|
||||
deal_product = DealProduct(**data.model_dump())
|
||||
|
||||
@ -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())
|
||||
|
||||
@ -16,6 +16,7 @@ class ProductRepository(
|
||||
RepGetByIdMixin[Product],
|
||||
):
|
||||
entity_class = Product
|
||||
entity_not_found_msg = "Товар не найден"
|
||||
|
||||
def _process_get_all_stmt_with_args(self, stmt: Select, *args) -> Select:
|
||||
search_input = args[0]
|
||||
|
||||
@ -8,6 +8,7 @@ from modules.fulfillment_base.models import DealProductService
|
||||
from modules.fulfillment_base.schemas.product_service import *
|
||||
from repositories.base import BaseRepository
|
||||
from repositories.mixins import RepUpdateMixin
|
||||
from utils.exceptions import ObjectNotFoundException
|
||||
|
||||
|
||||
class ProductServiceRepository(
|
||||
@ -15,10 +16,15 @@ class ProductServiceRepository(
|
||||
RepUpdateMixin[DealProductService, UpdateProductServiceSchema],
|
||||
):
|
||||
entity_class = DealProductService
|
||||
entity_not_found_msg = "Связь услуги с товаром не найдена"
|
||||
session: AsyncSession
|
||||
|
||||
async def get_by_id(
|
||||
self, deal_id: int, product_id: int, service_id: int
|
||||
self,
|
||||
deal_id: int,
|
||||
product_id: int,
|
||||
service_id: int,
|
||||
raise_if_not_found: Optional[bool] = True,
|
||||
) -> Optional[DealProductService]:
|
||||
stmt = (
|
||||
select(DealProductService)
|
||||
@ -31,8 +37,10 @@ class ProductServiceRepository(
|
||||
DealProductService.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: CreateProductServiceSchema):
|
||||
deal_product_service = DealProductService(**data.model_dump())
|
||||
|
||||
@ -15,6 +15,7 @@ class ServiceRepository(
|
||||
RepGetByIdMixin[Service],
|
||||
):
|
||||
entity_class = Service
|
||||
entity_not_found_msg = "Услуга не найдена"
|
||||
|
||||
async def update(self, service: Service, data: UpdateServiceSchema) -> Service:
|
||||
return await self._apply_update_data_to_model(service, data, True)
|
||||
|
||||
@ -12,6 +12,7 @@ class ServicesKitRepository(
|
||||
RepCrudMixin[ServicesKit, CreateServicesKitSchema, UpdateServicesKitSchema],
|
||||
):
|
||||
entity_class = ServicesKit
|
||||
entity_not_found_msg = "Набор услуг не найден"
|
||||
|
||||
def _process_get_by_id_stmt(self, stmt: Select) -> Select:
|
||||
return stmt.options(selectinload(ServicesKit.services))
|
||||
|
||||
Reference in New Issue
Block a user