2016-03-06 05:47:39 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-11-02 07:41:45 -05:00
|
|
|
"context"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2021-11-02 07:41:45 -05:00
|
|
|
|
2016-04-01 19:34:30 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2016-03-06 05:47:39 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-11-29 03:18:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2016-03-06 05:47:39 -06:00
|
|
|
)
|
|
|
|
|
2021-08-24 01:41:15 -05:00
|
|
|
const (
|
|
|
|
defaultTheme string = ""
|
|
|
|
darkTheme string = "dark"
|
|
|
|
lightTheme string = "light"
|
|
|
|
)
|
|
|
|
|
2016-03-17 01:22:27 -05:00
|
|
|
// POST /api/preferences/set-home-dash
|
2021-11-29 03:18:01 -06:00
|
|
|
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
|
|
|
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
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
|
|
|
|
2021-01-15 07:43:20 -06: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
|
2021-11-02 07:41:45 -05:00
|
|
|
func (hs *HTTPServer) GetUserPreferences(c *models.ReqContext) response.Response {
|
|
|
|
return hs.getPreferencesFor(c.Req.Context(), c.OrgId, c.UserId, 0)
|
2016-04-02 15:54:06 -05:00
|
|
|
}
|
|
|
|
|
2021-11-02 07:41:45 -05:00
|
|
|
func (hs *HTTPServer) getPreferencesFor(ctx context.Context, orgID, userID, teamID int64) response.Response {
|
2020-03-04 05:57:20 -06:00
|
|
|
prefsQuery := models.GetPreferencesQuery{UserId: userID, OrgId: orgID, TeamId: teamID}
|
2016-04-01 19:34:30 -05:00
|
|
|
|
2021-11-02 07:41:45 -05:00
|
|
|
if err := hs.SQLStore.GetPreferences(ctx, &prefsQuery); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(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,
|
2018-03-22 16:13:46 -05:00
|
|
|
HomeDashboardID: prefsQuery.Result.HomeDashboardId,
|
2016-04-02 15:54:06 -05:00
|
|
|
Timezone: prefsQuery.Result.Timezone,
|
2021-10-18 08:27:14 -05:00
|
|
|
WeekStart: prefsQuery.Result.WeekStart,
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, &dto)
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PUT /api/user/preferences
|
2021-11-29 03:18:01 -06:00
|
|
|
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)
|
|
|
|
}
|
2021-11-02 07:41:45 -05:00
|
|
|
return hs.updatePreferencesFor(c.Req.Context(), c.OrgId, c.UserId, 0, &dtoCmd)
|
2016-04-02 15:54:06 -05:00
|
|
|
}
|
|
|
|
|
2021-11-02 07:41:45 -05:00
|
|
|
func (hs *HTTPServer) updatePreferencesFor(ctx context.Context, orgID, userID, teamId int64, dtoCmd *dtos.UpdatePrefsCmd) response.Response {
|
2021-08-24 01:41:15 -05:00
|
|
|
if dtoCmd.Theme != lightTheme && dtoCmd.Theme != darkTheme && dtoCmd.Theme != defaultTheme {
|
|
|
|
return response.Error(400, "Invalid theme", nil)
|
|
|
|
}
|
2020-03-04 05:57:20 -06:00
|
|
|
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,
|
2021-10-18 08:27:14 -05:00
|
|
|
WeekStart: dtoCmd.WeekStart,
|
2018-03-22 16:13:46 -05:00
|
|
|
HomeDashboardId: dtoCmd.HomeDashboardID,
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
2021-11-02 07:41:45 -05:00
|
|
|
if err := hs.SQLStore.SavePreferences(ctx, &saveCmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to save preferences", err)
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Preferences updated")
|
2016-04-02 15:54:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/org/preferences
|
2021-11-02 07:41:45 -05:00
|
|
|
func (hs *HTTPServer) GetOrgPreferences(c *models.ReqContext) response.Response {
|
|
|
|
return hs.getPreferencesFor(c.Req.Context(), c.OrgId, 0, 0)
|
2016-04-02 15:54:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PUT /api/org/preferences
|
2021-11-29 03:18:01 -06:00
|
|
|
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)
|
|
|
|
}
|
2021-11-02 07:41:45 -05:00
|
|
|
return hs.updatePreferencesFor(c.Req.Context(), c.OrgId, 0, 0, &dtoCmd)
|
2016-04-01 19:34:30 -05:00
|
|
|
}
|