From 9c8d508247e1c3b048edd6b68248ab5859d5b653 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Sun, 6 Mar 2016 12:32:22 -0800 Subject: [PATCH] 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"); - }); - }; - - }); -});