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"
"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)

View File

@ -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()
}
*/

View File

@ -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()

View File

@ -31,7 +31,7 @@
<td><img class="icon" ng-src="{{'/api/good/freshness/'+item.id| reloadSrc}}"/></td>
<td>{{item.position}}</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>
</tbody>
</table>

View File

@ -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(){
$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.list = [];
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
});
$scope.delete = function(){
$http.delete(config.store.goods.productById.replace("%d",$stateParams.productid)).then(function(res) {
$scope.list = res.data
$scope.list = res.data;
},function(){
$scope.list = [];
});
}
load();
$scope.delete = function(id){
$http.delete(config.store.goods.productById.replace("%d",id)).then(load);
}
}]);