From 9c57445eeb0f53e6c5dea206e21063d347aea957 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Fri, 23 Jun 2017 17:09:03 +0200 Subject: [PATCH] [TASK] add buy option (delete without tag - by secret) --- config_example.conf | 2 +- http/bindapi.go | 1 + http/good_lock.go | 69 ++++++++++++++++++++++++++++++++--- http/good_lock_test.go | 55 ++++++++++++++++++++++++++-- http/good_temp.go | 3 ++ http/good_temp_test.go | 3 ++ webroot/dummy_cart/index.html | 50 ++++++++++++++++++------- 7 files changed, 160 insertions(+), 23 deletions(-) diff --git a/config_example.conf b/config_example.conf index a2e6eec..7620c71 100644 --- a/config_example.conf +++ b/config_example.conf @@ -8,7 +8,7 @@ fouled_deleted = "0m" [database] type = "sqlite3" -#logging = true +logging = true connection = "file::memory:?mode=memory&cache=shared" # For Master-Slave cluster # read_connection = "" diff --git a/http/bindapi.go b/http/bindapi.go index 75a91fb..c023030 100644 --- a/http/bindapi.go +++ b/http/bindapi.go @@ -20,4 +20,5 @@ func BindAPI(router *goji.Mux) { router.HandleFunc(pat.Post("/api/goods/locking"), lockGoods) router.HandleFunc(pat.Delete("/api/goods/locking"), releaseGoods) + router.HandleFunc(pat.Put("/api/goods/locking"), delLockedGoods) } diff --git a/http/good_lock.go b/http/good_lock.go index 166a940..0de6ede 100644 --- a/http/good_lock.go +++ b/http/good_lock.go @@ -3,6 +3,7 @@ package http import ( "net/http" + "time" "github.com/genofire/hs_master-kss-monolith/lib/database" lib "github.com/genofire/hs_master-kss-monolith/lib/http" @@ -45,12 +46,12 @@ func lockGoods(w http.ResponseWriter, r *http.Request) { } tx := database.Write.Begin() + defer tx.Rollback() count := int64(0) for _, good := range goods { if good.ProductID <= 0 { log.Warn("tried to log nothing") - tx.Rollback() http.Error(w, "tried to log nothing", http.StatusBadRequest) return } @@ -59,7 +60,6 @@ func lockGoods(w http.ResponseWriter, r *http.Request) { db := g.FilterAvailable(tx).First(g) if db.RecordNotFound() { log.Warn("good not found") - tx.Rollback() http.Error(w, "the good was not found in database", http.StatusNotFound) return } @@ -69,7 +69,6 @@ func lockGoods(w http.ResponseWriter, r *http.Request) { if db.Error != nil || db.RowsAffected != 1 { http.Error(w, "the good was not found in database", http.StatusInternalServerError) - tx.Rollback() log.Panic("there is more than one good locked: ", db.Error) return } @@ -85,11 +84,71 @@ func lockGoods(w http.ResponseWriter, r *http.Request) { // Function to release locked goods func releaseGoods(w http.ResponseWriter, r *http.Request) { + log := logger.HTTP(r) + secret := r.Header.Get("secret") + if secret == "" { + log.Warn("no secred for locking given") + http.Error(w, "no secred for locking given", http.StatusBadRequest) + return + } + + log = log.WithField("lSecret", secret) + var goods []*LockGood + + err := lib.Read(r, &goods) + + if err != nil { + log.Warn(err.Error()) + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + if len(goods) <= 0 { + log.Warn("tried to log nothing") + http.Error(w, "tried to log nothing", http.StatusBadRequest) + return + } + + tx := database.Write.Begin() + defer tx.Rollback() + for _, good := range goods { + if good.ProductID <= 0 { + log.Warn("tried to log nothing") + http.Error(w, "tried to release nothing", http.StatusBadRequest) + return + } + for i := 0; i < good.Count; i++ { + g := &models.Good{} + db := tx.Where(map[string]interface{}{"product_id": good.ProductID, "locked_secret": secret}).First(g) + if db.RecordNotFound() { + log.Warn("good not found") + http.Error(w, "the good was not found in database", http.StatusNotFound) + return + } + + g.Unlock(secret) + db = tx.Save(g) + + if db.Error != nil || db.RowsAffected != 1 { + http.Error(w, "the good was not found in database", http.StatusInternalServerError) + log.Panic("there is more than one good released: ", db.Error) + return + } + } + } + + lib.Write(w, true) + tx.Commit() + log.Info("done") +} + +// Function to del locked goods +func delLockedGoods(w http.ResponseWriter, r *http.Request) { log := logger.HTTP(r) secret := r.Header.Get("secret") log = log.WithField("lSecret", secret) - db := database.Write.Model(&models.Good{}).Where(&models.Good{LockedSecret: secret}).Updates(map[string]interface{}{"locked_secret": nil, "locked_at": nil}) + db := database.Write.Model(&models.Good{}).Where(&models.Good{LockedSecret: secret}).Updates(map[string]interface{}{"deleted_at": time.Now(), "locked_secret": ""}) err := db.Error result := db.RowsAffected @@ -101,7 +160,7 @@ func releaseGoods(w http.ResponseWriter, r *http.Request) { if result <= 0 { log.Warn("no goods found") - http.Error(w, "there are no goods to release", http.StatusNotFound) + http.Error(w, "there are no goods to buy", http.StatusNotFound) return } diff --git a/http/good_lock_test.go b/http/good_lock_test.go index 41927ed..c8c2901 100644 --- a/http/good_lock_test.go +++ b/http/good_lock_test.go @@ -60,7 +60,54 @@ func TestLockGoods(t *testing.T) { } // Function to test releaseGoods() -func TestReleaseGoods(t *testing.T) { +func TestReleaseLockGoods(t *testing.T) { + assertion, router := test.Init(t) + good := &models.Good{ + ProductID: 3, + Comment: "blabla", + LockedSecret: "hiddenLockTest", + } + database.Write.Create(good) + + BindAPI(router) + session := test.NewSession(router) + + _, w := session.JSONRequest("DELETE", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}}) + assertion.Equal(http.StatusBadRequest, w.StatusCode) + + session.Header["secret"] = "hiddenLockTest" + + _, w = session.JSONRequest("DELETE", "/api/goods/locking", 13) + assertion.Equal(http.StatusBadRequest, w.StatusCode) + + _, w = session.JSONRequest("DELETE", "/api/goods/locking", nil) + assertion.Equal(http.StatusBadRequest, w.StatusCode) + + _, w = session.JSONRequest("DELETE", "/api/goods/locking", []interface{}{LockGood{ProductID: 0, Count: 2}}) + assertion.Equal(http.StatusBadRequest, w.StatusCode) + + _, w = session.JSONRequest("DELETE", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 2}}) + assertion.Equal(http.StatusNotFound, w.StatusCode) + + result, w := session.JSONRequest("DELETE", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}}) + assertion.Equal(http.StatusOK, w.StatusCode) + resultBool := result.(bool) + assertion.True(resultBool) + + _, w = session.JSONRequest("DELETE", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}}) + assertion.Equal(http.StatusNotFound, w.StatusCode) + + database.Close() + + assertion.Panics(func() { + _, w = session.JSONRequest("DELETE", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}}) + assertion.Equal(http.StatusInternalServerError, w.StatusCode) + }) + + test.Close() +} + +func TestDelLockGoods(t *testing.T) { now := time.Now() assertion, router := test.Init(t) @@ -75,18 +122,18 @@ func TestReleaseGoods(t *testing.T) { session := test.NewSession(router) session.Header["secret"] = "a" - result, w := session.JSONRequest("DELETE", "/api/goods/locking", nil) + result, w := session.JSONRequest("PUT", "/api/goods/locking", nil) assertion.Equal(http.StatusNotFound, w.StatusCode) session.Header["secret"] = "hidden" - result, w = session.JSONRequest("DELETE", "/api/goods/locking", nil) + result, w = session.JSONRequest("PUT", "/api/goods/locking", nil) assertion.Equal(http.StatusOK, w.StatusCode) resultMap := result.(map[string]interface{}) count := resultMap["count"] assertion.Equal(float64(1), count) database.Close() - result, w = session.JSONRequest("DELETE", "/api/goods/locking", nil) + result, w = session.JSONRequest("PUT", "/api/goods/locking", nil) assertion.Equal(http.StatusInternalServerError, w.StatusCode) test.Close() diff --git a/http/good_temp.go b/http/good_temp.go index c3f6b2d..9ab9db4 100644 --- a/http/good_temp.go +++ b/http/good_temp.go @@ -16,6 +16,9 @@ var GoodFreshnessTemplate string // Function to calculate a percent value from a given value and a maximum value func tempPercent(value, max int) int { + if value >= max { + return 100 + } return value * 100 / max } diff --git a/http/good_temp_test.go b/http/good_temp_test.go index afd411f..8aa1d35 100644 --- a/http/good_temp_test.go +++ b/http/good_temp_test.go @@ -13,6 +13,9 @@ func TestTempFuncs(t *testing.T) { resultInt := tempPercent(3, 9) assert.Equal(33, resultInt) + resultInt = tempPercent(13, 9) + assert.Equal(100, resultInt) + // TODO is there a other way to calc this? resultFloat := tempProcessRadius(3, 9, 0) assert.Equal(float64(0), resultFloat) diff --git a/webroot/dummy_cart/index.html b/webroot/dummy_cart/index.html index 5db4836..9bddbc4 100644 --- a/webroot/dummy_cart/index.html +++ b/webroot/dummy_cart/index.html @@ -31,7 +31,7 @@
Select Product
@@ -55,13 +55,14 @@ {{item.count}} - {{getProduct(item.product_id).title}} + {{getProduct(item.product_id).name}}
+