Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

route_user.py 1.6 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Routes for user. MAIN PART OF THE API
  2. from fastapi import APIRouter, HTTPException, status
  3. from sqlalchemy.orm import Session
  4. from fastapi import Depends
  5. from typing import List, Annotated
  6. from apis.v1.route_auth import get_current_user
  7. from db.models.user import User
  8. from schemas.user import UserCreate, ShowUser
  9. from db.session import get_db
  10. from db.repository.user import create_new_user, list_users, get_user_by_id
  11. router = APIRouter()
  12. @router.post("/", response_model=ShowUser, status_code=status.HTTP_201_CREATED)
  13. def create_user(
  14. user: UserCreate,
  15. db: Session = Depends(get_db),
  16. ):
  17. # if current_user.Role != "Admin":
  18. # raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
  19. user = create_new_user(user=user, db=db)
  20. return user
  21. @router.get("/", response_model=List[ShowUser], status_code=status.HTTP_200_OK)
  22. def get_all_users(db: Session = Depends(get_db), role: str = None):
  23. if role is None:
  24. users = list_users(db=db)
  25. return users
  26. users = list_users(db=db, role=role)
  27. return users
  28. @router.get("/me", response_model=ShowUser, status_code=status.HTTP_200_OK)
  29. def get_user_me(
  30. current_user: Annotated[User, Depends(get_current_user)],
  31. db: Annotated[Session, Depends(get_db)],
  32. ):
  33. print("Getting current user...")
  34. return current_user
  35. @router.get("/{user_id}", response_model=ShowUser, status_code=status.HTTP_200_OK)
  36. def get_user(user_id: int, db: Session = Depends(get_db)):
  37. user = get_user_by_id(user_id=user_id, db=db)
  38. if not user:
  39. raise HTTPException(status_code=404, detail="User not found")
  40. return user