early_start.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from fastapi import APIRouter, Depends, HTTPException
  2. from sqlalchemy.orm import Session
  3. from app.api.dependencies.db import get_db
  4. from app.crud.model_path.early_start import model_path_early_start_dtr
  5. from app.schemas.model_path.early_start import (
  6. EarlyStartDTRModelPath,
  7. EarlyStartDTRModelPathCreate,
  8. EarlyStartDTRModelPathUpdate,
  9. )
  10. router = APIRouter()
  11. @router.post("/early-start/dtr", response_model=EarlyStartDTRModelPath)
  12. async def create_model_path(
  13. model_path: EarlyStartDTRModelPathCreate, db: Session = Depends(get_db)
  14. ):
  15. return model_path_early_start_dtr.create(db=db, obj_in=model_path)
  16. @router.get("/early-start/dtr", response_model=list[EarlyStartDTRModelPath])
  17. async def read_model_path(
  18. skip: int = 0, limit: int = 100, db: Session = Depends(get_db)
  19. ):
  20. model_paths = model_path_early_start_dtr.get_multi(db, skip=skip, limit=limit)
  21. return model_paths
  22. @router.get("/early-start/dtr/{device_id}", response_model=EarlyStartDTRModelPath)
  23. async def read_model_path_by_device(device_id: str, db: Session = Depends(get_db)):
  24. db_model_path = model_path_early_start_dtr.get_path_by_device(
  25. db=db, device_id=device_id
  26. )
  27. return db_model_path
  28. @router.put("/early-start/dtr/{device_id}", response_model=EarlyStartDTRModelPath)
  29. async def update_model_path(
  30. device_id: str,
  31. model_path_in: EarlyStartDTRModelPathUpdate,
  32. db: Session = Depends(get_db),
  33. ):
  34. model_path = model_path_early_start_dtr.get_path_by_device(
  35. db=db, device_id=device_id
  36. )
  37. if model_path.device_id == device_id:
  38. new_model_path = model_path_early_start_dtr.update(
  39. db=db, db_obj=model_path, obj_in=model_path_in
  40. )
  41. else:
  42. raise HTTPException(status_code=404, detail="Model path not found")
  43. return new_model_path
  44. @router.delete("/early-start/dtr/{id}", response_model=EarlyStartDTRModelPath)
  45. async def delete_model_path(id: int, db: Session = Depends(get_db)):
  46. model_path = model_path_early_start_dtr.get(db=db, id=id)
  47. if not model_path:
  48. raise HTTPException(status_code=404, detail="Model path not found")
  49. model_path = model_path_early_start_dtr.remove(db=db, id=id)
  50. return model_path