preferences.go 1.94 KB
Newer Older
1 2 3
package api

import (
Torkel Ödegaard committed
4
	"github.com/grafana/grafana/pkg/api/dtos"
5 6 7 8
	"github.com/grafana/grafana/pkg/bus"
	m "github.com/grafana/grafana/pkg/models"
)

9
// POST /api/preferences/set-home-dash
10
func SetHomeDashboard(c *m.ReqContext, cmd m.SavePreferencesCommand) Response {
11

utkarshcmu committed
12 13
	cmd.UserId = c.UserId
	cmd.OrgId = c.OrgId
14

utkarshcmu committed
15
	if err := bus.Dispatch(&cmd); err != nil {
16
		return Error(500, "Failed to set home dashboard", err)
utkarshcmu committed
17
	}
18

19
	return Success("Home dashboard set")
20
}
Torkel Ödegaard committed
21 22

// GET /api/user/preferences
23
func GetUserPreferences(c *m.ReqContext) Response {
24
	return getPreferencesFor(c.OrgId, c.UserId, 0)
25 26
}

27 28
func getPreferencesFor(orgID, userID, teamID int64) Response {
	prefsQuery := m.GetPreferencesQuery{UserId: userID, OrgId: orgID, TeamId: teamID}
Torkel Ödegaard committed
29

30
	if err := bus.Dispatch(&prefsQuery); err != nil {
31
		return Error(500, "Failed to get preferences", err)
Torkel Ödegaard committed
32 33
	}

34 35
	dto := dtos.Prefs{
		Theme:           prefsQuery.Result.Theme,
36
		HomeDashboardID: prefsQuery.Result.HomeDashboardId,
37
		Timezone:        prefsQuery.Result.Timezone,
Torkel Ödegaard committed
38 39
	}

40
	return JSON(200, &dto)
Torkel Ödegaard committed
41 42 43
}

// PUT /api/user/preferences
44
func UpdateUserPreferences(c *m.ReqContext, dtoCmd dtos.UpdatePrefsCmd) Response {
45
	return updatePreferencesFor(c.OrgId, c.UserId, 0, &dtoCmd)
46 47
}

48
func updatePreferencesFor(orgID, userID, teamId int64, dtoCmd *dtos.UpdatePrefsCmd) Response {
Torkel Ödegaard committed
49
	saveCmd := m.SavePreferencesCommand{
50 51
		UserId:          userID,
		OrgId:           orgID,
52
		TeamId:          teamId,
Torkel Ödegaard committed
53 54
		Theme:           dtoCmd.Theme,
		Timezone:        dtoCmd.Timezone,
55
		HomeDashboardId: dtoCmd.HomeDashboardID,
Torkel Ödegaard committed
56 57 58
	}

	if err := bus.Dispatch(&saveCmd); err != nil {
59
		return Error(500, "Failed to save preferences", err)
Torkel Ödegaard committed
60 61
	}

62
	return Success("Preferences updated")
63 64 65
}

// GET /api/org/preferences
66
func GetOrgPreferences(c *m.ReqContext) Response {
67
	return getPreferencesFor(c.OrgId, 0, 0)
68 69 70
}

// PUT /api/org/preferences
71
func UpdateOrgPreferences(c *m.ReqContext, dtoCmd dtos.UpdatePrefsCmd) Response {
72
	return updatePreferencesFor(c.OrgId, 0, 0, &dtoCmd)
Torkel Ödegaard committed
73
}