Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

route_maintenancejob.py 3.4 KiB

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