From 785e8ad0bd33ed7a124a0401c61a71275c7a6b79 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Sun, 23 Mar 2025 15:08:27 +0100 Subject: [PATCH] Update code in endpoints\users.get.py --- endpoints/users.get.py | 60 ++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/endpoints/users.get.py b/endpoints/users.get.py index ca829c4..9eef5e7 100644 --- a/endpoints/users.get.py +++ b/endpoints/users.get.py @@ -1,30 +1,50 @@ from fastapi import APIRouter, HTTPException -users = [ - { - "id": "1", - "email": "user1@example.com", - "password": "password1", - "disabled": False - }, - { - "id": "2", - "email": "user2@example.com", - "password": "password2", - "disabled": True - } -] +users = [] # In-memory storage router = APIRouter() @router.get("/users", response_model=list) async def get_users(): """Get all registered users""" - if request.method != "GET": - raise HTTPException(status_code=405, detail="Method Not Allowed") - + return [ + { + "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 { "method": "GET", - "_verb": "get", - "users": users - } \ No newline at end of file + "_verb": "get", + "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. \ No newline at end of file