Преглед изворни кода

a space could have multiple records now

chenhaiyang пре 4 година
родитељ
комит
4738965ae1
1 измењених фајлова са 23 додато и 18 уклоњено
  1. 23 18
      app/api/routers/space.py

+ 23 - 18
app/api/routers/space.py

@@ -2,12 +2,12 @@
 
 from typing import List
 
-from fastapi import APIRouter, Depends, Query, HTTPException
+from fastapi import APIRouter, Depends, HTTPException, Query
 from sqlalchemy.orm import Session
 
 from app.api.dependencies.db import get_db
 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.crud.space.weight import get_weights_by_space, get_weights_by_vav, create_weight, update_weight
 from app.models.domain.space import SpaceControlResponse
 from app.schemas.sapce_weight import SpaceWeight, SpaceWeightCreate, SpaceWeightUpdate
 from app.services.transfer import Season
@@ -45,12 +45,10 @@ async def create_space_weight(weight: SpaceWeightCreate, db: Session = Depends(g
     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/{space_id}', response_model=List[SpaceWeight])
+async def read_weight_by_space(space_id: str, db: Session = Depends(get_db)):
+    db_weights = get_weights_by_space(db, space_id=space_id)
+    return db_weights
 
 
 @router.get('/weight/vav/{vav_id}', response_model=List[SpaceWeight])
@@ -59,13 +57,20 @@ async def read_weight_by_vav(vav_id: str, db: Session = Depends(get_db)):
     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
+@router.patch('/weight/{space_id}/{vav_id}', response_model=SpaceWeight)
+async def update_weight_by_space(
+        space_id: str,
+        vav_id: str,
+        weight_in: SpaceWeightUpdate,
+        db: Session = Depends(get_db)
+):
+    weights = get_weights_by_space(db, space_id=space_id)
+    new_weight = None
+    for weight in weights:
+        if weight.vav_box_id == vav_id:
+            new_weight = update_weight(db, db_weight=weight, weight_in=weight_in)
+            break
+    if not new_weight:
+        raise HTTPException(status_code=404, detail='Wight not found')
+
+    return new_weight