Просмотр исходного кода

add some router for space_weight table

chenhaiyang 4 лет назад
Родитель
Сommit
9facd9a6a3
1 измененных файлов с 38 добавлено и 1 удалено
  1. 38 1
      app/api/routers/space.py

+ 38 - 1
app/api/routers/space.py

@@ -1,9 +1,15 @@
 # -*- coding: utf-8 -*-
 
-from fastapi import APIRouter, Query
+from typing import List
+
+from fastapi import APIRouter, Depends, Query, HTTPException
+from sqlalchemy.orm import Session
 
 from app.controllers.equipment.fcu.q_learning import QLearningCommandBuilder
+from app.crud.space.weight import get_weight_by_space, get_weights_by_vav, create_weight, update_weight
+from app.api.dependencies.db import get_db
 from app.models.domain.space import SpaceControlResponse
+from app.schemas.sapce_weight import SpaceWeight, SpaceWeightCreate, SpaceWeightUpdate
 from app.services.transfer import Season
 
 router = APIRouter()
@@ -32,3 +38,34 @@ async def get_test_result(current: float, pre: float, target: float):
     command = await builder.get_command(current, pre, target)
 
     return command
+
+
+@router.post('/weight', response_model=SpaceWeight)
+async def create_space_weight(weight: SpaceWeightCreate, db: Session = Depends(get_db)):
+    return create_weight(db=db, weight=weight)
+
+
+@router.get('/weight/{space_id}', response_model=SpaceWeight)
+async def read_weight(space_id: str, db: Session = Depends(get_db)):
+    db_weight = get_weight_by_space(db, space_id=space_id)
+    if db_weight is None:
+        raise HTTPException(status_code=404, detail='Space weight not found')
+    return db_weight
+
+
+@router.get('/weight/vav/{vav_id}', response_model=List[SpaceWeight])
+async def read_weight_by_vav(vav_id: str, db: Session = Depends(get_db)):
+    db_weights = get_weights_by_vav(db, vav_id=vav_id)
+    return db_weights
+
+
+@router.patch('/weight/{space_id}', response_model=SpaceWeight)
+async def update_weight_by_space(space_id: str, weight_in: SpaceWeightUpdate, db: Session = Depends(get_db)):
+    weight = get_weight_by_space(db, space_id=space_id)
+    if not weight:
+        raise HTTPException(
+            status_code=404,
+            detail='The weight with this space does not in the system'
+        )
+    weight = update_weight(db, db_weight=weight, weight_in=weight_in)
+    return weight