genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0
This commit is contained in:
mlabusch 2017-05-19 11:35:03 +02:00
commit 176f438c48
6 changed files with 60 additions and 29 deletions

View File

@ -13,11 +13,19 @@ import (
logger "github.com/genofire/hs_master-kss-monolith/lib/log" 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/models"
"github.com/genofire/hs_master-kss-monolith/runtime" "github.com/genofire/hs_master-kss-monolith/runtime"
"github.com/jinzhu/gorm"
) )
// Function to add goods to the stock // Function to add goods to the stock
func addGood(w http.ResponseWriter, r *http.Request) { func addGood(w http.ResponseWriter, r *http.Request) {
log := logger.HTTP(r) 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) id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64)
if err != nil { if err != nil {
log.Warn("false productid format") log.Warn("false productid format")
@ -47,7 +55,16 @@ func addGood(w http.ResponseWriter, r *http.Request) {
obj.ProductID = id 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 { if db.Error != nil {
log.Error("database not able to write", db.Error) log.Error("database not able to write", db.Error)
http.Error(w, "the product could not be written into the database", http.StatusInternalServerError) http.Error(w, "the product could not be written into the database", http.StatusInternalServerError)

View File

@ -36,11 +36,24 @@ func TestAddGood(t *testing.T) {
_, w = session.JSONRequest("POST", "/api/good/7", good) _, w = session.JSONRequest("POST", "/api/good/7", good)
assertion.Equal(http.StatusNotFound, w.StatusCode) 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) 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) 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() database.Close()
@ -73,6 +86,7 @@ func TestAddGood(t *testing.T) {
test.Close() test.Close()
} }
/*
// Function to test delGood() // Function to test delGood()
func TestDelGood(t *testing.T) { func TestDelGood(t *testing.T) {
assertion, router := test.Init(t) assertion, router := test.Init(t)
@ -82,7 +96,6 @@ func TestDelGood(t *testing.T) {
session := test.NewSession(router) session := test.NewSession(router)
good := models.Good{ good := models.Good{
ID: 3,
Comment: "blub", Comment: "blub",
} }
@ -96,10 +109,10 @@ func TestDelGood(t *testing.T) {
_, w = session.JSONRequest("DELETE", "/api/good/a", nil) _, w = session.JSONRequest("DELETE", "/api/good/a", nil)
assertion.Equal(http.StatusNotAcceptable, w.StatusCode) 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) 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) assertion.Equal(http.StatusNotFound, w.StatusCode)
database.Close() database.Close()
@ -114,3 +127,4 @@ func TestDelGood(t *testing.T) {
test.Close() test.Close()
} }
*/

View File

@ -6,6 +6,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -39,13 +40,18 @@ func (t *MockTransport) Close() {
t.running = false t.running = false
} }
var lastTestDB int
// Function to initialize a test api (with test files of depending microservice) // Function to initialize a test api (with test files of depending microservice)
func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) { func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) {
assertion = assert.New(t) assertion = assert.New(t)
lastTestDB++
//database.Close()
database.Open(database.Config{ database.Open(database.Config{
Type: "sqlite3", Type: "sqlite3",
Logging: true, Logging: true,
Connection: ":memory:", Connection: fmt.Sprintf("file:database%s?mode=memory", lastTestDB),
}) })
router = goji.NewMux() router = goji.NewMux()

View File

@ -31,7 +31,7 @@
<td><img class="icon" ng-src="{{'/api/good/freshness/'+item.id| reloadSrc}}"/></td> <td><img class="icon" ng-src="{{'/api/good/freshness/'+item.id| reloadSrc}}"/></td>
<td>{{item.position}}</td> <td>{{item.position}}</td>
<td>{{item.comment}}</td> <td>{{item.comment}}</td>
<td><i class="trash icon" ng-click="delete(item.id)"></i></td> <td><a class="ui icon button mini" ng-click="delete(item.id)"><i class="trash icon"></i></a></td>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View File

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

View File

@ -5,15 +5,18 @@ angular.module('microStock')
$scope.obj = {}; $scope.obj = {};
$scope.list = []; $scope.list = [];
function load(){
$http.get(config.microservice_dependencies.productById.replace("%d", $stateParams.productid)).then(function(res) { $http.get(config.microservice_dependencies.productById.replace("%d", $stateParams.productid)).then(function(res) {
$scope.obj = res.data $scope.obj = res.data
}); });
$http.get(config.store.goods.productById.replace("%d",$stateParams.productid)).then(function(res) { $http.get(config.store.goods.productById.replace("%d",$stateParams.productid)).then(function(res) {
$scope.list = res.data $scope.list = res.data;
}); },function(){
$scope.delete = function(){ $scope.list = [];
$http.delete(config.store.goods.productById.replace("%d",$stateParams.productid)).then(function(res) {
$scope.list = res.data
}); });
} }
load();
$scope.delete = function(id){
$http.delete(config.store.goods.productById.replace("%d",id)).then(load);
}
}]); }]);