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.
 
 

78 lines
2.1 KiB

  1. from sqlalchemy.orm import Session
  2. from schemas.drivetask import CreateTask
  3. from db.models.drivetask import DriveTask
  4. from db.repository.user import get_user_by_id
  5. def create_new_task(task: CreateTask, db: Session):
  6. driver = get_user_by_id(task.DriverId, db)
  7. if driver is None:
  8. return "notdriver"
  9. elif driver.Role != "Driver":
  10. return "notdriver"
  11. task_object = DriveTask(
  12. DriverId=task.DriverId,
  13. Description=task.Description,
  14. Status="Pending",
  15. StartLocation=task.StartLocation,
  16. EndLocation=task.EndLocation,
  17. )
  18. db.add(task_object)
  19. db.commit()
  20. db.refresh(task_object)
  21. return task_object
  22. def change_task_status(task_id: int, status: str, db: Session):
  23. task = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
  24. if not task:
  25. return "notaskfound"
  26. task.Status = status
  27. db.commit()
  28. db.refresh(task)
  29. return task
  30. def get_task_driver(task_id: int, db: Session):
  31. task = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
  32. if not task:
  33. return "notaskfound"
  34. driver = get_user_by_id(task.DriverId, db)
  35. if not driver:
  36. return "notdriver"
  37. return driver
  38. def get_tasks_by_driver(driver_id: int, db: Session):
  39. driver = get_user_by_id(driver_id, db)
  40. if not driver:
  41. return "notdriver"
  42. if driver.Role != "Driver":
  43. return "notdriver"
  44. tasks = db.query(DriveTask).filter(DriveTask.DriverId == driver_id).all()
  45. return tasks
  46. def get_task_by_id(task_id: int, db: Session):
  47. task = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
  48. if not task:
  49. return "notaskfound"
  50. return task
  51. def get_all_tasks(db: Session):
  52. tasks = db.query(DriveTask).all()
  53. return tasks
  54. def edit_task(task_id: int, task: CreateTask, db: Session):
  55. tasks = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
  56. if not tasks:
  57. return "notaskfound"
  58. tasks.Description = task.Description
  59. tasks.StartLocation = task.StartLocation
  60. tasks.EndLocation = task.EndLocation
  61. db.commit()
  62. db.refresh(tasks)
  63. return tasks