From cd926b648703621b6ce4e4714bc4a8901360fd05 Mon Sep 17 00:00:00 2001 From: Alexis Feron Date: Mon, 3 Feb 2025 16:46:07 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20get=20one=20user=20route?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/main.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 73ba199..c22ce66 100644 --- a/app/main.py +++ b/app/main.py @@ -304,4 +304,20 @@ async def search_users(name: str, current_user: User = Depends(get_current_user) except pymongo.errors.OperationFailure: raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Regex may be wrongly formatted") - return users \ No newline at end of file + return users + +@app.get( + path="/user/{id}", + responses={401: {"model": HTTPError}, 422: {"model": HTTPError}, 404: {"model": HTTPError}}, + response_model=UserDTO +) +async def get_user(id: str, current_user: User = Depends(get_current_user)): + try: + user = users_collection.find_one({"_id": ObjectId(id)}) + except bson.errors.InvalidId: + objectid_misformatted() + + if user is None: + raise HTTPException(status_code=404, detail="User not found") + + return serializers.user_serialize(user) \ No newline at end of file