選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

103 行
3.3 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 typing import List
  12. from schemas.maintenancejob import CreateMaintenanceJob, OutputMaintenanceJob
  13. from schemas.carpart import CreateCarPart
  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("/carpart", status_code=status.HTTP_201_CREATED)
  32. def create_carpart(
  33. car_part: CreateCarPart,
  34. db: Session = Depends(get_db),
  35. current_user: User = Depends(get_current_user),
  36. ):
  37. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  38. raise HTTPException(
  39. status_code=403, detail="You are not authorized to perform this action"
  40. )
  41. car_part_res = create_car_part(car_part=car_part, db=db)
  42. return car_part_res
  43. @router.get(
  44. "/", response_model=List[OutputMaintenanceJob], status_code=status.HTTP_200_OK
  45. )
  46. def get_all_maintenancejobs(
  47. db: Session = Depends(get_db),
  48. current_user: User = Depends(get_current_user),
  49. ):
  50. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  51. raise HTTPException(
  52. status_code=403, detail="You are not authorized to perform this action"
  53. )
  54. maintenancejobs = get_all_maintenance_jobs(db)
  55. return maintenancejobs
  56. @router.get(
  57. "/{maintenance_job_id}",
  58. response_model=OutputMaintenanceJob,
  59. status_code=status.HTTP_200_OK,
  60. )
  61. def get_maintenancejob(
  62. maintenance_job_id: int,
  63. db: Session = Depends(get_db),
  64. current_user: User = Depends(get_current_user),
  65. ):
  66. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  67. raise HTTPException(
  68. status_code=403, detail="You are not authorized to perform this action"
  69. )
  70. maintenancejob = get_maintenance_job(maintenance_job_id, db)
  71. print(maintenancejob)
  72. return maintenancejob
  73. @router.patch("/{maintenance_job_id}", status_code=status.HTTP_200_OK)
  74. def change_status(
  75. maintenance_job_id: int,
  76. status: str,
  77. db: Session = Depends(get_db),
  78. current_user: User = Depends(get_current_user),
  79. ):
  80. if current_user.Role != "Maintenance" and current_user.Role != "Admin":
  81. raise HTTPException(
  82. status_code=403, detail="You are not authorized to perform this action"
  83. )
  84. result = change_maintenance_status(maintenance_job_id, status, db)
  85. if result is None:
  86. raise HTTPException(status_code=404, detail="Maintenance job not found")
  87. return result