您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

route_user.py 817 B

123456789101112131415161718192021222324252627
  1. # Routes for user. MAIN PART OF THE API
  2. from fastapi import APIRouter, 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
  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