feat: Semaine 8
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
users_db = {}
|
||||
next_id = 1
|
||||
|
||||
# DTO : Data Transfer Object
|
||||
class CreateUserDTO(BaseModel):
|
||||
name: str
|
||||
email: str
|
||||
|
||||
@app.post("/users")
|
||||
async def create_user(json: CreateUserDTO):
|
||||
global next_id
|
||||
user_id = next_id
|
||||
next_id += 1
|
||||
users_db[user_id] = {
|
||||
"id": user_id,
|
||||
"name": json.name,
|
||||
"email": json.email
|
||||
}
|
||||
return users_db[user_id]
|
||||
|
||||
@app.get("/users/{user_id}")
|
||||
async def get_user(user_id: int):
|
||||
user = users_db.get(user_id)
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
return user
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
return {"status": "ok", "service": "user-service"}
|
||||
Reference in New Issue
Block a user