Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

36 righe
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