105 lines
3.0 KiB
Python
105 lines
3.0 KiB
Python
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.session import get_db
|
|
from app.models.user import User
|
|
from app.core.auth import get_current_user, get_current_active_superuser
|
|
from app.schemas.user import UserCreate, UserUpdate, UserResponse
|
|
from app.crud import user as crud_user
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_user(
|
|
user_in: UserCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_superuser)
|
|
):
|
|
"""
|
|
Create new user (superuser only).
|
|
"""
|
|
user = crud_user.get_user_by_email(db, email=user_in.email)
|
|
if user:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="The user with this email already exists in the system."
|
|
)
|
|
user = crud_user.create_user(db=db, obj_in=user_in)
|
|
return user
|
|
|
|
@router.get("/", response_model=List[UserResponse])
|
|
def read_users(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_superuser)
|
|
):
|
|
"""
|
|
Retrieve users (superuser only).
|
|
"""
|
|
users = crud_user.get_users(db, skip=skip, limit=limit)
|
|
return users
|
|
|
|
@router.get("/me", response_model=UserResponse)
|
|
def read_user_me(
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""
|
|
Get current user.
|
|
"""
|
|
return current_user
|
|
|
|
@router.put("/me", response_model=UserResponse)
|
|
def update_user_me(
|
|
user_in: UserUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""
|
|
Update own user.
|
|
"""
|
|
user = crud_user.update_user(db, db_obj=current_user, obj_in=user_in)
|
|
return user
|
|
|
|
@router.get("/{user_id}", response_model=UserResponse)
|
|
def read_user_by_id(
|
|
user_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""
|
|
Get a specific user by id.
|
|
"""
|
|
user = crud_user.get_user(db, user_id=user_id)
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail="The user with this ID does not exist in the system"
|
|
)
|
|
if user.id != current_user.id and not current_user.is_superuser:
|
|
raise HTTPException(
|
|
status_code=403,
|
|
detail="Not enough permissions"
|
|
)
|
|
return user
|
|
|
|
@router.put("/{user_id}", response_model=UserResponse)
|
|
def update_user(
|
|
user_id: int,
|
|
user_in: UserUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_superuser)
|
|
):
|
|
"""
|
|
Update a user (superuser only).
|
|
"""
|
|
user = crud_user.get_user(db, user_id=user_id)
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=404,
|
|
detail="The user with this ID does not exist in the system"
|
|
)
|
|
user = crud_user.update_user(db, db_obj=user, obj_in=user_in)
|
|
return user |