最後活躍 1703554190

修訂 fd9b4f1dd0817cbc8b160d6c1982b1e44cf6efb1

a.go 原始檔案
1package db
2
3import (
4 "gorm.io/gorm/clause"
5)
6
7type AdminSetting struct {
8 Key string `gorm:"uniqueIndex"`
9 Value string
10}
11
12const (
13 SettingDisableSignup = "disable-signup"
14 SettingRequireLogin = "require-login"
15 SettingDisableLoginForm = "disable-login-form"
16 SettingDisableGravatar = "disable-gravatar"
17)
18
19func GetSetting(key string) (string, error) {
20 var setting AdminSetting
21 err := db.Where("key = ?", key).First(&setting).Error
22 return setting.Value, err
23}
24
25func GetSettings() (map[string]string, error) {
26 var settings []AdminSetting
27 err := db.Find(&settings).Error
28 if err != nil {
29 return nil, err
30 }
31
32 result := make(map[string]string)
33 for _, setting := range settings {
34 result[setting.Key] = setting.Value
35 }
36
37 return result, nil
38}
39
40func UpdateSetting(key string, value string) error {
41 return db.Clauses(clause.OnConflict{
42 Columns: []clause.Column{{Name: "key"}}, // key column
43 DoUpdates: clause.AssignmentColumns([]string{"value"}),
44 }).Create(&AdminSetting{
45 Key: key,
46 Value: value,
47 }).Error
48}
49
50func setSetting(key string, value string) error {
51 return db.Create(&AdminSetting{Key: key, Value: value}).Error
52}
53
54func initAdminSettings(settings map[string]string) error {
55 for key, value := range settings {
56 if err := setSetting(key, value); err != nil {
57 if !IsUniqueConstraintViolation(err) {
58 return err
59 }
60 }
61 }
62
63 return nil
64}
65