2022-12-08 03:27:15 +03:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
2022-12-09 14:40:57 +03:00
|
|
|
from med_backend.auth.schemas import UpdateUserProfile, User
|
2022-12-08 03:27:15 +03:00
|
|
|
from med_backend.db.dependencies import get_db_session
|
2022-12-09 02:33:22 +03:00
|
|
|
from med_backend.users import crud
|
2022-12-08 12:18:59 +03:00
|
|
|
from med_backend.users.schemas import FullUser, ListUser
|
2022-12-09 02:33:22 +03:00
|
|
|
from med_backend.users.services import get_current_active_manager
|
2022-12-08 03:27:15 +03:00
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/list", response_model=list[ListUser])
|
|
|
|
async def get_all_users(
|
|
|
|
skip: int = 0,
|
|
|
|
limit: int = 100,
|
2022-12-08 12:18:59 +03:00
|
|
|
current_user: User = Depends(get_current_active_manager),
|
2022-12-08 03:27:15 +03:00
|
|
|
session: AsyncSession = Depends(get_db_session),
|
|
|
|
):
|
2022-12-08 12:18:59 +03:00
|
|
|
users = await crud.get_users(session, skip, limit)
|
2022-12-08 03:27:15 +03:00
|
|
|
return users
|
2022-12-08 12:18:59 +03:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/{key}", response_model=FullUser)
|
|
|
|
async def get_user(
|
|
|
|
key: int,
|
|
|
|
current_user: User = Depends(get_current_active_manager),
|
|
|
|
session: AsyncSession = Depends(get_db_session),
|
|
|
|
):
|
|
|
|
user = await crud.get_user(session, key)
|
|
|
|
if not user:
|
|
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
|
|
return user
|
2022-12-09 14:40:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
@router.put("/{key}", response_model=FullUser)
|
|
|
|
async def update_user(
|
|
|
|
key: int,
|
|
|
|
data: UpdateUserProfile,
|
|
|
|
current_user: User = Depends(get_current_active_manager),
|
|
|
|
session: AsyncSession = Depends(get_db_session),
|
|
|
|
) -> User:
|
|
|
|
user = await crud.get_user(session, key)
|
|
|
|
if not user:
|
|
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
|
|
await crud.update_user(session, key, data)
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/{key}")
|
|
|
|
async def delete_user(
|
|
|
|
key: int,
|
|
|
|
current_user: User = Depends(get_current_active_manager),
|
|
|
|
session: AsyncSession = Depends(get_db_session),
|
|
|
|
):
|
|
|
|
user = await crud.get_user(session, key)
|
|
|
|
if not user:
|
|
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
|
|
await crud.delete_user(session, key)
|
|
|
|
return {"detail": "deleted"}
|