63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
from typing import Optional
|
||
|
||
from sqlalchemy import Select, select
|
||
from sqlalchemy.orm import joinedload, selectinload
|
||
|
||
from modules.fulfillment_base.models import DealProductService
|
||
from modules.fulfillment_base.models.deal_product import DealProduct
|
||
from modules.fulfillment_base.schemas.deal_product import (
|
||
UpdateDealProductSchema,
|
||
CreateDealProductSchema,
|
||
)
|
||
from repositories.base import BaseRepository
|
||
from repositories.mixins import RepGetAllMixin, RepUpdateMixin
|
||
from utils.exceptions import ObjectNotFoundException
|
||
|
||
|
||
class DealProductRepository(
|
||
BaseRepository,
|
||
RepGetAllMixin[DealProduct],
|
||
RepUpdateMixin[DealProduct, UpdateDealProductSchema],
|
||
):
|
||
entity_class = DealProduct
|
||
|
||
def _process_get_all_stmt_with_args(self, stmt: Select, *args) -> Select:
|
||
deal_id = args[0]
|
||
return (
|
||
stmt.options(
|
||
joinedload(DealProduct.product),
|
||
selectinload(DealProduct.product_services).joinedload(
|
||
DealProductService.service
|
||
),
|
||
)
|
||
.where(DealProduct.deal_id == deal_id)
|
||
.order_by(DealProduct.product_id)
|
||
)
|
||
|
||
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(
|
||
joinedload(DealProduct.product),
|
||
selectinload(DealProduct.product_services).joinedload(
|
||
DealProductService.service
|
||
),
|
||
)
|
||
.where(DealProduct.deal_id == deal_id, DealProduct.product_id == product_id)
|
||
)
|
||
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())
|
||
self.session.add(deal_product)
|
||
await self.session.commit()
|
||
|
||
async def delete(self, obj: DealProduct):
|
||
await self.session.delete(obj)
|
||
await self.session.commit()
|