forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preferences.go
98 lines (80 loc) · 2.08 KB
/
preferences.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package sqlstore
import (
"time"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
)
func init() {
bus.AddHandler("sql", GetPreferences)
bus.AddHandler("sql", GetPreferencesWithDefaults)
bus.AddHandler("sql", SavePreferences)
}
func GetPreferencesWithDefaults(query *m.GetPreferencesWithDefaultsQuery) error {
prefs := make([]*m.Preferences, 0)
filter := "(org_id=? AND user_id=?) OR (org_id=? AND user_id=0)"
err := x.Where(filter, query.OrgId, query.UserId, query.OrgId).
OrderBy("user_id ASC").
Find(&prefs)
if err != nil {
return err
}
res := &m.Preferences{
Theme: setting.DefaultTheme,
Timezone: "browser",
HomeDashboardId: 0,
}
for _, p := range prefs {
if p.Theme != "" {
res.Theme = p.Theme
}
if p.Timezone != "" {
res.Timezone = p.Timezone
}
if p.HomeDashboardId != 0 {
res.HomeDashboardId = p.HomeDashboardId
}
}
query.Result = res
return nil
}
func GetPreferences(query *m.GetPreferencesQuery) error {
var prefs m.Preferences
exists, err := x.Where("org_id=? AND user_id=?", query.OrgId, query.UserId).Get(&prefs)
if err != nil {
return err
}
if exists {
query.Result = &prefs
} else {
query.Result = new(m.Preferences)
}
return nil
}
func SavePreferences(cmd *m.SavePreferencesCommand) error {
return inTransaction2(func(sess *session) error {
var prefs m.Preferences
exists, err := sess.Where("org_id=? AND user_id=?", cmd.OrgId, cmd.UserId).Get(&prefs)
if !exists {
prefs = m.Preferences{
UserId: cmd.UserId,
OrgId: cmd.OrgId,
HomeDashboardId: cmd.HomeDashboardId,
Timezone: cmd.Timezone,
Theme: cmd.Theme,
Created: time.Now(),
Updated: time.Now(),
}
_, err = sess.Insert(&prefs)
return err
} else {
prefs.HomeDashboardId = cmd.HomeDashboardId
prefs.Timezone = cmd.Timezone
prefs.Theme = cmd.Theme
prefs.Updated = time.Now()
prefs.Version += 1
_, err := sess.Id(prefs.Id).AllCols().Update(&prefs)
return err
}
})
}