Explorar el Código

add crud for space weight table

chenhaiyang hace 4 años
padre
commit
539b04ac6c
Se han modificado 1 ficheros con 44 adiciones y 0 borrados
  1. 44 0
      app/crud/space/weight.py

+ 44 - 0
app/crud/space/weight.py

@@ -0,0 +1,44 @@
+# -*- coding: utf-8 -*-
+
+from typing import List
+
+from fastapi.encoders import jsonable_encoder
+from sqlalchemy.orm import Session
+
+from app.models.space.weight import VAVRoomWeight
+from app.schemas.sapce_weight import SpaceWeightCreate, SpaceWeightUpdate
+from app.utils.date import get_time_str
+
+
+def get_weight_by_space(db: Session, space_id: str) -> VAVRoomWeight:
+    return db.query(VAVRoomWeight).filter(VAVRoomWeight.space_id == space_id).first()
+
+
+def get_weights_by_vav(db: Session, vav_id: str) -> List[VAVRoomWeight]:
+    return db.query(VAVRoomWeight).filter(VAVRoomWeight.vav_box_id == vav_id).all()
+
+
+def create_weight(db: Session, weight: SpaceWeightCreate) -> VAVRoomWeight:
+    db_weight = VAVRoomWeight(
+        default_weight=weight.default_weight,
+        project_id=weight.project_id,
+        space_id=weight.space_id,
+        vav_box_id=weight.vav_box_id
+    )
+    db.add(db_weight)
+    db.commit()
+    db.refresh(db_weight)
+    return db_weight
+
+
+def update_weight(db: Session, db_weight: VAVRoomWeight, weight_in: SpaceWeightUpdate) -> VAVRoomWeight:
+    db_weight.temporary_weight_update_time = get_time_str()
+    weight_data = jsonable_encoder(db_weight)
+    update_data = weight_in.dict(exclude_unset=True)
+    for field in weight_data:
+        if field in update_data:
+            setattr(db_weight, field, update_data[field])
+    db.add(db_weight)
+    db.commit()
+    db.refresh(db_weight)
+    return db_weight