72 lines
2.5 KiB
Python
72 lines
2.5 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.models.service import ServicesKit
|
|
from modules.fulfillment_base.schemas.deal_product import (
|
|
UpdateDealProductSchema,
|
|
CreateDealProductSchema,
|
|
)
|
|
from repositories.base import BaseRepository
|
|
from repositories.mixins import RepGetAllMixin, RepUpdateMixin
|
|
|
|
|
|
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) -> 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)
|
|
return result.scalar_one_or_none()
|
|
|
|
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()
|
|
|
|
async def add_services_kit(
|
|
self, deal_product: DealProduct, services_kit: ServicesKit
|
|
):
|
|
for service in services_kit.services:
|
|
deal_product_service = DealProductService(
|
|
deal_id=deal_product.deal_id,
|
|
product_id=deal_product.product_id,
|
|
service_id=service.id,
|
|
price=service.price,
|
|
)
|
|
self.session.add(deal_product_service)
|
|
await self.session.commit()
|