You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

129 lines
4.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, Annotated
  6. from apis.v1.route_auth import get_current_user
  7. from core.config import settings
  8. from db.models.user import User
  9. from schemas.user import UserCreate, ShowUser, ShowDriver, DriverCreate
  10. from db.session import get_db
  11. from db.repository.user import (
  12. create_new_user,
  13. list_users,
  14. get_user_by_id,
  15. replace_user_data,
  16. create_new_driver,
  17. delete_user_data,
  18. )
  19. router = APIRouter()
  20. @router.post("/", response_model=ShowUser, status_code=status.HTTP_201_CREATED)
  21. def create_user(
  22. user: UserCreate,
  23. db: Session = Depends(get_db),
  24. current_user: User = Depends(get_current_user),
  25. ):
  26. if user.Role not in settings.ALLOWED_ROLES:
  27. raise HTTPException(
  28. status_code=400,
  29. detail=f"Role {user.Role} is not allowed. Allowed roles are {settings.ALLOWED_ROLES}",
  30. )
  31. if current_user.Role != "Admin":
  32. raise HTTPException(
  33. status_code=403, detail="You are not authorized to perform this action"
  34. )
  35. # if current_user.Role != "Admin":
  36. # raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
  37. user = create_new_user(user=user, db=db)
  38. return user
  39. @router.post("/driver", response_model=ShowDriver, status_code=status.HTTP_201_CREATED)
  40. def create_driver(
  41. driver: DriverCreate,
  42. db: Session = Depends(get_db),
  43. current_user: User = Depends(get_current_user),
  44. ):
  45. if current_user.Role != "Admin":
  46. raise HTTPException(
  47. status_code=403, detail="You are not authorized to perform this action"
  48. )
  49. driver = create_new_driver(driver=driver, db=db)
  50. return driver
  51. @router.get("/", response_model=List[ShowUser], status_code=status.HTTP_200_OK)
  52. def get_all_users(db: Session = Depends(get_db), role: str = None):
  53. if role is None:
  54. users = list_users(db=db)
  55. return users
  56. users = list_users(db=db, role=role)
  57. return users
  58. @router.put("/{user_id}", response_model=ShowUser, status_code=status.HTTP_202_ACCEPTED)
  59. def update_user(
  60. user_id: int,
  61. user: UserCreate,
  62. db: Session = Depends(get_db),
  63. current_user: User = Depends(get_current_user),
  64. ):
  65. if current_user.Role != "Admin":
  66. raise HTTPException(
  67. status_code=403, detail="You are not authorized to perform this action"
  68. )
  69. user = replace_user_data(user_id=user_id, user_data=user, db=db)
  70. if user == "userNotFound":
  71. raise HTTPException(status_code=404, detail="User not found")
  72. return user
  73. @router.get("/me", response_model=ShowUser, status_code=status.HTTP_200_OK)
  74. def get_user_me(
  75. current_user: Annotated[User, Depends(get_current_user)],
  76. db: Annotated[Session, Depends(get_db)],
  77. ):
  78. print("Getting current user...")
  79. return current_user
  80. @router.get("/{user_id}", response_model=ShowUser, status_code=status.HTTP_200_OK)
  81. def get_user(user_id: int, db: Session = Depends(get_db)):
  82. user = get_user_by_id(user_id=user_id, role="Any", db=db)
  83. if not user:
  84. raise HTTPException(status_code=404, detail="User not found")
  85. return user
  86. @router.get(
  87. "/driver/{driver_id}", response_model=ShowDriver, status_code=status.HTTP_200_OK
  88. )
  89. def get_driver(driver_id: int, db: Session = Depends(get_db)):
  90. driver = get_user_by_id(user_id=driver_id, role="Driver", db=db)
  91. if not driver:
  92. raise HTTPException(status_code=404, detail="Driver not found")
  93. res = driver.__dict__
  94. res["AssignedVehicle"] = driver.vehicle
  95. return driver
  96. @router.delete("/{user_id}", status_code=status.HTTP_200_OK)
  97. def delete_user(
  98. user_id: int,
  99. db: Session = Depends(get_db),
  100. current_user: User = Depends(get_current_user),
  101. ):
  102. if current_user.Role != "Admin":
  103. raise HTTPException(
  104. status_code=403, detail="You are not authorized to perform this action"
  105. )
  106. result = delete_user_data(id=user_id, db=db)
  107. if result == "userNotFound":
  108. raise HTTPException(status_code=404, detail="User not found")
  109. return result