Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

107 wiersze
3.3 KiB

  1. from fastapi import Depends, APIRouter, File, Form, HTTPException, UploadFile, status
  2. from sqlalchemy.orm import Session
  3. from db.session import get_db
  4. from db.repository.maintenancejob import (
  5. create_new_maintenancejob,
  6. create_car_part,
  7. get_all_maintenance_jobs,
  8. get_maintenance_job,
  9. change_maintenance_status,
  10. )
  11. from typing import List
  12. from schemas.maintenancejob import (
  13. CreateMaintenanceJob,
  14. OutputMaintenanceJob,
  15. OutputMinimalMaintenanceJob,
  16. )
  17. from schemas.carpart import CreateCarPart, ShowCarPart
  18. from db.models.user import User
  19. from apis.v1.route_auth import get_current_user
  20. router = APIRouter()
  21. @router.post("/", status_code=status.HTTP_201_CREATED)
  22. def create_maintenancejob(
  23. maintenancejob: CreateMaintenanceJob,
  24. db: Session = Depends(get_db),
  25. current_user: User = Depends(get_current_user),
  26. ):
  27. if current_user.Role != "Driver" and current_user.Role != "Admin":
  28. raise HTTPException(
  29. status_code=403, detail="You are not authorized to perform this action"
  30. )
  31. maintenancejob_res = create_new_maintenancejob(maintenancejob=maintenancejob, db=db)
  32. if maintenancejob_res == "nodriver":
  33. raise HTTPException(status_code=404, detail="This car has no driver")
  34. return maintenancejob_res
  35. @router.post(
  36. "/carpart", response_model=ShowCarPart, status_code=status.HTTP_201_CREATED
  37. )
  38. def create_carpart(
  39. car_part: CreateCarPart = Depends(),
  40. db: Session = Depends(get_db),
  41. current_user: User = Depends(get_current_user),
  42. ):
  43. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  44. raise HTTPException(
  45. status_code=403, detail="You are not authorized to perform this action"
  46. )
  47. car_part_res = create_car_part(car_part=car_part, db=db)
  48. return car_part_res
  49. @router.get(
  50. "/",
  51. response_model=List[OutputMinimalMaintenanceJob],
  52. status_code=status.HTTP_200_OK,
  53. )
  54. def get_all_maintenancejobs(
  55. db: Session = Depends(get_db),
  56. current_user: User = Depends(get_current_user),
  57. ):
  58. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  59. raise HTTPException(
  60. status_code=403, detail="You are not authorized to perform this action"
  61. )
  62. maintenancejobs = get_all_maintenance_jobs(db)
  63. return maintenancejobs
  64. @router.get(
  65. "/{maintenance_job_id}",
  66. response_model=OutputMaintenanceJob,
  67. status_code=status.HTTP_200_OK,
  68. )
  69. def get_maintenancejob(
  70. maintenance_job_id: int,
  71. db: Session = Depends(get_db),
  72. ):
  73. maintenancejob = get_maintenance_job(maintenance_job_id, db)
  74. if maintenancejob is None:
  75. raise HTTPException(
  76. status_code=404, detail="Maintenance job with this id does not exist"
  77. )
  78. return maintenancejob
  79. @router.patch("/{maintenance_job_id}", status_code=status.HTTP_200_OK)
  80. def change_status(
  81. maintenance_job_id: int,
  82. status: str,
  83. db: Session = Depends(get_db),
  84. current_user: User = Depends(get_current_user),
  85. ):
  86. if current_user.Role != "Maintenance":
  87. raise HTTPException(
  88. status_code=403, detail="You are not authorized to perform this action"
  89. )
  90. result = change_maintenance_status(maintenance_job_id, status, current_user.Id, db)
  91. if result is None:
  92. raise HTTPException(status_code=404, detail="Maintenance job not found")
  93. return result