Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

110 righe
3.4 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. print("So it begins...")
  48. car_part_res = create_car_part(car_part=car_part, db=db)
  49. print("So it ends...")
  50. return car_part_res
  51. @router.get(
  52. "/",
  53. response_model=List[OutputMinimalMaintenanceJob],
  54. status_code=status.HTTP_200_OK,
  55. )
  56. def get_all_maintenancejobs(
  57. db: Session = Depends(get_db),
  58. current_user: User = Depends(get_current_user),
  59. ):
  60. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  61. raise HTTPException(
  62. status_code=403, detail="You are not authorized to perform this action"
  63. )
  64. maintenancejobs = get_all_maintenance_jobs(db)
  65. return maintenancejobs
  66. @router.get(
  67. "/{maintenance_job_id}",
  68. response_model=OutputMaintenanceJob,
  69. status_code=status.HTTP_200_OK,
  70. )
  71. def get_maintenancejob(
  72. maintenance_job_id: int,
  73. db: Session = Depends(get_db),
  74. ):
  75. maintenancejob = get_maintenance_job(maintenance_job_id, db)
  76. if maintenancejob is None:
  77. raise HTTPException(
  78. status_code=404, detail="Maintenance job with this id does not exist"
  79. )
  80. print(maintenancejob)
  81. return maintenancejob
  82. @router.patch("/{maintenance_job_id}", status_code=status.HTTP_200_OK)
  83. def change_status(
  84. maintenance_job_id: int,
  85. status: str,
  86. db: Session = Depends(get_db),
  87. current_user: User = Depends(get_current_user),
  88. ):
  89. if current_user.Role != "Maintenance":
  90. raise HTTPException(
  91. status_code=403, detail="You are not authorized to perform this action"
  92. )
  93. result = change_maintenance_status(maintenance_job_id, status, current_user.Id, db)
  94. if result is None:
  95. raise HTTPException(status_code=404, detail="Maintenance job not found")
  96. return result