[TASK] implement lock of goods + add tests
This commit is contained in:
parent
18414bbb95
commit
cf5d94d55d
|
@ -11,25 +11,71 @@ import (
|
||||||
|
|
||||||
type LockGood struct {
|
type LockGood struct {
|
||||||
ProductID int64 `json:"product_id"`
|
ProductID int64 `json:"product_id"`
|
||||||
Count int64 `json:"count"`
|
Count int `json:"count"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func lockGoods(w http.ResponseWriter, r *http.Request) {
|
func lockGoods(w http.ResponseWriter, r *http.Request) {
|
||||||
log := logger.HTTP(r)
|
log := logger.HTTP(r)
|
||||||
secret := r.Header.Get("secret")
|
secret := r.Header.Get("secret")
|
||||||
log = log.WithField("lSecret", secret)
|
|
||||||
|
|
||||||
tx := database.Write.Begin()
|
if secret == "" {
|
||||||
// TODO the logic
|
log.Warn("no secred for locking given")
|
||||||
if tx.Error != nil {
|
http.Error(w, "no secred for locking given", http.StatusBadRequest)
|
||||||
tx.Rollback()
|
|
||||||
log.Warn("good not found")
|
|
||||||
http.Error(w, "the good was not found in database", http.StatusNotFound)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// TODO the logic
|
|
||||||
|
|
||||||
lib.Write(w, map[string]int64{"count": 0})
|
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("try to log nothing")
|
||||||
|
http.Error(w, "try to log nothing", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tx := database.Write.Begin()
|
||||||
|
count := int64(0)
|
||||||
|
|
||||||
|
for _, good := range goods {
|
||||||
|
if good.ProductID <= 0 {
|
||||||
|
log.Warn("try to log nothing")
|
||||||
|
tx.Rollback()
|
||||||
|
http.Error(w, "try to log nothing", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for i := 0; i < good.Count; i++ {
|
||||||
|
g := &models.Good{ProductID: good.ProductID}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
g.Lock(secret)
|
||||||
|
|
||||||
|
db = tx.Save(g)
|
||||||
|
|
||||||
|
if db.Error != nil || db.RowsAffected != 1 {
|
||||||
|
http.Error(w, "the good was not found in database", http.StatusInternalServerError)
|
||||||
|
tx.Rollback()
|
||||||
|
log.Panic("more then one good locked: ", db.Error)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
count += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lib.Write(w, map[string]int64{"count": count})
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
log.Info("done")
|
log.Info("done")
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,53 @@ import (
|
||||||
"github.com/genofire/hs_master-kss-monolith/test"
|
"github.com/genofire/hs_master-kss-monolith/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestLockGoods(t *testing.T) {
|
||||||
|
assertion, router := test.Init(t)
|
||||||
|
good := &models.Good{
|
||||||
|
ProductID: 3,
|
||||||
|
Comment: "blabla",
|
||||||
|
}
|
||||||
|
database.Write.Create(good)
|
||||||
|
|
||||||
|
BindAPI(router)
|
||||||
|
session := test.NewSession(router)
|
||||||
|
|
||||||
|
_, w := session.JSONRequest("POST", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}})
|
||||||
|
assertion.Equal(http.StatusBadRequest, w.StatusCode)
|
||||||
|
|
||||||
|
session.Header["secret"] = "hiddenLockTest"
|
||||||
|
|
||||||
|
_, w = session.JSONRequest("POST", "/api/goods/locking", 13)
|
||||||
|
assertion.Equal(http.StatusBadRequest, w.StatusCode)
|
||||||
|
|
||||||
|
_, w = session.JSONRequest("POST", "/api/goods/locking", nil)
|
||||||
|
assertion.Equal(http.StatusBadRequest, w.StatusCode)
|
||||||
|
|
||||||
|
_, w = session.JSONRequest("POST", "/api/goods/locking", []interface{}{LockGood{ProductID: 0, Count: 2}})
|
||||||
|
assertion.Equal(http.StatusBadRequest, w.StatusCode)
|
||||||
|
|
||||||
|
_, w = session.JSONRequest("POST", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 2}})
|
||||||
|
assertion.Equal(http.StatusNotFound, w.StatusCode)
|
||||||
|
|
||||||
|
result, w := session.JSONRequest("POST", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}})
|
||||||
|
assertion.Equal(http.StatusOK, w.StatusCode)
|
||||||
|
resultMap := result.(map[string]interface{})
|
||||||
|
count := resultMap["count"]
|
||||||
|
assertion.Equal(float64(1), count)
|
||||||
|
|
||||||
|
_, w = session.JSONRequest("POST", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}})
|
||||||
|
assertion.Equal(http.StatusNotFound, w.StatusCode)
|
||||||
|
|
||||||
|
database.Close()
|
||||||
|
|
||||||
|
assertion.Panics(func() {
|
||||||
|
_, w = session.JSONRequest("POST", "/api/goods/locking", []interface{}{LockGood{ProductID: 3, Count: 1}})
|
||||||
|
assertion.Equal(http.StatusInternalServerError, w.StatusCode)
|
||||||
|
})
|
||||||
|
|
||||||
|
test.Close()
|
||||||
|
}
|
||||||
|
|
||||||
func TestReleaseGoods(t *testing.T) {
|
func TestReleaseGoods(t *testing.T) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
assertion, router := test.Init(t)
|
assertion, router := test.Init(t)
|
||||||
|
|
Reference in New Issue