genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[BUGFIX] improve database access

This commit is contained in:
Martin Geno 2017-05-19 16:47:08 +02:00
parent 4ba6bfa009
commit 16b11662ed
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
4 changed files with 16 additions and 7 deletions

View File

@ -28,7 +28,7 @@ func listGoods(w http.ResponseWriter, r *http.Request) {
} }
log = log.WithField("productid", id) log = log.WithField("productid", id)
var list []*models.Good 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 { if result.RowsAffected == 0 {
log.Warn("no goods found") log.Warn("no goods found")
http.Error(w, "no goods found for this product", http.StatusNotFound) 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 return -1, log
} }
var count float64 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 return int(count), log
} }

View File

@ -13,10 +13,9 @@ import (
// Function to get the status of the microservice, the database and the goods // Function to get the status of the microservice, the database and the goods
func status(w http.ResponseWriter, r *http.Request) { func status(w http.ResponseWriter, r *http.Request) {
log := logger.HTTP(r) log := logger.HTTP(r)
var good models.Good
var count int64 var count int64
var avg float64 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) 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{}{ lib.Write(w, map[string]interface{}{
"status": "running", "status": "running",

View File

@ -15,8 +15,12 @@ angular.module('microStock')
$http.post(config.store.goods.productById.replace("%d",$stateParams.productid)+'?count='+$scope.count,$scope.obj).then(function(){ $http.post(config.store.goods.productById.replace("%d",$stateParams.productid)+'?count='+$scope.count,$scope.obj).then(function(){
$scope.obj = {}; $scope.obj = {};
$scope.msg = {type:'success',text:'Saved '+$scope.count+' good(s) from product '+$scope.product.title+'.'}; $scope.msg = {type:'success',text:'Saved '+$scope.count+' good(s) from product '+$scope.product.title+'.'};
}, function(){ }, function(e){
$scope.msg = {type:'error',text:'An error occurred while saving good(s) from product '+$scope.product.title+'.'}; 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};
}
}); });
}; };
}]); }]);

View File

@ -17,6 +17,12 @@ angular.module('microStock')
} }
load(); load();
$scope.delete = function(id){ $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);
}
});
} }
}]); }]);