您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

90 行
2.9 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.fuelingtask import (
  5. create_fueling_task,
  6. delete_fueling_task,
  7. get_fueling_task_by_id,
  8. get_all_fueling_tasks,
  9. )
  10. from schemas.fuelingtask import (
  11. CreateFuelingTask,
  12. OutputFuelingTask,
  13. OutputFuelingTaskMin,
  14. OutputFuelingTaskList,
  15. )
  16. from db.models.user import User
  17. from apis.v1.route_auth import get_current_user
  18. router = APIRouter()
  19. @router.post("/", response_model=OutputFuelingTask, status_code=status.HTTP_201_CREATED)
  20. def create_fuelingtask(
  21. fuelingtask: CreateFuelingTask = Depends(),
  22. db: Session = Depends(get_db),
  23. current_user: User = Depends(get_current_user),
  24. ):
  25. if current_user.Role != "Fueling" and current_user.Role != "Admin":
  26. raise HTTPException(
  27. status_code=403, detail="You are not authorized to perform this action"
  28. )
  29. fuelingtask_res = create_fueling_task(
  30. fueling_task=fuelingtask, current_user=current_user.Id, db=db
  31. )
  32. if fuelingtask_res == "nodriver":
  33. raise HTTPException(status_code=404, detail="Driver ID not found")
  34. if fuelingtask_res == "novehicle":
  35. raise HTTPException(status_code=404, detail="Vehicle ID not found")
  36. return fuelingtask_res
  37. @router.get(
  38. "/{fueling_task_id}",
  39. response_model=OutputFuelingTask,
  40. status_code=status.HTTP_200_OK,
  41. )
  42. def get_fuelingtask(
  43. fueling_task_id: int,
  44. db: Session = Depends(get_db),
  45. current_user: User = Depends(get_current_user),
  46. ):
  47. if current_user.Role != "Fueling" and current_user.Role != "Admin":
  48. raise HTTPException(
  49. status_code=403, detail="You are not authorized to perform this action"
  50. )
  51. fuelingtask = get_fueling_task_by_id(fueling_task_id, db)
  52. if fuelingtask == "notfound":
  53. raise HTTPException(status_code=404, detail="fuck off")
  54. return fuelingtask
  55. @router.delete("/{fueling_task_id}", status_code=status.HTTP_204_NO_CONTENT)
  56. def delete_fuelingtask(
  57. fueling_task_id: int,
  58. db: Session = Depends(get_db),
  59. current_user: User = Depends(get_current_user),
  60. ):
  61. if current_user.Role != "Fueling" and current_user.Role != "Admin":
  62. raise HTTPException(
  63. status_code=403, detail="You are not authorized to perform this action"
  64. )
  65. if not delete_fueling_task(fueling_task_id, db):
  66. raise HTTPException(status_code=404, detail="Fueling task not found")
  67. @router.get("/", response_model=OutputFuelingTaskList, status_code=status.HTTP_200_OK)
  68. def get_all(
  69. db: Session = Depends(get_db), current_user: User = Depends(get_current_user)
  70. ):
  71. if current_user.Role != "Admin" and current_user.Role != "Fueling":
  72. raise HTTPException(
  73. status_code=403, detail="You are not authorized to perform this action"
  74. )
  75. tasks = get_all_fueling_tasks(db)
  76. return tasks