system_config_service.py 942 B

12345678910111213141516171819202122232425262728
  1. from sqlalchemy.orm import Session
  2. from app.models.system_config import SystemConfig
  3. class SystemConfigService:
  4. @staticmethod
  5. def get_config(db: Session, key: str) -> str | None:
  6. config = db.query(SystemConfig).filter(SystemConfig.key == key).first()
  7. return config.value if config else None
  8. @staticmethod
  9. def set_config(db: Session, key: str, value: str, description: str = None) -> SystemConfig:
  10. config = db.query(SystemConfig).filter(SystemConfig.key == key).first()
  11. if config:
  12. config.value = value
  13. if description:
  14. config.description = description
  15. else:
  16. config = SystemConfig(key=key, value=value, description=description)
  17. db.add(config)
  18. db.commit()
  19. db.refresh(config)
  20. return config
  21. @staticmethod
  22. def get_all_configs(db: Session):
  23. return db.query(SystemConfig).all()