You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

95 lines
3.1 KiB

  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. change_maintenance_status
  10. )
  11. from schemas.maintenancejob import CreateMaintenanceJob
  12. from schemas.carpart import CreateCarPart
  13. from db.models.user import User
  14. from apis.v1.route_auth import get_current_user
  15. router = APIRouter()
  16. @router.post("/", status_code=status.HTTP_201_CREATED)
  17. def create_maintenancejob(
  18. maintenancejob: CreateMaintenanceJob,
  19. db: Session = Depends(get_db),
  20. current_user: User = Depends(get_current_user),
  21. ):
  22. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  23. raise HTTPException(
  24. status_code=403, detail="You are not authorized to perform this action"
  25. )
  26. maintenancejob_res = create_new_maintenancejob(
  27. maintenancejob=maintenancejob, maintenanceworker=current_user.Id, db=db
  28. )
  29. return maintenancejob_res
  30. @router.post("/carpart", status_code=status.HTTP_201_CREATED)
  31. def create_carpart(
  32. car_part: CreateCarPart,
  33. db: Session = Depends(get_db),
  34. current_user: User = Depends(get_current_user),
  35. ):
  36. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  37. raise HTTPException(
  38. status_code=403, detail="You are not authorized to perform this action"
  39. )
  40. car_part_res = create_car_part(car_part=car_part, db=db)
  41. return car_part_res
  42. @router.get("/", status_code=status.HTTP_200_OK)
  43. def get_all_maintenancejobs(
  44. db: Session = Depends(get_db),
  45. current_user: User = Depends(get_current_user),
  46. ):
  47. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  48. raise HTTPException(
  49. status_code=403, detail="You are not authorized to perform this action"
  50. )
  51. maintenancejobs = get_all_maintenance_jobs(db)
  52. return maintenancejobs
  53. @router.get("/{maintenance_job_id}", status_code=status.HTTP_200_OK)
  54. def get_maintenancejob(
  55. maintenance_job_id: int,
  56. db: Session = Depends(get_db),
  57. current_user: User = Depends(get_current_user),
  58. ):
  59. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  60. raise HTTPException(
  61. status_code=403, detail="You are not authorized to perform this action"
  62. )
  63. maintenancejob = get_maintenance_job(maintenance_job_id, db)
  64. return maintenancejob
  65. @router.patch("/{maintenance_job_id}", status_code=status.HTTP_200_OK)
  66. def change_status(
  67. maintenance_job_id: int,
  68. status: str,
  69. db: Session = Depends(get_db),
  70. current_user: User = Depends(get_current_user),
  71. ):
  72. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  73. raise HTTPException(
  74. status_code=403, detail="You are not authorized to perform this action"
  75. )
  76. result = change_maintenance_status(maintenance_job_id, status, db)
  77. if result is None:
  78. raise HTTPException(
  79. status_code=404, detail="Maintenance job not found"
  80. )
  81. return result