2016-03-06 05:47:39 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2016-04-01 19:34:30 -05:00
|
|
|
"time"
|
|
|
|
|
2016-03-06 05:47:39 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2016-05-12 07:11:10 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-03-06 05:47:39 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-03-06 13:42:15 -06:00
|
|
|
bus.AddHandler("sql", GetPreferences)
|
2016-04-02 15:54:06 -05:00
|
|
|
bus.AddHandler("sql", GetPreferencesWithDefaults)
|
2016-03-06 05:47:39 -06:00
|
|
|
bus.AddHandler("sql", SavePreferences)
|
|
|
|
}
|
|
|
|
|
2016-04-02 15:54:06 -05:00
|
|
|
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{
|
2016-05-12 07:11:10 -05:00
|
|
|
Theme: setting.DefaultTheme,
|
2016-04-02 15:54:06 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-03-06 13:42:15 -06:00
|
|
|
func GetPreferences(query *m.GetPreferencesQuery) error {
|
|
|
|
|
2016-03-15 16:49:52 -05:00
|
|
|
var prefs m.Preferences
|
|
|
|
exists, err := x.Where("org_id=? AND user_id=?", query.OrgId, query.UserId).Get(&prefs)
|
2016-03-06 13:42:15 -06:00
|
|
|
|
2016-03-15 16:49:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-03-06 13:42:15 -06:00
|
|
|
}
|
2016-03-06 14:32:22 -06:00
|
|
|
|
2016-03-15 16:49:52 -05:00
|
|
|
if exists {
|
|
|
|
query.Result = &prefs
|
2016-03-06 14:32:22 -06:00
|
|
|
} else {
|
|
|
|
query.Result = new(m.Preferences)
|
2016-03-06 13:42:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-06 05:47:39 -06:00
|
|
|
func SavePreferences(cmd *m.SavePreferencesCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2016-03-06 05:47:39 -06:00
|
|
|
|
2016-03-15 16:49:52 -05:00
|
|
|
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,
|
2016-03-17 01:35:06 -05:00
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
2016-03-15 16:49:52 -05:00
|
|
|
}
|
|
|
|
_, err = sess.Insert(&prefs)
|
|
|
|
return err
|
2016-03-06 14:32:22 -06:00
|
|
|
} else {
|
2016-03-15 16:49:52 -05:00
|
|
|
prefs.HomeDashboardId = cmd.HomeDashboardId
|
|
|
|
prefs.Timezone = cmd.Timezone
|
|
|
|
prefs.Theme = cmd.Theme
|
2016-03-17 01:35:06 -05:00
|
|
|
prefs.Updated = time.Now()
|
|
|
|
prefs.Version += 1
|
2016-04-02 15:54:06 -05:00
|
|
|
_, err := sess.Id(prefs.Id).AllCols().Update(&prefs)
|
2016-03-15 16:49:52 -05:00
|
|
|
return err
|
2016-03-06 14:32:22 -06:00
|
|
|
}
|
2016-03-06 05:47:39 -06:00
|
|
|
})
|
|
|
|
}
|