37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
from fastapi import APIRouter
|
|
from fastapi import HTTPException
|
|
|
|
from models.user import User, UserChangePassword
|
|
from lib.db import get_connection
|
|
|
|
router = APIRouter(prefix="/users", tags=["users"])
|
|
|
|
|
|
@router.get("/", response_model=list[User])
|
|
async def list_users():
|
|
with get_connection() as conn:
|
|
users = conn.execute('SELECT * FROM "User"').fetchdf()
|
|
|
|
# Convert DataFrame to dict
|
|
users_dict = users.to_dict(orient="records")
|
|
return users_dict
|
|
|
|
|
|
@router.get("/{user_id}", response_model=User)
|
|
async def get_user(user_id: int):
|
|
with get_connection() as conn:
|
|
user_df = conn.execute(
|
|
'SELECT * FROM "User" WHERE id = ?', (user_id,)
|
|
).fetchdf()
|
|
|
|
if user_df.empty:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
|
|
user_data = user_df.to_dict(orient="records")[0]
|
|
return User(**user_data)
|
|
|
|
|
|
@router.patch("/{user_id}")
|
|
async def change_password(user: UserChangePassword):
|
|
raise HTTPException(status_code=401, detail="Not implemented")
|