feat: deal attributes editing
This commit is contained in:
@ -1,8 +1,15 @@
|
||||
from sqlalchemy import and_
|
||||
from sqlalchemy.orm import joinedload
|
||||
|
||||
from models import Attribute, AttributeLabel, AttributeType
|
||||
from models import (
|
||||
Attribute,
|
||||
AttributeLabel,
|
||||
AttributeType,
|
||||
AttributeValue,
|
||||
module_attribute,
|
||||
)
|
||||
from repositories.mixins import *
|
||||
from schemas.attribute import CreateAttributeSchema, UpdateAttributeSchema
|
||||
from schemas.attribute import CreateAttributeSchema, UpdateAttributeSchema, UpdateDealModuleAttributeSchema
|
||||
from utils.exceptions import ForbiddenException
|
||||
|
||||
|
||||
@ -71,3 +78,75 @@ class AttributeRepository(
|
||||
stmt = select(AttributeType).where(AttributeType.is_deleted.is_(False))
|
||||
result = await self.session.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def get_deal_module_attributes(
|
||||
self, deal_id: int, module_id: int
|
||||
) -> list[tuple[Attribute, AttributeValue, AttributeLabel]]:
|
||||
stmt = (
|
||||
select(Attribute, AttributeValue, AttributeLabel)
|
||||
.outerjoin(
|
||||
AttributeValue,
|
||||
and_(
|
||||
AttributeValue.attribute_id == Attribute.id,
|
||||
AttributeValue.module_id == module_id,
|
||||
AttributeValue.deal_id == deal_id,
|
||||
),
|
||||
)
|
||||
.outerjoin(
|
||||
AttributeLabel,
|
||||
and_(
|
||||
AttributeLabel.attribute_id == Attribute.id,
|
||||
AttributeLabel.module_id == module_id,
|
||||
),
|
||||
)
|
||||
.join(
|
||||
module_attribute,
|
||||
and_(
|
||||
module_attribute.c.attribute_id == Attribute.id,
|
||||
module_attribute.c.module_id == module_id,
|
||||
),
|
||||
)
|
||||
)
|
||||
result = await self.session.execute(stmt)
|
||||
return list(result.all())
|
||||
|
||||
async def _get_deal_attribute_values(
|
||||
self, deal_id: int, module_id: int
|
||||
) -> list[AttributeValue]:
|
||||
stmt = (
|
||||
select(AttributeValue)
|
||||
.join(Attribute, AttributeValue.attribute_id == Attribute.id)
|
||||
.where(
|
||||
AttributeValue.deal_id == deal_id,
|
||||
AttributeValue.module_id == module_id,
|
||||
Attribute.is_deleted.is_(False),
|
||||
)
|
||||
)
|
||||
result = await self.session.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def update_or_create_deal_attribute_values(
|
||||
self,
|
||||
deal_id: int,
|
||||
module_id: int,
|
||||
attributes: list[UpdateDealModuleAttributeSchema],
|
||||
):
|
||||
old_deal_attribute_values: list[AttributeValue] = await self._get_deal_attribute_values(deal_id, module_id)
|
||||
dict_old_attrs: dict[int, AttributeValue] = {obj.attribute_id: obj for obj in old_deal_attribute_values}
|
||||
|
||||
for attribute in attributes:
|
||||
if attribute.attribute_id in dict_old_attrs:
|
||||
# update
|
||||
attribute_value = dict_old_attrs[attribute.attribute_id]
|
||||
attribute_value.value = attribute.value
|
||||
else:
|
||||
# create
|
||||
attribute_value = AttributeValue(
|
||||
attribute_id=attribute.attribute_id,
|
||||
deal_id=deal_id,
|
||||
module_id=module_id,
|
||||
value=attribute.value,
|
||||
)
|
||||
self.session.add(attribute_value)
|
||||
|
||||
await self.session.commit()
|
||||
|
||||
@ -46,7 +46,7 @@ class ModuleRepository(
|
||||
Module.is_deleted.is_(False),
|
||||
or_(Attribute.id.is_(None), Attribute.is_deleted.is_(False)),
|
||||
)
|
||||
.order_by(Attribute.id)
|
||||
.order_by(Module.id, Attribute.id)
|
||||
)
|
||||
|
||||
async def get_with_attributes_as_tuples(
|
||||
|
||||
Reference in New Issue
Block a user