INNER CODE UNIT · Python
update_product
doganarif/fastapi-radar · example_app.py:187
async def update_product(product_id: int, product: ProductCreate, db: Session = Depends(get_db)):
"""Update an existing product."""
db_product = db.query(Product).filter(Product.id == product_id).first()
if not db_product:
raise HTTPException(status_code=404, detail="Product not found")
for key, value in product.dict().items():
setattr(db_product, key, value)
db.commit()
db.refresh(db_product)
return db_product
@app.delete("/products/{product_id}")
async def delete_product(product_id: int, db: Session = Depends(get_db)):
"""Delete a product."""