From cf926134ef03d4a1ccbb44178660181ff40b8fd5 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Sat, 5 Mar 2016 13:15:49 -0800 Subject: [PATCH 1/5] Added savePreferencesAPI --- pkg/api/api.go | 1 + pkg/api/user.go | 21 ++++++++++++++++++++- pkg/models/preferences.go | 28 ++++++++++++++++++++++++++++ pkg/services/sqlstore/user.go | 25 +++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 pkg/models/preferences.go diff --git a/pkg/api/api.go b/pkg/api/api.go index ed029a5171a..3f805cd9c98 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -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.Put("/prefs", bind(m.SavePreferenceCommand{}), wrap(SaveUserPreferences)) }) // users (admin permission required) diff --git a/pkg/api/user.go b/pkg/api/user.go index 5af243eeb22..0f972431426 100644 --- a/pkg/api/user.go +++ b/pkg/api/user.go @@ -5,6 +5,7 @@ import ( "github.com/grafana/grafana/pkg/middleware" m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/util" + "github.com/grafana/grafana/pkg/log" ) // GET /api/user (current authenticated user) @@ -110,7 +111,10 @@ func UserSetUsingOrg(c *middleware.Context) Response { } func ChangeUserPassword(c *middleware.Context, cmd m.ChangeUserPasswordCommand) Response { - userQuery := m.GetUserByIdQuery{Id: c.UserId} + + log.Info("%v", cmd) + + userQuery := m.GetUserByIdQuery{Id: c.UserId} if err := bus.Dispatch(&userQuery); err != nil { return ApiError(500, "Could not read user from database", err) @@ -144,3 +148,18 @@ func SearchUsers(c *middleware.Context) Response { return Json(200, query.Result) } + +func SaveUserPreferences(c *middleware.Context, cmd m.SavePreferenceCommand) Response { + + log.Info("%v", cmd.PrefData) + + 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") + +} diff --git a/pkg/models/preferences.go b/pkg/models/preferences.go new file mode 100644 index 00000000000..cdaab0f6af8 --- /dev/null +++ b/pkg/models/preferences.go @@ -0,0 +1,28 @@ +package models + +import ( + "errors" +) + +// Typed errors +var ( + ErrPreferenceNotFound = errors.New("Preference not found") +) + +type Preference struct { + Id int64 + PrefId int64 + PrefType string + PrefData map[string]interface{} +} + +// --------------------- +// COMMANDS + +type SavePreferenceCommand struct { + + PrefData map[string]interface{} `json:"prefData"` + PrefId int64 `json:"-"` + PrefType string `json:"-"` + +} diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index 96b8c24b8fc..a5c992d10b6 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -11,6 +11,7 @@ import ( m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" + "github.com/grafana/grafana/pkg/log" ) func init() { @@ -27,6 +28,7 @@ func init() { bus.AddHandler("sql", DeleteUser) bus.AddHandler("sql", SetUsingOrg) bus.AddHandler("sql", UpdateUserPermissions) + bus.AddHandler("sql", SaveUserPreferences) } func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *session) (int64, error) { @@ -346,3 +348,26 @@ func UpdateUserPermissions(cmd *m.UpdateUserPermissionsCommand) error { return err }) } + +func SaveUserPreferences(cmd *m.SavePreferenceCommand) error { + return inTransaction2(func(sess *session) error { + + log.Info("%v", cmd) + + pref := m.Preference{ + PrefId: cmd.PrefId, + PrefType: cmd.PrefType, + PrefData: cmd.PrefData, + } + + sess.Table("preferences").Where("pref_id", pref.PrefId).And("pref_type", pref.PrefType) + + if _, err := sess.Update(&pref); err != nil { + return err + } + + log.Info("%v", pref) + + return nil + }) +} From 1ef332e82ceacbfa0ab7ff1d038016f6e282353e Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Sat, 5 Mar 2016 23:44:56 -0800 Subject: [PATCH 2/5] preferences UI poc --- pkg/api/user.go | 3 -- .../app/core/components/sidemenu/sidemenu.ts | 1 + public/app/core/routes/routes.ts | 4 +++ public/app/features/all.js | 1 + .../profile/partials/preferences.html | 32 +++++++++++++++++++ .../features/profile/partials/profile.html | 2 +- .../app/features/profile/preferencesCtrl.js | 25 +++++++++++++++ 7 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 public/app/features/profile/partials/preferences.html create mode 100644 public/app/features/profile/preferencesCtrl.js diff --git a/pkg/api/user.go b/pkg/api/user.go index 0f972431426..d4545a7c709 100644 --- a/pkg/api/user.go +++ b/pkg/api/user.go @@ -111,9 +111,6 @@ func UserSetUsingOrg(c *middleware.Context) Response { } func ChangeUserPassword(c *middleware.Context, cmd m.ChangeUserPasswordCommand) Response { - - log.Info("%v", cmd) - userQuery := m.GetUserByIdQuery{Id: c.UserId} if err := bus.Dispatch(&userQuery); err != nil { diff --git a/public/app/core/components/sidemenu/sidemenu.ts b/public/app/core/components/sidemenu/sidemenu.ts index 4c0e100f85c..f68edf8fca5 100644 --- a/public/app/core/components/sidemenu/sidemenu.ts +++ b/public/app/core/components/sidemenu/sidemenu.ts @@ -39,6 +39,7 @@ export class SideMenuCtrl { this.orgMenu = [ {section: 'You', cssClass: 'dropdown-menu-title'}, {text: 'Profile', url: this.getUrl('/profile')}, + {text: 'Preferences', url: this.getUrl('/preferences')}, ]; if (this.isSignedIn) { diff --git a/public/app/core/routes/routes.ts b/public/app/core/routes/routes.ts index 6e3af683c82..334800c22f5 100644 --- a/public/app/core/routes/routes.ts +++ b/public/app/core/routes/routes.ts @@ -89,6 +89,10 @@ function setupAngularRoutes($routeProvider, $locationProvider) { templateUrl: 'public/app/features/profile/partials/profile.html', controller : 'ProfileCtrl', }) + .when('/preferences', { + templateUrl: 'public/app/features/profile/partials/preferences.html', + controller : 'PreferencesCtrl', + }) .when('/profile/password', { templateUrl: 'public/app/features/profile/partials/password.html', controller : 'ChangePasswordCtrl', diff --git a/public/app/features/all.js b/public/app/features/all.js index c110bcff7cd..94da3809ae7 100644 --- a/public/app/features/all.js +++ b/public/app/features/all.js @@ -10,5 +10,6 @@ define([ './profile/profileCtrl', './profile/changePasswordCtrl', './profile/selectOrgCtrl', + './profile/preferencesCtrl', './styleguide/styleguide', ], function () {}); diff --git a/public/app/features/profile/partials/preferences.html b/public/app/features/profile/partials/preferences.html new file mode 100644 index 00000000000..093163dde32 --- /dev/null +++ b/public/app/features/profile/partials/preferences.html @@ -0,0 +1,32 @@ + + + +
+ + +
+
+ Home Dashboard + +
+ +
+ Time Range + +
+ +
+ Theme + +
+ +
+ + Cancel +
+
+ +
+ diff --git a/public/app/features/profile/partials/profile.html b/public/app/features/profile/partials/profile.html index 042344eeac6..dd5afebeaac 100644 --- a/public/app/features/profile/partials/profile.html +++ b/public/app/features/profile/partials/profile.html @@ -7,7 +7,7 @@
-

Preferences

+

Information

Name diff --git a/public/app/features/profile/preferencesCtrl.js b/public/app/features/profile/preferencesCtrl.js new file mode 100644 index 00000000000..00e7d59182c --- /dev/null +++ b/public/app/features/profile/preferencesCtrl.js @@ -0,0 +1,25 @@ +define([ + 'angular', + 'app/core/config', +], +function (angular) { + 'use strict'; + + var module = angular.module('grafana.controllers'); + + module.controller('PreferencesCtrl', function($scope, backendSrv, $location) { + + $scope.command = {}; + + $scope.setUserPreferences = function() { + if (!$scope.userForm.$valid) { return; } + + console.log($scope.command); + + backendSrv.put('/api/user/prefs', $scope.command).then(function() { + $location.path("profile"); + }); + }; + + }); +}); From 660d3fa1e9a75e79468eb66c2dd14ff3f282e318 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Sun, 6 Mar 2016 03:47:39 -0800 Subject: [PATCH 3/5] Implemented savePreferences API --- pkg/api/api.go | 2 +- pkg/api/preferences.go | 21 +++++++++++ pkg/api/user.go | 18 +-------- pkg/models/preferences.go | 18 ++++----- pkg/services/sqlstore/preferences.go | 37 +++++++++++++++++++ pkg/services/sqlstore/user.go | 25 ------------- .../profile/partials/preferences.html | 6 +-- .../app/features/profile/preferencesCtrl.js | 4 +- 8 files changed, 73 insertions(+), 58 deletions(-) create mode 100644 pkg/api/preferences.go create mode 100644 pkg/services/sqlstore/preferences.go diff --git a/pkg/api/api.go b/pkg/api/api.go index 3f805cd9c98..a3d206c4956 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -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.SavePreferenceCommand{}), wrap(SaveUserPreferences)) + r.Put("/prefs", bind(m.SavePreferencesCommand{}), wrap(SaveUserPreferences)) }) // users (admin permission required) diff --git a/pkg/api/preferences.go b/pkg/api/preferences.go new file mode 100644 index 00000000000..849ed4aa8aa --- /dev/null +++ b/pkg/api/preferences.go @@ -0,0 +1,21 @@ +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") + +} diff --git a/pkg/api/user.go b/pkg/api/user.go index d4545a7c709..5af243eeb22 100644 --- a/pkg/api/user.go +++ b/pkg/api/user.go @@ -5,7 +5,6 @@ import ( "github.com/grafana/grafana/pkg/middleware" m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/util" - "github.com/grafana/grafana/pkg/log" ) // GET /api/user (current authenticated user) @@ -111,7 +110,7 @@ func UserSetUsingOrg(c *middleware.Context) Response { } func ChangeUserPassword(c *middleware.Context, cmd m.ChangeUserPasswordCommand) Response { - userQuery := m.GetUserByIdQuery{Id: c.UserId} + userQuery := m.GetUserByIdQuery{Id: c.UserId} if err := bus.Dispatch(&userQuery); err != nil { return ApiError(500, "Could not read user from database", err) @@ -145,18 +144,3 @@ func SearchUsers(c *middleware.Context) Response { return Json(200, query.Result) } - -func SaveUserPreferences(c *middleware.Context, cmd m.SavePreferenceCommand) Response { - - log.Info("%v", cmd.PrefData) - - 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") - -} diff --git a/pkg/models/preferences.go b/pkg/models/preferences.go index cdaab0f6af8..8476ec49429 100644 --- a/pkg/models/preferences.go +++ b/pkg/models/preferences.go @@ -9,20 +9,18 @@ var ( ErrPreferenceNotFound = errors.New("Preference not found") ) -type Preference struct { - Id int64 - PrefId int64 - PrefType string - PrefData map[string]interface{} +type Preferences struct { + Id int64 + PrefId int64 + PrefType string + PrefData map[string]interface{} } // --------------------- // COMMANDS -type SavePreferenceCommand struct { - - PrefData map[string]interface{} `json:"prefData"` - PrefId int64 `json:"-"` +type SavePreferencesCommand struct { + PrefData map[string]interface{} `json:"prefData" binding:"Required"` + PrefId int64 `json:"-"` PrefType string `json:"-"` - } diff --git a/pkg/services/sqlstore/preferences.go b/pkg/services/sqlstore/preferences.go new file mode 100644 index 00000000000..756048360ec --- /dev/null +++ b/pkg/services/sqlstore/preferences.go @@ -0,0 +1,37 @@ +package sqlstore + +import ( + "github.com/grafana/grafana/pkg/bus" + m "github.com/grafana/grafana/pkg/models" +) + +func init() { + bus.AddHandler("sql", SavePreferences) +} + +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 matchedPref m.Preferences + matchedPref = prefResults[0] + matchedPref.PrefData = cmd.PrefData + affectedRows, updateErr := sess.Id(matchedPref.Id).Update(&matchedPref) + + if affectedRows == 0 { + return m.ErrPreferenceNotFound + } + + return updateErr + }) +} diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index a5c992d10b6..96b8c24b8fc 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -11,7 +11,6 @@ import ( m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" - "github.com/grafana/grafana/pkg/log" ) func init() { @@ -28,7 +27,6 @@ func init() { bus.AddHandler("sql", DeleteUser) bus.AddHandler("sql", SetUsingOrg) bus.AddHandler("sql", UpdateUserPermissions) - bus.AddHandler("sql", SaveUserPreferences) } func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *session) (int64, error) { @@ -348,26 +346,3 @@ func UpdateUserPermissions(cmd *m.UpdateUserPermissionsCommand) error { return err }) } - -func SaveUserPreferences(cmd *m.SavePreferenceCommand) error { - return inTransaction2(func(sess *session) error { - - log.Info("%v", cmd) - - pref := m.Preference{ - PrefId: cmd.PrefId, - PrefType: cmd.PrefType, - PrefData: cmd.PrefData, - } - - sess.Table("preferences").Where("pref_id", pref.PrefId).And("pref_type", pref.PrefType) - - if _, err := sess.Update(&pref); err != nil { - return err - } - - log.Info("%v", pref) - - return nil - }) -} diff --git a/public/app/features/profile/partials/preferences.html b/public/app/features/profile/partials/preferences.html index 093163dde32..39cf64957cb 100644 --- a/public/app/features/profile/partials/preferences.html +++ b/public/app/features/profile/partials/preferences.html @@ -9,17 +9,17 @@
Home Dashboard - +
Time Range - +
Theme - +
diff --git a/public/app/features/profile/preferencesCtrl.js b/public/app/features/profile/preferencesCtrl.js index 00e7d59182c..f9d1e4da4f0 100644 --- a/public/app/features/profile/preferencesCtrl.js +++ b/public/app/features/profile/preferencesCtrl.js @@ -9,14 +9,14 @@ function (angular) { module.controller('PreferencesCtrl', function($scope, backendSrv, $location) { - $scope.command = {}; + $scope.prefData = {}; $scope.setUserPreferences = function() { if (!$scope.userForm.$valid) { return; } console.log($scope.command); - backendSrv.put('/api/user/prefs', $scope.command).then(function() { + backendSrv.put('/api/user/prefs', { prefData : $scope.prefData }).then(function() { $location.path("profile"); }); }; From 8f42bec270428bd1714258dd7cd0d8e60d7d87af Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Sun, 6 Mar 2016 11:42:15 -0800 Subject: [PATCH 4/5] Implemented GetUserPreferences API --- pkg/api/api.go | 2 +- pkg/api/preferences.go | 11 +++++++++++ pkg/models/preferences.go | 19 +++++++++++++++++++ pkg/services/sqlstore/preferences.go | 22 ++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index a3d206c4956..c02629ddd8f 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -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) diff --git a/pkg/api/preferences.go b/pkg/api/preferences.go index 849ed4aa8aa..3a72467acb0 100644 --- a/pkg/api/preferences.go +++ b/pkg/api/preferences.go @@ -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) +} diff --git a/pkg/models/preferences.go b/pkg/models/preferences.go index 8476ec49429..d54bbcda9bc 100644 --- a/pkg/models/preferences.go +++ b/pkg/models/preferences.go @@ -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"` +} diff --git a/pkg/services/sqlstore/preferences.go b/pkg/services/sqlstore/preferences.go index 756048360ec..7d210749a7d 100644 --- a/pkg/services/sqlstore/preferences.go +++ b/pkg/services/sqlstore/preferences.go @@ -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 { From 9c8d508247e1c3b048edd6b68248ab5859d5b653 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Sun, 6 Mar 2016 12:32:22 -0800 Subject: [PATCH 5/5] Made API handling better, removed unused components --- pkg/api/preferences.go | 13 ++++++-- pkg/models/preferences.go | 4 +-- pkg/services/sqlstore/preferences.go | 33 ++++++++++++------- .../app/core/components/sidemenu/sidemenu.ts | 1 - public/app/core/routes/routes.ts | 4 --- public/app/features/all.js | 1 - .../profile/partials/preferences.html | 32 ------------------ .../features/profile/partials/profile.html | 2 +- .../app/features/profile/preferencesCtrl.js | 25 -------------- 9 files changed, 35 insertions(+), 80 deletions(-) delete mode 100644 public/app/features/profile/partials/preferences.html delete mode 100644 public/app/features/profile/preferencesCtrl.js diff --git a/pkg/api/preferences.go b/pkg/api/preferences.go index 3a72467acb0..b39d0446a9b 100644 --- a/pkg/api/preferences.go +++ b/pkg/api/preferences.go @@ -20,13 +20,20 @@ func SaveUserPreferences(c *middleware.Context, cmd m.SavePreferencesCommand) Re } -func GetUserPreferences(c *middleware.Context) Response { +// GET /api/user/prefs +func GetUserPreferences(c *middleware.Context) { query := m.GetPreferencesQuery{PrefId: c.UserId, PrefType: `user`} if err := bus.Dispatch(&query); err != nil { - return ApiError(500, "Failed to get user", err) + c.JsonApiErr(500, "Failed to get preferences for user", err) } - return Json(200, query.Result) + dto := m.PreferencesDTO{ + PrefId: query.Result.PrefId, + PrefType: query.Result.PrefType, + PrefData: query.Result.PrefData, + } + + c.JSON(200, dto) } diff --git a/pkg/models/preferences.go b/pkg/models/preferences.go index d54bbcda9bc..237e577dbc9 100644 --- a/pkg/models/preferences.go +++ b/pkg/models/preferences.go @@ -6,7 +6,7 @@ import ( // Typed errors var ( - ErrPreferenceNotFound = errors.New("Preference not found") + ErrPreferencesNotFound = errors.New("Preferences not found") ) type Preferences struct { @@ -23,7 +23,7 @@ type GetPreferencesQuery struct { PrefId int64 PrefType string - Result PreferencesDTO + Result *Preferences } // --------------------- diff --git a/pkg/services/sqlstore/preferences.go b/pkg/services/sqlstore/preferences.go index 7d210749a7d..b03463f8089 100644 --- a/pkg/services/sqlstore/preferences.go +++ b/pkg/services/sqlstore/preferences.go @@ -22,10 +22,11 @@ func GetPreferences(query *m.GetPreferencesQuery) error { if resultsErr != nil { return resultsErr } - query.Result = m.PreferencesDTO{ - PrefId: prefResults[0].PrefId, - PrefType: prefResults[0].PrefType, - PrefData: prefResults[0].PrefData, + + if len(prefResults) > 0 { + query.Result = &prefResults[0] + } else { + query.Result = new(m.Preferences) } return nil @@ -45,15 +46,25 @@ func SavePreferences(cmd *m.SavePreferencesCommand) error { return resultsErr } - var matchedPref m.Preferences - matchedPref = prefResults[0] - matchedPref.PrefData = cmd.PrefData - affectedRows, updateErr := sess.Id(matchedPref.Id).Update(&matchedPref) + var savePref m.Preferences + var affectedRows int64 + var saveErr error - if affectedRows == 0 { - return m.ErrPreferenceNotFound + 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) } - return updateErr + if affectedRows == 0 { + return m.ErrPreferencesNotFound + } + + return saveErr }) } diff --git a/public/app/core/components/sidemenu/sidemenu.ts b/public/app/core/components/sidemenu/sidemenu.ts index f68edf8fca5..4c0e100f85c 100644 --- a/public/app/core/components/sidemenu/sidemenu.ts +++ b/public/app/core/components/sidemenu/sidemenu.ts @@ -39,7 +39,6 @@ export class SideMenuCtrl { this.orgMenu = [ {section: 'You', cssClass: 'dropdown-menu-title'}, {text: 'Profile', url: this.getUrl('/profile')}, - {text: 'Preferences', url: this.getUrl('/preferences')}, ]; if (this.isSignedIn) { diff --git a/public/app/core/routes/routes.ts b/public/app/core/routes/routes.ts index 334800c22f5..6e3af683c82 100644 --- a/public/app/core/routes/routes.ts +++ b/public/app/core/routes/routes.ts @@ -89,10 +89,6 @@ function setupAngularRoutes($routeProvider, $locationProvider) { templateUrl: 'public/app/features/profile/partials/profile.html', controller : 'ProfileCtrl', }) - .when('/preferences', { - templateUrl: 'public/app/features/profile/partials/preferences.html', - controller : 'PreferencesCtrl', - }) .when('/profile/password', { templateUrl: 'public/app/features/profile/partials/password.html', controller : 'ChangePasswordCtrl', diff --git a/public/app/features/all.js b/public/app/features/all.js index 94da3809ae7..c110bcff7cd 100644 --- a/public/app/features/all.js +++ b/public/app/features/all.js @@ -10,6 +10,5 @@ define([ './profile/profileCtrl', './profile/changePasswordCtrl', './profile/selectOrgCtrl', - './profile/preferencesCtrl', './styleguide/styleguide', ], function () {}); diff --git a/public/app/features/profile/partials/preferences.html b/public/app/features/profile/partials/preferences.html deleted file mode 100644 index 39cf64957cb..00000000000 --- a/public/app/features/profile/partials/preferences.html +++ /dev/null @@ -1,32 +0,0 @@ - - - -
- - - -
- Home Dashboard - -
- -
- Time Range - -
- -
- Theme - -
- -
- - Cancel -
- - -
- diff --git a/public/app/features/profile/partials/profile.html b/public/app/features/profile/partials/profile.html index dd5afebeaac..042344eeac6 100644 --- a/public/app/features/profile/partials/profile.html +++ b/public/app/features/profile/partials/profile.html @@ -7,7 +7,7 @@
-

Information

+

Preferences

Name diff --git a/public/app/features/profile/preferencesCtrl.js b/public/app/features/profile/preferencesCtrl.js deleted file mode 100644 index f9d1e4da4f0..00000000000 --- a/public/app/features/profile/preferencesCtrl.js +++ /dev/null @@ -1,25 +0,0 @@ -define([ - 'angular', - 'app/core/config', -], -function (angular) { - 'use strict'; - - var module = angular.module('grafana.controllers'); - - module.controller('PreferencesCtrl', function($scope, backendSrv, $location) { - - $scope.prefData = {}; - - $scope.setUserPreferences = function() { - if (!$scope.userForm.$valid) { return; } - - console.log($scope.command); - - backendSrv.put('/api/user/prefs', { prefData : $scope.prefData }).then(function() { - $location.path("profile"); - }); - }; - - }); -});