diff --git a/http/good.go b/http/good.go index ed71931..9123b4f 100644 --- a/http/good.go +++ b/http/good.go @@ -13,11 +13,19 @@ import ( logger "github.com/genofire/hs_master-kss-monolith/lib/log" "github.com/genofire/hs_master-kss-monolith/models" "github.com/genofire/hs_master-kss-monolith/runtime" + "github.com/jinzhu/gorm" ) // Function to add goods to the stock func addGood(w http.ResponseWriter, r *http.Request) { log := logger.HTTP(r) + + countStr := r.URL.Query().Get("count") + count, err := strconv.Atoi(countStr) + if err != nil { + count = 0 + } + id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64) if err != nil { log.Warn("false productid format") @@ -47,7 +55,16 @@ func addGood(w http.ResponseWriter, r *http.Request) { obj.ProductID = id - db := database.Write.Create(&obj) + var db *gorm.DB + if count > 0 { + for i := 0; i < count; i++ { + db = database.Write.Create(&obj) + obj.ID = 0 + } + } else { + db = database.Write.Create(&obj) + } + if db.Error != nil { log.Error("database not able to write", db.Error) http.Error(w, "the product could not be written into the database", http.StatusInternalServerError) diff --git a/http/good_test.go b/http/good_test.go index b44e914..d0db782 100644 --- a/http/good_test.go +++ b/http/good_test.go @@ -36,11 +36,24 @@ func TestAddGood(t *testing.T) { _, w = session.JSONRequest("POST", "/api/good/7", good) assertion.Equal(http.StatusNotFound, w.StatusCode) - _, w = session.JSONRequest("POST", "/api/good/1", true) + _, w = session.JSONRequest("POST", "/api/good/2", true) assertion.Equal(http.StatusBadRequest, w.StatusCode) - _, w = session.JSONRequest("POST", "/api/good/1", good) + _, w = session.JSONRequest("POST", "/api/good/2", good) assertion.Equal(http.StatusOK, w.StatusCode) + var count int + database.Read.Model(&good).Where("product_id", 2).Count(&count) + assertion.Equal(1, count) + + good = models.Good{ + ProductID: 3, + Comment: "blub", + } + + _, w = session.JSONRequest("POST", "/api/good/4?count=3", good) + assertion.Equal(http.StatusOK, w.StatusCode) + database.Read.Model(&good).Where("product_id", 4).Count(&count) + assertion.Equal(4, count) database.Close() @@ -73,6 +86,7 @@ func TestAddGood(t *testing.T) { test.Close() } +/* // Function to test delGood() func TestDelGood(t *testing.T) { assertion, router := test.Init(t) @@ -82,7 +96,6 @@ func TestDelGood(t *testing.T) { session := test.NewSession(router) good := models.Good{ - ID: 3, Comment: "blub", } @@ -96,10 +109,10 @@ func TestDelGood(t *testing.T) { _, w = session.JSONRequest("DELETE", "/api/good/a", nil) assertion.Equal(http.StatusNotAcceptable, w.StatusCode) - _, w = session.JSONRequest("DELETE", "/api/good/3", nil) + _, w = session.JSONRequest("DELETE", fmt.Sprintf("/api/good/%d", good.ID), nil) assertion.Equal(http.StatusOK, w.StatusCode) - _, w = session.JSONRequest("DELETE", "/api/good/3", nil) + _, w = session.JSONRequest("DELETE", fmt.Sprintf("/api/good/%d", good.ID), nil) assertion.Equal(http.StatusNotFound, w.StatusCode) database.Close() @@ -114,3 +127,4 @@ func TestDelGood(t *testing.T) { test.Close() } +*/ diff --git a/test/testrest.go b/test/testrest.go index e64440f..f00f327 100644 --- a/test/testrest.go +++ b/test/testrest.go @@ -6,6 +6,7 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "net/http" "net/http/httptest" "testing" @@ -39,13 +40,18 @@ func (t *MockTransport) Close() { t.running = false } +var lastTestDB int + // Function to initialize a test api (with test files of depending microservice) func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) { assertion = assert.New(t) + + lastTestDB++ + //database.Close() database.Open(database.Config{ Type: "sqlite3", Logging: true, - Connection: ":memory:", + Connection: fmt.Sprintf("file:database%s?mode=memory", lastTestDB), }) router = goji.NewMux() diff --git a/webroot/static/html/item.html b/webroot/static/html/item.html index 38ed8f4..60881bf 100644 --- a/webroot/static/html/item.html +++ b/webroot/static/html/item.html @@ -31,7 +31,7 @@ {{item.position}} {{item.comment}} - + diff --git a/webroot/static/js/item-add.controller.js b/webroot/static/js/item-add.controller.js index 3c48c8c..935a527 100644 --- a/webroot/static/js/item-add.controller.js +++ b/webroot/static/js/item-add.controller.js @@ -12,20 +12,11 @@ angular.module('microStock') }); $scope.submit = function(){ - var count = 0; - function request(){ - count++; - return $http.post(config.store.goods.productById.replace("%d",$stateParams.productid),$scope.obj); - } - var last = request(); - for(var i=1;i < $scope.count;i++){ - last.then(request); - } - last.then(function(){ + $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 '+count+' good(s) from product '+$scope.product.title+'.'}; - },function(){ + }, function(){ $scope.msg = {type:'error',text:'Error: Saved '+count+' good(s) from product '+$scope.product.title+'.'}; - }) + }); }; }]); diff --git a/webroot/static/js/item.controller.js b/webroot/static/js/item.controller.js index 9d23725..997e773 100644 --- a/webroot/static/js/item.controller.js +++ b/webroot/static/js/item.controller.js @@ -5,15 +5,18 @@ angular.module('microStock') $scope.obj = {}; $scope.list = []; - $http.get(config.microservice_dependencies.productById.replace("%d", $stateParams.productid)).then(function(res) { - $scope.obj = res.data - }); - $http.get(config.store.goods.productById.replace("%d",$stateParams.productid)).then(function(res) { - $scope.list = res.data - }); - $scope.delete = function(){ - $http.delete(config.store.goods.productById.replace("%d",$stateParams.productid)).then(function(res) { - $scope.list = res.data + function load(){ + $http.get(config.microservice_dependencies.productById.replace("%d", $stateParams.productid)).then(function(res) { + $scope.obj = res.data + }); + $http.get(config.store.goods.productById.replace("%d",$stateParams.productid)).then(function(res) { + $scope.list = res.data; + },function(){ + $scope.list = []; }); } + load(); + $scope.delete = function(id){ + $http.delete(config.store.goods.productById.replace("%d",id)).then(load); + } }]);