Add admin user management
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
"""User management API routes."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import NoReturn
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response, status
|
||||
|
||||
from app.core.auth import require_admin
|
||||
from app.domain.models import User
|
||||
from app.schemas.user import CreateUserRequest, UpdateUserRequest, UserResponse
|
||||
from app.services.user_service import UserService, get_user_service
|
||||
|
||||
router = APIRouter(prefix="/users", tags=["users"])
|
||||
|
||||
|
||||
def _to_user_response(user: User) -> UserResponse:
|
||||
return UserResponse(
|
||||
id=user.id,
|
||||
username=user.username,
|
||||
email=user.email,
|
||||
created_at=user.created_at,
|
||||
updated_at=user.updated_at,
|
||||
)
|
||||
|
||||
|
||||
def _raise_user_error(exc: ValueError) -> NoReturn:
|
||||
detail = str(exc)
|
||||
if "찾을 수 없습니다" in detail:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=detail) from exc
|
||||
if "이미 존재" in detail:
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=detail) from exc
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=detail) from exc
|
||||
|
||||
|
||||
@router.get("", response_model=list[UserResponse], dependencies=[Depends(require_admin)])
|
||||
def list_users(service: UserService = Depends(get_user_service)) -> list[UserResponse]:
|
||||
return [_to_user_response(user) for user in service.list()]
|
||||
|
||||
|
||||
@router.get("/{user_id}", response_model=UserResponse, dependencies=[Depends(require_admin)])
|
||||
def get_user(user_id: str, service: UserService = Depends(get_user_service)) -> UserResponse:
|
||||
try:
|
||||
return _to_user_response(service.get(user_id))
|
||||
except ValueError as exc:
|
||||
_raise_user_error(exc)
|
||||
|
||||
|
||||
@router.post("", response_model=UserResponse, status_code=status.HTTP_201_CREATED, dependencies=[Depends(require_admin)])
|
||||
def create_user(payload: CreateUserRequest, service: UserService = Depends(get_user_service)) -> UserResponse:
|
||||
try:
|
||||
return _to_user_response(service.create(payload))
|
||||
except ValueError as exc:
|
||||
_raise_user_error(exc)
|
||||
|
||||
|
||||
@router.patch("/{user_id}", response_model=UserResponse, dependencies=[Depends(require_admin)])
|
||||
def update_user(
|
||||
user_id: str,
|
||||
payload: UpdateUserRequest,
|
||||
service: UserService = Depends(get_user_service),
|
||||
) -> UserResponse:
|
||||
try:
|
||||
return _to_user_response(service.update(user_id, payload))
|
||||
except ValueError as exc:
|
||||
_raise_user_error(exc)
|
||||
|
||||
|
||||
@router.delete("/{user_id}", status_code=status.HTTP_204_NO_CONTENT, dependencies=[Depends(require_admin)])
|
||||
def delete_user(user_id: str, service: UserService = Depends(get_user_service)) -> Response:
|
||||
try:
|
||||
service.delete(user_id)
|
||||
except ValueError as exc:
|
||||
_raise_user_error(exc)
|
||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||
Reference in New Issue
Block a user