Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

route_maintenancejob.py 2.4 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from fastapi import Depends, APIRouter, HTTPException, 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. )
  10. from schemas.maintenancejob import CreateMaintenanceJob
  11. from schemas.carpart import CreateCarPart
  12. from db.models.user import User
  13. from apis.v1.route_auth import get_current_user
  14. router = APIRouter()
  15. @router.post("/", status_code=status.HTTP_201_CREATED)
  16. def create_maintenancejob(
  17. maintenancejob: CreateMaintenanceJob,
  18. db: Session = Depends(get_db),
  19. current_user: User = Depends(get_current_user),
  20. ):
  21. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  22. raise HTTPException(
  23. status_code=403, detail="You are not authorized to perform this action"
  24. )
  25. maintenancejob_res = create_new_maintenancejob(
  26. maintenancejob=maintenancejob, maintenanceworker=current_user.Id, db=db
  27. )
  28. return maintenancejob_res
  29. @router.post("/carpart", status_code=status.HTTP_201_CREATED)
  30. def create_carpart(
  31. car_part: CreateCarPart,
  32. db: Session = Depends(get_db),
  33. current_user: User = Depends(get_current_user),
  34. ):
  35. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  36. raise HTTPException(
  37. status_code=403, detail="You are not authorized to perform this action"
  38. )
  39. car_part_res = create_car_part(car_part=car_part, db=db)
  40. return car_part_res
  41. @router.get("/", status_code=status.HTTP_200_OK)
  42. def get_all_maintenancejobs(
  43. db: Session = Depends(get_db),
  44. current_user: User = Depends(get_current_user),
  45. ):
  46. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  47. raise HTTPException(
  48. status_code=403, detail="You are not authorized to perform this action"
  49. )
  50. maintenancejobs = get_all_maintenance_jobs(db)
  51. return maintenancejobs
  52. @router.get("/{maintenance_job_id}", status_code=status.HTTP_200_OK)
  53. def get_maintenancejob(
  54. maintenance_job_id: int,
  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. maintenancejob = get_maintenance_job(maintenance_job_id, db)
  63. return maintenancejob