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.
 
 

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