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.
 
 

108 lines
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 CreateMaintenanceJob, OutputMaintenanceJob
  13. from schemas.carpart import CreateCarPart, ShowCarPart
  14. from db.models.user import User
  15. from apis.v1.route_auth import get_current_user
  16. router = APIRouter()
  17. @router.post("/", status_code=status.HTTP_201_CREATED)
  18. def create_maintenancejob(
  19. maintenancejob: CreateMaintenanceJob,
  20. db: Session = Depends(get_db),
  21. current_user: User = Depends(get_current_user),
  22. ):
  23. if current_user.Role != "Driver" and current_user.Role != "Admin":
  24. raise HTTPException(
  25. status_code=403, detail="You are not authorized to perform this action"
  26. )
  27. maintenancejob_res = create_new_maintenancejob(
  28. maintenancejob=maintenancejob, db=db
  29. )
  30. if maintenancejob_res == "nodriver":
  31. raise HTTPException(
  32. status_code=404, detail="This car has no driver"
  33. )
  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. "/", response_model=List[OutputMaintenanceJob], 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. print(maintenancejob)
  79. return maintenancejob
  80. @router.patch("/{maintenance_job_id}", status_code=status.HTTP_200_OK)
  81. def change_status(
  82. maintenance_job_id: int,
  83. status: str,
  84. db: Session = Depends(get_db),
  85. current_user: User = Depends(get_current_user),
  86. ):
  87. if current_user.Role != "Maintenance":
  88. raise HTTPException(
  89. status_code=403, detail="You are not authorized to perform this action"
  90. )
  91. result = change_maintenance_status(maintenance_job_id, status, current_user.Id, db)
  92. if result is None:
  93. raise HTTPException(status_code=404, detail="Maintenance job not found")
  94. return result