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.
 
 

36 linhas
1.1 KiB

  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
  6. from schemas.user import UserCreate, ShowUser
  7. from db.session import get_db
  8. from db.repository.user import create_new_user, list_users, get_user_by_id
  9. router = APIRouter()
  10. @router.post("/", response_model=ShowUser, status_code=status.HTTP_201_CREATED)
  11. def create_user(user: UserCreate, db: Session = Depends(get_db)):
  12. user = create_new_user(user=user, db=db)
  13. return user
  14. @router.get("/", response_model=List[ShowUser], status_code=status.HTTP_200_OK)
  15. def get_all_users(db: Session = Depends(get_db), role: str = None):
  16. if role == None:
  17. users = list_users(db=db)
  18. return users
  19. users = list_users(db=db, role=role)
  20. return users
  21. @router.get("/{user_id}", response_model=ShowUser, status_code=status.HTTP_200_OK)
  22. def get_user(user_id: int, db: Session = Depends(get_db)):
  23. user = get_user_by_id(user_id=user_id, db=db)
  24. if not user:
  25. raise HTTPException(status_code=404, detail="User not found")
  26. return user