From 16b11662ed87c3d1c5113010a3758fb52e556766 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Fri, 19 May 2017 16:47:08 +0200 Subject: [PATCH] [BUGFIX] improve database access --- http/good_show.go | 4 ++-- http/status.go | 3 +-- webroot/static/js/item-add.controller.js | 8 ++++++-- webroot/static/js/item.controller.js | 8 +++++++- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/http/good_show.go b/http/good_show.go index 94a09fa..48f031b 100644 --- a/http/good_show.go +++ b/http/good_show.go @@ -28,7 +28,7 @@ func listGoods(w http.ResponseWriter, r *http.Request) { } log = log.WithField("productid", id) var list []*models.Good - result := &models.Good.FilterAvailable(database.Read).Where("product_id = ?", id).Find(&list) + result := (&models.Good{}).FilterAvailable(database.Read).Where("product_id = ?", id).Find(&list) if result.RowsAffected == 0 { log.Warn("no goods found") http.Error(w, "no goods found for this product", http.StatusNotFound) @@ -60,7 +60,7 @@ func getGoodAvailablityCount(w http.ResponseWriter, r *http.Request) (int, *logr return -1, log } var count float64 - &models.Good.FilterAvailable(database.Read).Where("product_id = ?", id).Count(&count) + (&models.Good{}).FilterAvailable(database.Read).Where("product_id = ?", id).Count(&count) return int(count), log } diff --git a/http/status.go b/http/status.go index 1a57940..9744fa2 100644 --- a/http/status.go +++ b/http/status.go @@ -13,10 +13,9 @@ import ( // Function to get the status of the microservice, the database and the goods func status(w http.ResponseWriter, r *http.Request) { log := logger.HTTP(r) - var good models.Good var count int64 var avg float64 - &models.Good.FilterAvailable(database.Read).Count(&count) + (&models.Good{}).FilterAvailable(database.Read).Count(&count) database.Read.Raw("SELECT avg(g.gcount) as avg FROM (select count(*) as gcount FROM good g WHERE deleted_at is NULL GROUP BY g.product_id) g").Row().Scan(&avg) lib.Write(w, map[string]interface{}{ "status": "running", diff --git a/webroot/static/js/item-add.controller.js b/webroot/static/js/item-add.controller.js index 291318a..8a758a6 100644 --- a/webroot/static/js/item-add.controller.js +++ b/webroot/static/js/item-add.controller.js @@ -15,8 +15,12 @@ angular.module('microStock') $http.post(config.store.goods.productById.replace("%d",$stateParams.productid)+'?count='+$scope.count,$scope.obj).then(function(){ $scope.obj = {}; $scope.msg = {type:'success',text:'Saved '+$scope.count+' good(s) from product '+$scope.product.title+'.'}; - }, function(){ - $scope.msg = {type:'error',text:'An error occurred while saving good(s) from product '+$scope.product.title+'.'}; + }, function(e){ + if(e.status == 403){ + $scope.msg = {type:'error',text:'You are not allowed to add goods, maybe you should login!'}; + }else{ + $scope.msg = {type:'error',text:'An error occurred while saving good(s): '+e.data}; + } }); }; }]); diff --git a/webroot/static/js/item.controller.js b/webroot/static/js/item.controller.js index 997e773..f029b3d 100644 --- a/webroot/static/js/item.controller.js +++ b/webroot/static/js/item.controller.js @@ -17,6 +17,12 @@ angular.module('microStock') } load(); $scope.delete = function(id){ - $http.delete(config.store.goods.productById.replace("%d",id)).then(load); + $http.delete(config.store.goods.productById.replace("%d",id)).then(load,function(e){ + if(e.status == 403) { + alert("You are not allowed to delete manuelle goods, maybe you should login!"); + }else{ + alert("A error occurred during deleting this good: "+e.data); + } + }); } }]);