Commit 02221c99 by Torkel Ödegaard

Merge pull request #4268 from utkarshcmu/save-pref

Added APIs for Get and Save User Preferences
parents 00c70307 9c8d5082
......@@ -96,6 +96,7 @@ func Register(r *macaron.Macaron) {
r.Delete("/stars/dashboard/:id", wrap(UnstarDashboard))
r.Put("/password", bind(m.ChangeUserPasswordCommand{}), wrap(ChangeUserPassword))
r.Get("/quotas", wrap(GetUserQuotas))
r.Combo("/prefs").Get(GetUserPreferences).Put(bind(m.SavePreferencesCommand{}), wrap(SaveUserPreferences))
})
// users (admin permission required)
......
package api
import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models"
)
// PUT /api/user/prefs
func SaveUserPreferences(c *middleware.Context, cmd m.SavePreferencesCommand) Response {
cmd.PrefId = c.UserId
cmd.PrefType = `user`
if err := bus.Dispatch(&cmd); err != nil {
return ApiError(500, "Failed to saved user preferences", err)
}
return ApiSuccess("User preferences saved")
}
// GET /api/user/prefs
func GetUserPreferences(c *middleware.Context) {
query := m.GetPreferencesQuery{PrefId: c.UserId, PrefType: `user`}
if err := bus.Dispatch(&query); err != nil {
c.JsonApiErr(500, "Failed to get preferences for user", err)
}
dto := m.PreferencesDTO{
PrefId: query.Result.PrefId,
PrefType: query.Result.PrefType,
PrefData: query.Result.PrefData,
}
c.JSON(200, dto)
}
package models
import (
"errors"
)
// Typed errors
var (
ErrPreferencesNotFound = errors.New("Preferences not found")
)
type Preferences struct {
Id int64
PrefId int64
PrefType string
PrefData map[string]interface{}
}
// ---------------------
// QUERIES
type GetPreferencesQuery struct {
PrefId int64
PrefType string
Result *Preferences
}
// ---------------------
// COMMANDS
type SavePreferencesCommand struct {
PrefData map[string]interface{} `json:"prefData" binding:"Required"`
PrefId int64 `json:"-"`
PrefType string `json:"-"`
}
// ----------------------
// DTO & Projections
type PreferencesDTO struct {
PrefId int64 `json:"prefId"`
PrefType string `json:"prefType"`
PrefData map[string]interface{} `json:"prefData"`
}
package sqlstore
import (
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
)
func init() {
bus.AddHandler("sql", GetPreferences)
bus.AddHandler("sql", SavePreferences)
}
func GetPreferences(query *m.GetPreferencesQuery) error {
sql := `SELECT * FROM preferences WHERE pref_id = ? ` +
`AND pref_type = ?`
var prefResults = make([]m.Preferences, 0)
resultsErr := x.Sql(sql, query.PrefId, query.PrefType).Find(&prefResults)
if resultsErr != nil {
return resultsErr
}
if len(prefResults) > 0 {
query.Result = &prefResults[0]
} else {
query.Result = new(m.Preferences)
}
return nil
}
func SavePreferences(cmd *m.SavePreferencesCommand) error {
return inTransaction2(func(sess *session) error {
sql := `SELECT * FROM preferences WHERE pref_id = ? ` +
`AND pref_type = ?`
var prefResults = make([]m.Preferences, 0)
resultsErr := sess.Sql(sql, cmd.PrefId, cmd.PrefType).Find(&prefResults)
if resultsErr != nil {
return resultsErr
}
var savePref m.Preferences
var affectedRows int64
var saveErr error
if len(prefResults) == 0 {
savePref.PrefId = cmd.PrefId
savePref.PrefType = cmd.PrefType
savePref.PrefData = cmd.PrefData
affectedRows, saveErr = sess.Insert(&savePref)
} else {
savePref = prefResults[0]
savePref.PrefData = cmd.PrefData
affectedRows, saveErr = sess.Id(savePref.Id).Update(&savePref)
}
if affectedRows == 0 {
return m.ErrPreferencesNotFound
}
return saveErr
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment