Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
1 рік тому
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 db.models.user import User
  8. from schemas.user import UserCreate, ShowUser, ShowDriver, DriverCreate
  9. from db.session import get_db
  10. from db.repository.user import create_new_user, list_users, get_user_by_id, replace_user_data, create_new_driver
  11. router = APIRouter()
  12. @router.post("/", response_model=ShowUser, status_code=status.HTTP_201_CREATED)
  13. def create_user(
  14. user: UserCreate,
  15. db: Session = Depends(get_db),
  16. current_user: User = Depends(get_current_user),
  17. ):
  18. if (current_user.Role != "Admin"):
  19. raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
  20. # if current_user.Role != "Admin":
  21. # raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
  22. user = create_new_user(user=user, db=db)
  23. return user
  24. @router.post("/driver", response_model=ShowDriver, status_code=status.HTTP_201_CREATED)
  25. def create_driver(
  26. driver: DriverCreate,
  27. db: Session = Depends(get_db),
  28. current_user: User = Depends(get_current_user),
  29. ):
  30. if (current_user.Role != "Admin"):
  31. raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
  32. driver = create_new_driver(driver=driver, db=db)
  33. return driver
  34. @router.get("/", response_model=List[ShowUser], status_code=status.HTTP_200_OK)
  35. def get_all_users(db: Session = Depends(get_db), role: str = None):
  36. if role is None:
  37. users = list_users(db=db)
  38. return users
  39. users = list_users(db=db, role=role)
  40. return users
  41. @router.put("/{user_id}", response_model=ShowUser, status_code=status.HTTP_202_ACCEPTED)
  42. def update_user(
  43. user_id: int,
  44. user: UserCreate,
  45. db: Session = Depends(get_db),
  46. current_user: User = Depends(get_current_user),
  47. ):
  48. if current_user.Role != "Admin":
  49. raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
  50. user = replace_user_data(user_id=user_id, user=user, db=db)
  51. return user
  52. @router.get("/me", response_model=ShowUser, status_code=status.HTTP_200_OK)
  53. def get_user_me(
  54. current_user: Annotated[User, Depends(get_current_user)],
  55. db: Annotated[Session, Depends(get_db)],
  56. ):
  57. print("Getting current user...")
  58. return current_user
  59. @router.get("/{user_id}", response_model=ShowUser, status_code=status.HTTP_200_OK)
  60. def get_user(user_id: int, db: Session = Depends(get_db)):
  61. user = get_user_by_id(user_id=user_id, db=db)
  62. if not user:
  63. raise HTTPException(status_code=404, detail="User not found")
  64. return user
  65. @router.get("/driver/{driver_id}", response_model=ShowDriver, status_code=status.HTTP_200_OK)
  66. def get_driver(driver_id: int, db: Session = Depends(get_db)):
  67. driver = get_user_by_id(user_id=driver_id, role = "Driver", db=db)
  68. res = driver.__dict__
  69. res["AssignedVehicle"] = driver.vehicle
  70. if not driver:
  71. raise HTTPException(status_code=404, detail="Driver not found")
  72. return driver