| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- # app/services/product_routing_service.py
- from typing import List
- from sqlalchemy.ext.asyncio import AsyncSession
- from sqlalchemy import select, delete
- from app.core.biz_exception import NotFoundError
- from app.models.product_routing import VasProductRouting
- from app.schemas.product_routing import VasProductRoutingCreate, VasProductRoutingUpdate
- class ProductRoutingService:
- @staticmethod
- async def create(
- db: AsyncSession,
- data: VasProductRoutingCreate,
- ) -> VasProductRouting:
- rec = VasProductRouting(**data.model_dump(exclude_unset=True, by_alias=True))
- db.add(rec)
- await db.commit()
- await db.refresh(rec)
- return rec
- @staticmethod
- async def update(
- db: AsyncSession,
- id: int,
- data: VasProductRoutingUpdate,
- ) -> VasProductRouting:
- stmt = select(VasProductRouting).where(
- VasProductRouting.id == id
- )
- obj = (await db.execute(stmt)).scalar_one_or_none()
- if not obj:
- raise NotFoundError("Product routing not exist")
- update_data = data.model_dump(exclude_unset=True, by_alias=True)
- for k, v in update_data.items():
- setattr(obj, k, v)
- await db.commit()
- await db.refresh(obj)
- return obj
- @staticmethod
- async def list_by_product(
- db: AsyncSession,
- product_id: int,
- ) -> List[VasProductRouting]:
- stmt = select(VasProductRouting).where(
- VasProductRouting.product_id == product_id
- )
- result = await db.execute(stmt)
- return result.scalars().all()
- @staticmethod
- async def delete(
- db: AsyncSession,
- id: int,
- ) -> None:
- stmt = select(VasProductRouting).where(
- VasProductRouting.id == id
- )
- obj = (await db.execute(stmt)).scalar_one_or_none()
- if not obj:
- raise NotFoundError("Product routing not exist")
- await db.delete(obj)
- await db.commit()
|