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.
 
 

158 lines
4.8 KiB

  1. # Routes for user. MAIN PART OF THE API
  2. from math import ceil
  3. from fastapi import APIRouter, HTTPException, status, Query
  4. from sqlalchemy.orm import Session
  5. from sqlalchemy import or_
  6. from fastapi import Depends
  7. from typing import List, Annotated
  8. from apis.v1.route_auth import get_current_user
  9. from core.config import settings
  10. from db.models.user import User
  11. from schemas.user import (
  12. UserCreate,
  13. ShowUser,
  14. ShowDriver,
  15. DriverCreate,
  16. OutputUser,
  17. UsersPage,
  18. )
  19. from db.session import get_db
  20. from db.repository.user import (
  21. create_new_user,
  22. list_users,
  23. get_user_by_id,
  24. replace_user_data,
  25. create_new_driver,
  26. delete_user_data,
  27. get_users_by_name,
  28. user_search_query,
  29. )
  30. router = APIRouter()
  31. @router.post("/", response_model=ShowUser, status_code=status.HTTP_201_CREATED)
  32. def create_user(
  33. user: UserCreate,
  34. db: Session = Depends(get_db),
  35. current_user: User = Depends(get_current_user),
  36. ):
  37. if user.Role not in settings.ALLOWED_ROLES:
  38. raise HTTPException(
  39. status_code=400,
  40. detail=f"Role {user.Role} is not allowed. Allowed roles are {settings.ALLOWED_ROLES}",
  41. )
  42. if current_user.Role != "Admin":
  43. raise HTTPException(
  44. status_code=403, detail="You are not authorized to perform this action"
  45. )
  46. # if current_user.Role != "Admin":
  47. # raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
  48. user = create_new_user(user=user, db=db)
  49. if user == "userExists":
  50. raise HTTPException(
  51. status_code=400, detail=f"User with this email already exists"
  52. )
  53. return user
  54. @router.post("/driver", response_model=ShowDriver, status_code=status.HTTP_201_CREATED)
  55. def create_driver(
  56. driver: DriverCreate,
  57. db: Session = Depends(get_db),
  58. current_user: User = Depends(get_current_user),
  59. ):
  60. if current_user.Role != "Admin":
  61. raise HTTPException(
  62. status_code=403, detail="You are not authorized to perform this action"
  63. )
  64. driver = create_new_driver(driver=driver, db=db)
  65. if driver == "userExists":
  66. raise HTTPException(
  67. status_code=400, detail=f"User with this email already exists"
  68. )
  69. return driver
  70. @router.get("/", response_model=List[ShowUser], status_code=status.HTTP_200_OK)
  71. def get_all_users(db: Session = Depends(get_db), role: str = None):
  72. if role is None:
  73. users = list_users(db=db)
  74. return users
  75. users = list_users(db=db, role=role)
  76. return users
  77. @router.put("/{user_id}", response_model=ShowUser, status_code=status.HTTP_202_ACCEPTED)
  78. def update_user(
  79. user_id: int,
  80. user: UserCreate,
  81. db: Session = Depends(get_db),
  82. current_user: User = Depends(get_current_user),
  83. ):
  84. if current_user.Role != "Admin":
  85. raise HTTPException(
  86. status_code=403, detail="You are not authorized to perform this action"
  87. )
  88. user = replace_user_data(user_id=user_id, user_data=user, db=db)
  89. if user == "userNotFound":
  90. raise HTTPException(status_code=404, detail="User not found")
  91. return user
  92. @router.get("/me", response_model=ShowUser, status_code=status.HTTP_200_OK)
  93. def get_user_me(
  94. current_user: Annotated[User, Depends(get_current_user)],
  95. db: Annotated[Session, Depends(get_db)],
  96. ):
  97. return current_user
  98. @router.get("/{user_id}", response_model=ShowUser, status_code=status.HTTP_200_OK)
  99. def get_user(user_id: int, db: Session = Depends(get_db)):
  100. user = get_user_by_id(user_id=user_id, role="Any", db=db)
  101. if not user:
  102. raise HTTPException(status_code=404, detail="User not found")
  103. return user
  104. @router.get(
  105. "/driver/{driver_id}", response_model=ShowDriver, status_code=status.HTTP_200_OK
  106. )
  107. def get_driver(driver_id: int, db: Session = Depends(get_db)):
  108. driver = get_user_by_id(user_id=driver_id, role="Driver", db=db)
  109. if not driver:
  110. raise HTTPException(status_code=404, detail="Driver not found")
  111. res = driver.__dict__
  112. res["AssignedVehicle"] = driver.vehicle
  113. return driver
  114. @router.delete("/{user_id}", status_code=status.HTTP_200_OK)
  115. def delete_user(
  116. user_id: int,
  117. db: Session = Depends(get_db),
  118. current_user: User = Depends(get_current_user),
  119. ):
  120. if current_user.Role != "Admin":
  121. raise HTTPException(
  122. status_code=403, detail="You are not authorized to perform this action"
  123. )
  124. result = delete_user_data(id=user_id, db=db)
  125. if result == "userNotFound":
  126. raise HTTPException(status_code=404, detail="User not found")
  127. return result
  128. @router.get("/search/", response_model=UsersPage)
  129. def search_users(
  130. db: Session = Depends(get_db),
  131. name: str = None,
  132. role: str = None,
  133. page: int = 1,
  134. per_page: int = 20,
  135. ):
  136. return user_search_query(db=db, name=name, role=role, page=page, per_page=per_page)