from typing import Any, List from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy.orm import Session from app import crud, models, schemas from app.api import deps router = APIRouter() @router.get("/", response_model=List[schemas.Supplier]) def read_suppliers( db: Session = Depends(deps.get_db), skip: int = 0, limit: int = 100, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Retrieve suppliers. """ suppliers = crud.supplier.get_multi(db, skip=skip, limit=limit) return suppliers @router.post("/", response_model=schemas.Supplier) def create_supplier( *, db: Session = Depends(deps.get_db), supplier_in: schemas.SupplierCreate, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Create new supplier. """ supplier = crud.supplier.get_by_name(db, name=supplier_in.name) if supplier: raise HTTPException( status_code=400, detail="A supplier with this name already exists in the system.", ) if supplier_in.email: supplier = crud.supplier.get_by_email(db, email=supplier_in.email) if supplier: raise HTTPException( status_code=400, detail="A supplier with this email already exists in the system.", ) supplier = crud.supplier.create(db, obj_in=supplier_in) return supplier @router.get("/{supplier_id}", response_model=schemas.Supplier) def read_supplier( *, db: Session = Depends(deps.get_db), supplier_id: str, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Get supplier by ID. """ supplier = crud.supplier.get(db, id=supplier_id) if not supplier: raise HTTPException(status_code=404, detail="Supplier not found") return supplier @router.put("/{supplier_id}", response_model=schemas.Supplier) def update_supplier( *, db: Session = Depends(deps.get_db), supplier_id: str, supplier_in: schemas.SupplierUpdate, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Update a supplier. """ supplier = crud.supplier.get(db, id=supplier_id) if not supplier: raise HTTPException(status_code=404, detail="Supplier not found") # If name is being updated, check for duplicates if supplier_in.name and supplier_in.name != supplier.name: existing_supplier = crud.supplier.get_by_name(db, name=supplier_in.name) if existing_supplier: raise HTTPException( status_code=400, detail="A supplier with this name already exists in the system.", ) # If email is being updated, check for duplicates if supplier_in.email and supplier_in.email != supplier.email: existing_supplier = crud.supplier.get_by_email(db, email=supplier_in.email) if existing_supplier: raise HTTPException( status_code=400, detail="A supplier with this email already exists in the system.", ) supplier = crud.supplier.update(db, db_obj=supplier, obj_in=supplier_in) return supplier @router.delete("/{supplier_id}", response_model=schemas.Supplier) def delete_supplier( *, db: Session = Depends(deps.get_db), supplier_id: str, current_user: models.User = Depends(deps.get_current_active_superuser), ) -> Any: """ Delete a supplier. """ supplier = crud.supplier.get(db, id=supplier_id) if not supplier: raise HTTPException(status_code=404, detail="Supplier not found") supplier = crud.supplier.remove(db, id=supplier_id) return supplier @router.get("/search/", response_model=List[schemas.Supplier]) def search_suppliers( *, db: Session = Depends(deps.get_db), query: str = Query(..., min_length=1), skip: int = 0, limit: int = 100, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Search suppliers by name, contact name, or email. """ suppliers = crud.supplier.search(db, query=query, skip=skip, limit=limit) return suppliers