cf085770e7
- 新增 settings 表用于存储 public/private 两行 JSON 配置 - 添加 AdminSettings 相关类型定义和 API 接口 - 实现管理后台系统设置页面,支持可视化和 JSON 编辑模式 - 集成 CodeMirror 编辑器用于 JSON 配置编辑 - 添加系统设置相关的路由和接口 - 更新依赖包并修改相关样式和布局文件
49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/basketikun/infinite-canvas/model"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
// GetSettings 返回 public 和 private 两行配置。
|
|
func GetSettings() (model.Settings, error) {
|
|
db, err := DB()
|
|
if err != nil {
|
|
return model.Settings{}, err
|
|
}
|
|
var items []model.Setting
|
|
if err := db.Find(&items).Error; err != nil {
|
|
return model.Settings{}, err
|
|
}
|
|
result := model.Settings{}
|
|
for _, item := range items {
|
|
if item.Key == model.SettingKeyPrivate {
|
|
_ = json.Unmarshal(item.Value, &result.Private)
|
|
} else if item.Key == model.SettingKeyPublic {
|
|
_ = json.Unmarshal(item.Value, &result.Public)
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// SaveSettings 保存 public 和 private 两行配置。
|
|
func SaveSettings(settings model.Settings, now string) (model.Settings, error) {
|
|
db, err := DB()
|
|
if err != nil {
|
|
return settings, err
|
|
}
|
|
publicValue, _ := json.Marshal(settings.Public)
|
|
privateValue, _ := json.Marshal(settings.Private)
|
|
items := []model.Setting{
|
|
{Key: model.SettingKeyPublic, Value: publicValue, CreatedAt: now, UpdatedAt: now},
|
|
{Key: model.SettingKeyPrivate, Value: privateValue, CreatedAt: now, UpdatedAt: now},
|
|
}
|
|
err = db.Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "key"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{"value", "updated_at"}),
|
|
}).Create(&items).Error
|
|
return settings, err
|
|
}
|