first commit
This commit is contained in:
32
backend/app/services/settings_service.py
Normal file
32
backend/app/services/settings_service.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.settings import GlobalSettings
|
||||
from app.schemas.settings import SettingsUpdate
|
||||
|
||||
|
||||
class SettingsService:
|
||||
def get_or_create(self, db: Session) -> GlobalSettings:
|
||||
settings = db.query(GlobalSettings).first()
|
||||
if not settings:
|
||||
settings = GlobalSettings()
|
||||
db.add(settings)
|
||||
db.commit()
|
||||
db.refresh(settings)
|
||||
return settings
|
||||
|
||||
def update(self, db: Session, payload: SettingsUpdate) -> GlobalSettings:
|
||||
settings = self.get_or_create(db)
|
||||
data = payload.model_dump(exclude={'clear_global_ssh_key'})
|
||||
for key, value in data.items():
|
||||
if key == 'global_ssh_key' and value is None and not payload.clear_global_ssh_key:
|
||||
continue
|
||||
setattr(settings, key, value)
|
||||
if payload.clear_global_ssh_key:
|
||||
settings.global_ssh_key = None
|
||||
db.add(settings)
|
||||
db.commit()
|
||||
db.refresh(settings)
|
||||
return settings
|
||||
|
||||
|
||||
settings_service = SettingsService()
|
||||
Reference in New Issue
Block a user