async def update_supplier(supplier_id: PositiveInt, supplier: dict, db: Session = Depends(get_db)): db_supplier = crud.get_supplier(db, supplier_id) if db_supplier is None: raise HTTPException(status_code=404, detail="Supplier not found") else: if supplier != {}: crud.update_supplier(db, supplier, supplier_id) db.commit() db_supplier = crud.get_supplier(db, supplier_id) return db_supplier
async def get_suppliers_product(id: int, db: Session = Depends(get_db)): check_id = crud.get_supplier(db, id) if check_id is None: raise HTTPException(status_code=404) else: return crud.get_product(db, id)
async def modify_supplier(id: int, db: Session = Depends(get_db)): deleted_supplier = crud.get_supplier(db, id) if deleted_supplier is None: raise HTTPException(status_code=404) crud.delete_supplier(db, id)
async def get_products_for_supplier(supplier_id: PositiveInt, db: Session = Depends(get_db)): db_supplier = crud.get_supplier(db, supplier_id) if not db_supplier: raise HTTPException(status_code=404, detail="Supplier not found") return crud.get_products_for_supplier(db, supplier_id)
async def update_supplier(supplier_id, new_props: schemas.SupplierUpdate, db: Session = Depends(get_db)): db_supplier = crud.get_supplier(db, supplier_id) if db_supplier is None: raise HTTPException(status_code=404, detail="Supplier not found") return crud.update_supplier(db, supplier_id, new_props)
async def delete_supplier(supplier_id: PositiveInt, db: Session = Depends(get_db)): db_supplier = crud.get_supplier(db, supplier_id) if db_supplier is None: raise HTTPException(status_code=404, detail="Supplier not found") else: crud.delete_supplier(db,supplier_id) db.commit()
async def add_supplier(supplier: schemas.Supplier, db: Session = Depends(get_db)): print(supplier) crud.add_supplier(db, supplier) db.commit() supplier_id = crud.get_last_Supplier(db).SupplierID db_supplier = crud.get_supplier(db, supplier_id) return db_supplier
async def get_products(supplier_id: PositiveInt, db: Session = Depends(get_db)): db_supplier = crud.get_supplier(db, supplier_id) if db_supplier is None: raise HTTPException(status_code=404, detail="Supplier not found") else: products = crud.get_products(db, supplier_id) products = [{"ProductID": product.ProductID, "ProductName": product.ProductName, "Category":{"CategoryID": product.CategoryID, "CategoryName": crud.get_product_category(db,product.CategoryID).CategoryName}, "Discontinued": product.Discontinued} for product in products] return products
async def put_supplier(update_supplier: schemas.UpdateSupplier, supplier_id: PositiveInt, db: Session = Depends(get_db)): updates = {} for k, v in dict(update_supplier).items(): if v: updates[k] = v db.query(models.Supplier).filter(models.Supplier.SupplierID == supplier_id).\ update(updates) db_supplier = crud.get_supplier(db, supplier_id) if db_supplier is None: raise HTTPException(status_code=404, detail="Supplier not found") return db_supplier
async def delete_supplier(supplier_id: int, db: Session = Depends(get_db)): check_id = crud.get_supplier(db, supplier_id) if check_id is None: raise HTTPException(status_code=404) return crud.delete_supplier(db, supplier_id)
async def put_supplier(supplier: schemas.SupplierPut, supplier_id: int, db: Session = Depends(get_db)): check_id = crud.get_supplier(db, supplier_id) if check_id is None: raise HTTPException(status_code=404) return crud.put_supplier(db, supplier, supplier_id)
async def get_supplier(id: PositiveInt, db: Session = Depends(get_db)): db_supplier = crud.get_supplier(db, id) if db_supplier is None: raise HTTPException(status_code=404) return db_supplier
async def delete_supplier(supplier_id, db: Session = Depends(get_db)): db_supplier = crud.get_supplier(db, supplier_id) if db_supplier is None: raise HTTPException(status_code=404, detail="Supplier not found") crud.delete_supplier(db, supplier_id)
async def update_supplier(sup_id: int, supp: schemas.Supplier, db: Session = Depends(get_db)): db_supplier = check_for_supplier(db, sup_id) crud.upd_supp(db, supp, sup_id) return crud.get_supplier(db, sup_id)
def check_for_supplier(db, supp_id): is_supp = crud.get_supplier(db, supp_id) if is_supp is None: raise HTTPException(status_code=404, detail="Not Okie Dokie ID") return is_supp
async def get_shipper(supplier_id: PositiveInt, db: Session = Depends(get_db)): db_supplier = crud.get_supplier(db, supplier_id) if db_supplier is None: raise HTTPException(status_code=404, detail="Supplier not found") return db_supplier