After_Chido_Api/api/v1/users.py

45 lines
2.0 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer
from services.user_service import UserService
from models.schemas import UserUpdateRole, UserBlockBan, UserResponse
from config.database import get_db
from services.auth_service import AuthService
from typing import Optional
router = APIRouter()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/auth/token")
@router.get("/", response_model=list[UserResponse])
async def list_users(status: Optional[str] = None, token: str = Depends(oauth2_scheme)):
async with get_db() as db:
return await UserService.list_users(token, status, db)
@router.patch("/role", status_code=200)
async def change_role(user_update: UserUpdateRole, token: str = Depends(oauth2_scheme)):
async with get_db() as db:
return await UserService.change_user_role(user_update, db, token)
@router.post("/block", status_code=200)
async def block_user(user_action: UserBlockBan, token: str = Depends(oauth2_scheme)):
async with get_db() as db:
return await UserService.block_user(user_action, db, token)
@router.post("/ban", status_code=200)
async def ban_user(user_action: UserBlockBan, token: str = Depends(oauth2_scheme)):
async with get_db() as db:
return await UserService.ban_user(user_action, db, token)
@router.post("/unblock", status_code=200)
async def unblock_user(user_action: UserBlockBan, token: str = Depends(oauth2_scheme)):
async with get_db() as db:
return await UserService.unblock_user(user_action, db, token)
@router.post("/unban", status_code=200)
async def unban_user(user_action: UserBlockBan, token: str = Depends(oauth2_scheme)):
async with get_db() as db:
return await UserService.unban_user(user_action, db, token)
@router.get("/getuserbymail", response_model=UserResponse)
async def get_user_by_email(email: str, token: str = Depends(oauth2_scheme)):
async with get_db() as db:
return await UserService.get_user_by_email(email, db, token)