grafana/pkg/api/preferences.go

74 lines
1.9 KiB
Go
Raw Normal View History

2016-03-06 05:47:39 -06:00
package api
import (
2016-04-01 19:34:30 -05:00
"github.com/grafana/grafana/pkg/api/dtos"
2016-03-06 05:47:39 -06:00
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
)
2016-03-17 01:22:27 -05:00
// POST /api/preferences/set-home-dash
2018-03-07 10:54:50 -06:00
func SetHomeDashboard(c *m.ReqContext, cmd m.SavePreferencesCommand) Response {
2016-03-17 01:22:27 -05:00
2016-03-17 01:35:06 -05:00
cmd.UserId = c.UserId
cmd.OrgId = c.OrgId
2016-03-17 01:22:27 -05:00
2016-03-17 01:35:06 -05:00
if err := bus.Dispatch(&cmd); err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to set home dashboard", err)
2016-03-17 01:35:06 -05:00
}
2016-03-17 01:22:27 -05:00
2018-03-22 16:13:46 -05:00
return Success("Home dashboard set")
2016-03-17 01:22:27 -05:00
}
2016-04-01 19:34:30 -05:00
// GET /api/user/preferences
2018-03-07 10:54:50 -06:00
func GetUserPreferences(c *m.ReqContext) Response {
2018-11-12 13:01:53 -06:00
return getPreferencesFor(c.OrgId, c.UserId, 0)
}
2018-11-12 13:01:53 -06:00
func getPreferencesFor(orgID, userID, teamID int64) Response {
prefsQuery := m.GetPreferencesQuery{UserId: userID, OrgId: orgID, TeamId: teamID}
2016-04-01 19:34:30 -05:00
if err := bus.Dispatch(&prefsQuery); err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to get preferences", err)
2016-04-01 19:34:30 -05:00
}
dto := dtos.Prefs{
Theme: prefsQuery.Result.Theme,
2018-03-22 16:13:46 -05:00
HomeDashboardID: prefsQuery.Result.HomeDashboardId,
Timezone: prefsQuery.Result.Timezone,
2016-04-01 19:34:30 -05:00
}
2018-03-22 16:13:46 -05:00
return JSON(200, &dto)
2016-04-01 19:34:30 -05:00
}
// PUT /api/user/preferences
2018-03-07 10:54:50 -06:00
func UpdateUserPreferences(c *m.ReqContext, dtoCmd dtos.UpdatePrefsCmd) Response {
2018-11-12 13:01:53 -06:00
return updatePreferencesFor(c.OrgId, c.UserId, 0, &dtoCmd)
}
2018-11-12 13:01:53 -06:00
func updatePreferencesFor(orgID, userID, teamId int64, dtoCmd *dtos.UpdatePrefsCmd) Response {
2016-04-01 19:34:30 -05:00
saveCmd := m.SavePreferencesCommand{
2018-03-22 06:37:35 -05:00
UserId: userID,
OrgId: orgID,
2018-11-12 13:01:53 -06:00
TeamId: teamId,
2016-04-01 19:34:30 -05:00
Theme: dtoCmd.Theme,
Timezone: dtoCmd.Timezone,
2018-03-22 16:13:46 -05:00
HomeDashboardId: dtoCmd.HomeDashboardID,
2016-04-01 19:34:30 -05:00
}
if err := bus.Dispatch(&saveCmd); err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to save preferences", err)
2016-04-01 19:34:30 -05:00
}
2018-03-22 16:13:46 -05:00
return Success("Preferences updated")
}
// GET /api/org/preferences
2018-03-07 10:54:50 -06:00
func GetOrgPreferences(c *m.ReqContext) Response {
2018-11-12 13:01:53 -06:00
return getPreferencesFor(c.OrgId, 0, 0)
}
// PUT /api/org/preferences
2018-03-07 10:54:50 -06:00
func UpdateOrgPreferences(c *m.ReqContext, dtoCmd dtos.UpdatePrefsCmd) Response {
2018-11-12 13:01:53 -06:00
return updatePreferencesFor(c.OrgId, 0, 0, &dtoCmd)
2016-04-01 19:34:30 -05:00
}