57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package settings
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"lst.net/internal/models"
|
|
"lst.net/pkg/logger"
|
|
)
|
|
|
|
func UpdateSetting(log *logger.CustomLogger, db *gorm.DB, id string, input SettingUpdateInput) error {
|
|
var cfg models.Settings
|
|
if err := db.Where("setting_id =?", id).First(&cfg).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
updates := map[string]interface{}{}
|
|
|
|
if input.Description != nil {
|
|
updates["description"] = *input.Description
|
|
}
|
|
if input.Value != nil {
|
|
updates["value"] = *input.Value
|
|
}
|
|
if input.Enabled != nil {
|
|
updates["enabled"] = *input.Enabled
|
|
}
|
|
if input.AppService != nil {
|
|
updates["app_service"] = *input.AppService
|
|
}
|
|
|
|
if len(updates) == 0 {
|
|
return nil // nothing to update
|
|
}
|
|
|
|
settingUpdate := db.Model(&cfg).Updates(updates)
|
|
|
|
if settingUpdate.Error != nil {
|
|
log.Error("There was an error updating the setting", "settings", map[string]interface{}{
|
|
"error": settingUpdate.Error,
|
|
})
|
|
return settingUpdate.Error
|
|
}
|
|
|
|
if err := Refresh(); err != nil {
|
|
log.Error("There was an error refreshing the settings after a setting update", "settings", map[string]interface{}{
|
|
"error": err,
|
|
})
|
|
}
|
|
|
|
log.Info("The setting was just updated", "settings", map[string]interface{}{
|
|
"id": id,
|
|
"name": cfg.Name,
|
|
"updated": updates,
|
|
})
|
|
|
|
return nil
|
|
}
|