a.go
· 1.3 KiB · Go
Sin formato
package db
import (
"gorm.io/gorm/clause"
)
type AdminSetting struct {
Key string `gorm:"uniqueIndex"`
Value string
}
const (
SettingDisableSignup = "disable-signup"
SettingRequireLogin = "require-login"
SettingDisableLoginForm = "disable-login-form"
SettingDisableGravatar = "disable-gravatar"
)
func GetSetting(key string) (string, error) {
var setting AdminSetting
err := db.Where("key = ?", key).First(&setting).Error
return setting.Value, err
}
func GetSettings() (map[string]string, error) {
var settings []AdminSetting
err := db.Find(&settings).Error
if err != nil {
return nil, err
}
result := make(map[string]string)
for _, setting := range settings {
result[setting.Key] = setting.Value
}
return result, nil
}
func UpdateSetting(key string, value string) error {
return db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "key"}}, // key column
DoUpdates: clause.AssignmentColumns([]string{"value"}),
}).Create(&AdminSetting{
Key: key,
Value: value,
}).Error
}
func setSetting(key string, value string) error {
return db.Create(&AdminSetting{Key: key, Value: value}).Error
}
func initAdminSettings(settings map[string]string) error {
for key, value := range settings {
if err := setSetting(key, value); err != nil {
if !IsUniqueConstraintViolation(err) {
return err
}
}
}
return nil
}
1 | package db |
2 | |
3 | import ( |
4 | "gorm.io/gorm/clause" |
5 | ) |
6 | |
7 | type AdminSetting struct { |
8 | Key string `gorm:"uniqueIndex"` |
9 | Value string |
10 | } |
11 | |
12 | const ( |
13 | SettingDisableSignup = "disable-signup" |
14 | SettingRequireLogin = "require-login" |
15 | SettingDisableLoginForm = "disable-login-form" |
16 | SettingDisableGravatar = "disable-gravatar" |
17 | ) |
18 | |
19 | func 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 | |
25 | func 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 | |
40 | func 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 | |
50 | func setSetting(key string, value string) error { |
51 | return db.Create(&AdminSetting{Key: key, Value: value}).Error |
52 | } |
53 | |
54 | func 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 |