Update code in endpoints\users.get.py

This commit is contained in:
Backend IM Bot 2025-03-23 15:08:27 +01:00
parent 03d34f83e8
commit 785e8ad0bd

View File

@ -1,30 +1,50 @@
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
users = [ users = [] # In-memory storage
{
"id": "1",
"email": "user1@example.com",
"password": "password1",
"disabled": False
},
{
"id": "2",
"email": "user2@example.com",
"password": "password2",
"disabled": True
}
]
router = APIRouter() router = APIRouter()
@router.get("/users", response_model=list) @router.get("/users", response_model=list)
async def get_users(): async def get_users():
"""Get all registered users""" """Get all registered users"""
if request.method != "GET": return [
raise HTTPException(status_code=405, detail="Method Not Allowed") {
"method": "GET",
"_verb": "get",
"users": [
{
"id": user["id"],
"username": user["username"],
"email": user["email"],
"disabled": user["disabled"]
}
for user in users
]
}
]
@router.get("/users/{user_id}")
async def get_user(user_id: str):
user = next((u for u in users if u["id"] == user_id), None)
if not user:
raise HTTPException(status_code=404, detail="User not found")
return { return {
"method": "GET", "method": "GET",
"_verb": "get", "_verb": "get",
"users": users "user": {
} "id": user["id"],
"username": user["username"],
"email": user["email"],
"disabled": user["disabled"]
}
}
```
This code defines two GET endpoints:
1. `/users` to get a list of all registered users
2. `/users/{user_id}` to get details of a specific user by ID
The `get_users` function returns a list of user objects with the specified fields. The `get_user` function looks up a user by ID and returns their details, or raises a 404 error if not found.
Both endpoints include the required `method` and `_verb` fields in the response.