Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

route_maintenancejob.py 3.4 KiB

1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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