grafana/pkg/api/preferences.go

101 lines
3.2 KiB
Go
Raw Normal View History

2016-03-06 05:47:39 -06:00
package api
import (
"context"
"net/http"
2016-04-01 19:34:30 -05:00
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
2016-03-06 05:47:39 -06:00
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/web"
2016-03-06 05:47:39 -06:00
)
const (
defaultTheme string = ""
darkTheme string = "dark"
lightTheme string = "light"
)
2016-03-17 01:22:27 -05:00
// POST /api/preferences/set-home-dash
func SetHomeDashboard(c *models.ReqContext) response.Response {
cmd := models.SavePreferencesCommand{}
if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
2016-03-17 01:35:06 -05:00
cmd.UserId = c.UserId
cmd.OrgId = c.OrgId
2016-03-17 01:22:27 -05:00
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
return response.Error(500, "Failed to set home dashboard", err)
2016-03-17 01:35:06 -05:00
}
2016-03-17 01:22:27 -05:00
return response.Success("Home dashboard set")
2016-03-17 01:22:27 -05:00
}
2016-04-01 19:34:30 -05:00
// GET /api/user/preferences
func (hs *HTTPServer) GetUserPreferences(c *models.ReqContext) response.Response {
return hs.getPreferencesFor(c.Req.Context(), c.OrgId, c.UserId, 0)
}
func (hs *HTTPServer) getPreferencesFor(ctx context.Context, orgID, userID, teamID int64) response.Response {
prefsQuery := models.GetPreferencesQuery{UserId: userID, OrgId: orgID, TeamId: teamID}
2016-04-01 19:34:30 -05:00
if err := hs.SQLStore.GetPreferences(ctx, &prefsQuery); err != nil {
return response.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,
WeekStart: prefsQuery.Result.WeekStart,
2016-04-01 19:34:30 -05:00
}
return response.JSON(200, &dto)
2016-04-01 19:34:30 -05:00
}
// PUT /api/user/preferences
func (hs *HTTPServer) UpdateUserPreferences(c *models.ReqContext) response.Response {
dtoCmd := dtos.UpdatePrefsCmd{}
if err := web.Bind(c.Req, &dtoCmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
return hs.updatePreferencesFor(c.Req.Context(), c.OrgId, c.UserId, 0, &dtoCmd)
}
func (hs *HTTPServer) updatePreferencesFor(ctx context.Context, orgID, userID, teamId int64, dtoCmd *dtos.UpdatePrefsCmd) response.Response {
if dtoCmd.Theme != lightTheme && dtoCmd.Theme != darkTheme && dtoCmd.Theme != defaultTheme {
return response.Error(400, "Invalid theme", nil)
}
saveCmd := models.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,
WeekStart: dtoCmd.WeekStart,
2018-03-22 16:13:46 -05:00
HomeDashboardId: dtoCmd.HomeDashboardID,
2016-04-01 19:34:30 -05:00
}
if err := hs.SQLStore.SavePreferences(ctx, &saveCmd); err != nil {
return response.Error(500, "Failed to save preferences", err)
2016-04-01 19:34:30 -05:00
}
return response.Success("Preferences updated")
}
// GET /api/org/preferences
func (hs *HTTPServer) GetOrgPreferences(c *models.ReqContext) response.Response {
return hs.getPreferencesFor(c.Req.Context(), c.OrgId, 0, 0)
}
// PUT /api/org/preferences
func (hs *HTTPServer) UpdateOrgPreferences(c *models.ReqContext) response.Response {
dtoCmd := dtos.UpdatePrefsCmd{}
if err := web.Bind(c.Req, &dtoCmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
return hs.updatePreferencesFor(c.Req.Context(), c.OrgId, 0, 0, &dtoCmd)
2016-04-01 19:34:30 -05:00
}