from typing import Generator from fastapi import Depends, HTTPException, status from sqlalchemy.orm import Session from app.db.session import get_db from app.models.product import Product, Category from app.models.inventory import InventoryItem, Location from app.models.supplier import Supplier, Purchase # Common dependencies def get_product(db: Session = Depends(get_db), product_id: int = None): if product_id: product = db.query(Product).filter(Product.id == product_id).first() if not product: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"Product with ID {product_id} not found" ) return product return None def get_category(db: Session = Depends(get_db), category_id: int = None): if category_id: category = db.query(Category).filter(Category.id == category_id).first() if not category: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"Category with ID {category_id} not found" ) return category return None def get_location(db: Session = Depends(get_db), location_id: int = None): if location_id: location = db.query(Location).filter(Location.id == location_id).first() if not location: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"Location with ID {location_id} not found" ) return location return None def get_supplier(db: Session = Depends(get_db), supplier_id: int = None): if supplier_id: supplier = db.query(Supplier).filter(Supplier.id == supplier_id).first() if not supplier: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"Supplier with ID {supplier_id} not found" ) return supplier return None