from sqlalchemy import select from sqlalchemy.orm import Session from backend.app.models.business import Supplier class SupplierRepository: def list_suppliers(self, session: Session, filters: dict) -> list[Supplier]: stmt = select(Supplier).where(Supplier.deleted == 0) if filters.get("supplier_name"): stmt = stmt.where(Supplier.supplier_name.contains(filters["supplier_name"])) if filters.get("supplier_type"): stmt = stmt.where(Supplier.supplier_type == filters["supplier_type"]) if filters.get("status") is not None: stmt = stmt.where(Supplier.status == filters["status"]) stmt = stmt.order_by(Supplier.id.desc()) return list(session.execute(stmt).scalars()) def get_supplier_by_name_and_type( self, session: Session, supplier_name: str, supplier_type: str, ) -> Supplier | None: stmt = select(Supplier).where( Supplier.supplier_name == supplier_name, Supplier.supplier_type == supplier_type, Supplier.deleted == 0, ) return session.execute(stmt).scalar_one_or_none() def create_supplier(self, session: Session, payload: dict) -> Supplier: supplier = Supplier(**payload) session.add(supplier) session.flush() return supplier