Spaces:
Sleeping
Sleeping
Update services.py
Browse files- services.py +29 -1
services.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# services.py
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
|
@@ -49,3 +48,32 @@ async def search_users(query: str) -> List[str]:
|
|
| 49 |
raise HTTPException(status_code=404, detail="No users found matching the query")
|
| 50 |
|
| 51 |
return matching_users
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
|
|
|
| 48 |
raise HTTPException(status_code=404, detail="No users found matching the query")
|
| 49 |
|
| 50 |
return matching_users
|
| 51 |
+
|
| 52 |
+
# New /api/ routes
|
| 53 |
+
@app.get("/api/users")
|
| 54 |
+
async def get_all_users() -> List[str]:
|
| 55 |
+
"""Get a list of all users."""
|
| 56 |
+
return list(USERS.keys())
|
| 57 |
+
|
| 58 |
+
@app.get("/api/user/{username}")
|
| 59 |
+
async def get_user(username: str):
|
| 60 |
+
"""Get details of a specific user."""
|
| 61 |
+
if username not in USERS:
|
| 62 |
+
raise HTTPException(status_code=404, detail="User not found")
|
| 63 |
+
return {"username": username, "password": USERS[username]}
|
| 64 |
+
|
| 65 |
+
@app.delete("/api/user/{username}")
|
| 66 |
+
async def delete_user(username: str):
|
| 67 |
+
"""Delete a specific user."""
|
| 68 |
+
if username not in USERS:
|
| 69 |
+
raise HTTPException(status_code=404, detail="User not found")
|
| 70 |
+
del USERS[username]
|
| 71 |
+
return {"message": f"User {username} deleted successfully"}
|
| 72 |
+
|
| 73 |
+
@app.put("/api/user/{username}")
|
| 74 |
+
async def update_user(username: str, user: User):
|
| 75 |
+
"""Update a user's password."""
|
| 76 |
+
if username not in USERS:
|
| 77 |
+
raise HTTPException(status_code=404, detail="User not found")
|
| 78 |
+
USERS[username] = user.password
|
| 79 |
+
return {"message": f"User {username} password updated successfully"}
|