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 {
|
|
|
|
return ApiError(500, "Failed to set home dashboard", err)
|
|
|
|
}
|
2016-03-17 01:22:27 -05:00
|
|
|
|
2016-03-17 01:35:06 -05:00
|
|
|
return ApiSuccess("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 {
|
2016-04-02 15:54:06 -05:00
|
|
|
return getPreferencesFor(c.OrgId, c.UserId)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPreferencesFor(orgId int64, userId int64) Response {
|
|
|
|
prefsQuery := m.GetPreferencesQuery{UserId: userId, OrgId: orgId}
|
2016-04-01 19:34:30 -05:00
|
|
|
|
2016-04-02 15:54:06 -05:00
|
|
|
if err := bus.Dispatch(&prefsQuery); err != nil {
|
|
|
|
return ApiError(500, "Failed to get preferences", err)
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
2016-04-02 15:54:06 -05:00
|
|
|
dto := dtos.Prefs{
|
|
|
|
Theme: prefsQuery.Result.Theme,
|
|
|
|
HomeDashboardId: prefsQuery.Result.HomeDashboardId,
|
|
|
|
Timezone: prefsQuery.Result.Timezone,
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, &dto)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT /api/user/preferences
|
2018-03-07 10:54:50 -06:00
|
|
|
func UpdateUserPreferences(c *m.ReqContext, dtoCmd dtos.UpdatePrefsCmd) Response {
|
2016-04-02 15:54:06 -05:00
|
|
|
return updatePreferencesFor(c.OrgId, c.UserId, &dtoCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updatePreferencesFor(orgId int64, userId int64, dtoCmd *dtos.UpdatePrefsCmd) Response {
|
2016-04-01 19:34:30 -05:00
|
|
|
saveCmd := m.SavePreferencesCommand{
|
2016-04-02 15:54:06 -05:00
|
|
|
UserId: userId,
|
|
|
|
OrgId: orgId,
|
2016-04-01 19:34:30 -05:00
|
|
|
Theme: dtoCmd.Theme,
|
|
|
|
Timezone: dtoCmd.Timezone,
|
|
|
|
HomeDashboardId: dtoCmd.HomeDashboardId,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&saveCmd); err != nil {
|
2016-04-02 15:54:06 -05:00
|
|
|
return ApiError(500, "Failed to save preferences", err)
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
2016-04-02 15:54:06 -05:00
|
|
|
return ApiSuccess("Preferences updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/org/preferences
|
2018-03-07 10:54:50 -06:00
|
|
|
func GetOrgPreferences(c *m.ReqContext) Response {
|
2016-04-02 15:54:06 -05:00
|
|
|
return getPreferencesFor(c.OrgId, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT /api/org/preferences
|
2018-03-07 10:54:50 -06:00
|
|
|
func UpdateOrgPreferences(c *m.ReqContext, dtoCmd dtos.UpdatePrefsCmd) Response {
|
2016-04-02 15:54:06 -05:00
|
|
|
return updatePreferencesFor(c.OrgId, 0, &dtoCmd)
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|