Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

route_task.py 974 B

1234567891011121314151617181920212223242526272829303132
  1. from fastapi import Depends, APIRouter
  2. from sqlalchemy.orm import Session
  3. from fastapi import status, HTTPException
  4. from typing import Annotated
  5. from db.session import get_db
  6. from core.config import settings
  7. from db.repository.drivetask import (
  8. create_new_task,
  9. )
  10. from schemas.drivetask import CreateTask
  11. from db.models.user import User
  12. from apis.v1.route_auth import get_current_user
  13. router = APIRouter()
  14. @router.post("/", status_code=status.HTTP_201_CREATED)
  15. def create_tasK(
  16. task: CreateTask,
  17. db: Session = Depends(get_db),
  18. current_user: User = Depends(get_current_user),
  19. ):
  20. if current_user.Role != "Admin":
  21. raise HTTPException(
  22. status_code=403, detail="You are not authorized to perform this action"
  23. )
  24. task_res = create_new_task(task=task, db=db)
  25. if task_res == "notdriver":
  26. raise HTTPException(
  27. status_code=404, detail=f"Driver with id {task.DriverId} not found"
  28. )
  29. return task