Files
gauvainboiche 3315cb2336 feat: Semaine 8
2026-05-11 09:25:19 +02:00

34 lines
1.1 KiB
Python

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI()
products_db: dict = {
1: {"id": 1, "name": "Laptop", "price": 999.99, "stock": 10},
2: {"id": 2, "name": "Smartphone", "price": 499.99, "stock": 20},
3: {"id": 3, "name": "Headphones", "price": 199.99, "stock": 15},
}
class UpdateStockDTO(BaseModel):
quantity: int
@app.get("/products/{product_id}")
async def get_product(product_id: int):
product = products_db.get(product_id)
if not product:
raise HTTPException(status_code=404, detail="Product not found")
return product
@app.put("/products/{product_id}/stock")
async def update_stock(product_id: int, payload: UpdateStockDTO):
product = products_db.get(product_id)
if not product:
raise HTTPException(status_code=404, detail="Product not found")
if product["stock"] < payload.quantity:
raise HTTPException(status_code=400, detail="Insufficient stock")
product["stock"] -= payload.quantity
return product
@app.get("/health")
def health():
return {"status": "ok", "service": "catalog-service"}