early_start.py 2.2 KB

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