constant.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import time
  2. from typing import List
  3. from app import models, schemas
  4. from sqlalchemy.orm import Session
  5. def create_constant(db: Session,item: schemas.ConstantCreate):
  6. db_item = models.Constant(**item.dict())
  7. db.add(db_item)
  8. db.commit()
  9. db.refresh(db_item)
  10. return db_item
  11. def get_constant_list(db: Session, type: str):
  12. res: List[models.Constant] = db.query(models.Constant).filter(models.Constant.type == type).all()
  13. return [r.value for r in res]
  14. def find_and_update(db: Session, type: str, value: str):
  15. res: List[models.Constant] = db.query(models.Constant)\
  16. .filter(models.Constant.type == type)\
  17. .filter(models.Constant.value == value).all()
  18. if len(res) == 0:
  19. db_item = models.Constant(**{
  20. 'type': type,
  21. 'value': value
  22. })
  23. db.add(db_item)
  24. db.commit()
  25. db.refresh(db_item)
  26. def delete_constant(db: Session, type: str, value: str):
  27. db_item = db.query(models.Constant)\
  28. .filter(models.Constant.type == type)\
  29. .filter(models.Constant.value == value).first()
  30. if not db_item:
  31. raise Exception('未找到该常亮')
  32. else:
  33. db.query(models.Constant)\
  34. .filter(models.Constant.type == type)\
  35. .filter(models.Constant.value == value).delete()
  36. db.commit()
  37. db.flush()
  38. return db_item