mirror of
https://github.com/grafana/grafana.git
synced 2025-02-11 16:15:42 -06:00
Implemented GetUserPreferences API
This commit is contained in:
parent
660d3fa1e9
commit
8f42bec270
@ -96,7 +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.Put("/prefs", bind(m.SavePreferencesCommand{}), wrap(SaveUserPreferences))
|
||||
r.Combo("/prefs").Get(GetUserPreferences).Put(bind(m.SavePreferencesCommand{}), wrap(SaveUserPreferences))
|
||||
})
|
||||
|
||||
// users (admin permission required)
|
||||
|
@ -19,3 +19,14 @@ func SaveUserPreferences(c *middleware.Context, cmd m.SavePreferencesCommand) Re
|
||||
return ApiSuccess("User preferences saved")
|
||||
|
||||
}
|
||||
|
||||
func GetUserPreferences(c *middleware.Context) Response {
|
||||
|
||||
query := m.GetPreferencesQuery{PrefId: c.UserId, PrefType: `user`}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return ApiError(500, "Failed to get user", err)
|
||||
}
|
||||
|
||||
return Json(200, query.Result)
|
||||
}
|
||||
|
@ -16,6 +16,16 @@ type Preferences struct {
|
||||
PrefData map[string]interface{}
|
||||
}
|
||||
|
||||
// ---------------------
|
||||
// QUERIES
|
||||
|
||||
type GetPreferencesQuery struct {
|
||||
PrefId int64
|
||||
PrefType string
|
||||
|
||||
Result PreferencesDTO
|
||||
}
|
||||
|
||||
// ---------------------
|
||||
// COMMANDS
|
||||
|
||||
@ -24,3 +34,12 @@ type SavePreferencesCommand struct {
|
||||
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"`
|
||||
}
|
||||
|
@ -6,9 +6,31 @@ import (
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
query.Result = m.PreferencesDTO{
|
||||
PrefId: prefResults[0].PrefId,
|
||||
PrefType: prefResults[0].PrefType,
|
||||
PrefData: prefResults[0].PrefData,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SavePreferences(cmd *m.SavePreferencesCommand) error {
|
||||
return inTransaction2(func(sess *session) error {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user