feat: Semaine 8
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
FROM python:3.12-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN pip install fastapi uvicorn httpx
|
||||
|
||||
COPY main.py .
|
||||
|
||||
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
@@ -0,0 +1,34 @@
|
||||
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"}
|
||||
Reference in New Issue
Block a user