early_start.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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(model_path: EarlyStartDTRModelPathCreate, db: Session = Depends(get_db)):
  14. return model_path_early_start_dtr.create(db=db, obj_in=model_path)
  15. @router.get('/early-start/dtr', response_model=List[EarlyStartDTRModelPath])
  16. async def read_model_path(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
  17. model_paths = model_path_early_start_dtr.get_multi(db, skip=skip, limit=limit)
  18. return model_paths
  19. @router.get('/early-start/dtr/{device_id}', response_model=EarlyStartDTRModelPath)
  20. async def read_model_path_by_device(device_id: str, db: Session = Depends(get_db)):
  21. db_model_path = model_path_early_start_dtr.get_path_by_device(db=db, device_id=device_id)
  22. return db_model_path
  23. @router.put('/early-start/dtr/{device_id}', response_model=EarlyStartDTRModelPath)
  24. async def update_model_path(device_id: str, model_path_in: EarlyStartDTRModelPathUpdate, db: Session = Depends(get_db)):
  25. model_path = model_path_early_start_dtr.get_path_by_device(db=db, device_id=device_id)
  26. if model_path.device_id == device_id:
  27. new_model_path = model_path_early_start_dtr.update(db=db, db_obj=model_path, obj_in=model_path_in)
  28. else:
  29. raise HTTPException(status_code=404, detail='Model path not found')
  30. return new_model_path
  31. @router.delete('/early-start/dtr/{id}', response_model=EarlyStartDTRModelPath)
  32. async def delete_model_path(id: int, db: Session = Depends(get_db)):
  33. model_path = model_path_early_start_dtr.get(db=db, id=id)
  34. if not model_path:
  35. raise HTTPException(status_code=404, detail='Model path not found')
  36. model_path = model_path_early_start_dtr.remove(db=db, id=id)
  37. return model_path