genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[TASK] switch from review to stock microservice

This commit is contained in:
Martin Geno 2017-04-03 14:59:43 +02:00
parent ea15b36a52
commit 66d6022d37
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
13 changed files with 57 additions and 85 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
cmd/review/config.conf cmd/stock/config.conf
.idea/ .idea/

View File

@ -7,4 +7,4 @@ install:
- go get "golang.org/x/tools/cmd/cover" - go get "golang.org/x/tools/cmd/cover"
script: script:
- ./.test-coverage - ./.test-coverage
- go install github.com/genofire/hs_master-kss-monolith/cmd/review - go install github.com/genofire/hs_master-kss-monolith/cmd/stock

View File

@ -1,9 +1,17 @@
# Review-Microservice # Stock-Microservice
This is a microservice cutted out of a [Monolith](https://gitlab.com/matthiasstock/monolith). This is a microservice cutted out of a [Monolith](https://gitlab.com/matthiasstock/monolith).
[![Build Status](https://travis-ci.org/genofire/hs_master-kss-monolith.svg?branch=master)](https://travis-ci.org/genofire/hs_master-kss-monolith) [![Coverage Status](https://coveralls.io/repos/github/genofire/hs_master-kss-monolith/badge.svg?branch=master)](https://coveralls.io/github/genofire/hs_master-kss-monolith?branch=master) [![Build Status](https://travis-ci.org/genofire/hs_master-kss-monolith.svg?branch=master)](https://travis-ci.org/genofire/hs_master-kss-monolith) [![Coverage Status](https://coveralls.io/repos/github/genofire/hs_master-kss-monolith/badge.svg?branch=master)](https://coveralls.io/github/genofire/hs_master-kss-monolith?branch=master)
## Features of this review mircoservice ## Features of this stock mircoservice
* with firstname, lastname, stars and text * save goods via product with timestamp, if it was found and there place in a warehouse (in the admin front end)
* without login * add new goods
* binded by a productid * manual delete of goods, e.g if the good get fouled
* remove a good from warehouse, if it was send to customer
* lock goods, if a customer has it in his bill (and release after time x, if it was not send to customer)
* customer front end
* display availability with something like a traffic lights
### Nice to have / Can-But-Must-Not-Feature
* display of statistics values like how many goods was removed in a time range
* traffic light in admin front end of goods which get fouled in next time or is already fouled

View File

@ -12,7 +12,7 @@ import (
"github.com/genofire/hs_master-kss-monolith/models" "github.com/genofire/hs_master-kss-monolith/models"
) )
func listReview(w http.ResponseWriter, r *http.Request) { func listGoods(w http.ResponseWriter, r *http.Request) {
log := logger.HTTP(r) log := logger.HTTP(r)
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 {
@ -21,11 +21,11 @@ func listReview(w http.ResponseWriter, r *http.Request) {
return return
} }
log = log.WithField("productid", id) log = log.WithField("productid", id)
var list []*models.Review var list []*models.Good
result := database.Read.Where("product_id = ?", id).Find(&list) result := database.Read.Where("product_id = ?", id).Find(&list)
if result.RowsAffected == 0 { if result.RowsAffected == 0 {
log.Warn("no reviews found") log.Warn("no goods found")
http.Error(w, "no reviews found", http.StatusNotFound) http.Error(w, "no goods found", http.StatusNotFound)
return return
} }
lib.Write(w, list) lib.Write(w, list)

View File

@ -10,27 +10,24 @@ import (
"github.com/genofire/hs_master-kss-monolith/test" "github.com/genofire/hs_master-kss-monolith/test"
) )
func TestReview(t *testing.T) { func TestGood(t *testing.T) {
assertion, router := test.Init(t) assertion, router := test.Init(t)
BindAPI(router) BindAPI(router)
session := test.NewSession(router) session := test.NewSession(router)
result, w := session.JSONRequest("GET", "/api/review/a", nil) result, w := session.JSONRequest("GET", "/api/good/a", nil)
assertion.Equal(http.StatusNotAcceptable, w.StatusCode) assertion.Equal(http.StatusNotAcceptable, w.StatusCode)
result, w = session.JSONRequest("GET", "/api/review/1", nil) result, w = session.JSONRequest("GET", "/api/good/1", nil)
assertion.Equal(http.StatusNotFound, w.StatusCode) assertion.Equal(http.StatusNotFound, w.StatusCode)
database.Write.Create(&models.Review{ database.Write.Create(&models.Good{
ProductID: 3, ProductID: 3,
FirstName: "Max", Comment: "blub",
LastName: "Mustmann",
RatingStars: 3,
Text: "blub",
}) })
result, w = session.JSONRequest("GET", "/api/review/3", nil) result, w = session.JSONRequest("GET", "/api/good/3", nil)
assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Len(result, 1) assertion.Len(result, 1)

View File

@ -7,5 +7,5 @@ import (
func BindAPI(router *goji.Mux) { func BindAPI(router *goji.Mux) {
router.HandleFunc(pat.Get("/api/status"), status) router.HandleFunc(pat.Get("/api/status"), status)
router.HandleFunc(pat.Get("/api/review/:productid"), listReview) router.HandleFunc(pat.Get("/api/good/:productid"), listGoods)
} }

View File

@ -11,9 +11,16 @@ import (
func status(w http.ResponseWriter, r *http.Request) { func status(w http.ResponseWriter, r *http.Request) {
log := logger.HTTP(r) log := logger.HTTP(r)
var reviews []*models.Review var good []*models.Good
var count int64 var count int64
database.Read.Find(&reviews).Count(&count) var avg int64
lib.Write(w, map[string]interface{}{"status": "running", "review_count": count}) database.Read.Find(&good).Count(&count) //.Avg(&avg)
lib.Write(w, map[string]interface{}{
"status": "running",
"good": map[string]interface{}{
"count": count,
"avg": avg,
},
})
log.Info("done") log.Info("done")
} }

View File

@ -17,7 +17,8 @@ func TestStatus(t *testing.T) {
result := r.(map[string]interface{}) result := r.(map[string]interface{})
assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal("running", result["status"]) assertion.Equal("running", result["status"])
assertion.Equal(float64(0), result["review_count"]) good := result["good"].(map[string]interface{})
assertion.Equal(float64(0), good["count"])
test.Close() test.Close()
} }

13
models/good.go Normal file
View File

@ -0,0 +1,13 @@
package models
import "github.com/genofire/hs_master-kss-monolith/lib/database"
type Good struct {
ID int64
ProductID int64
Comment string
}
func init() {
database.AddModel(&Good{})
}

5
models/product.go Normal file
View File

@ -0,0 +1,5 @@
package models
func ProductExists(id int64) (bool, error) {
return true, nil
}

View File

@ -1,28 +0,0 @@
package models
import "github.com/genofire/hs_master-kss-monolith/lib/database"
type Review struct {
ID int64
ProductID int64
LocaleLanguage string
FirstName string
LastName string
RatingStars int64
Text string
}
func (r *Review) DisplayName() string {
if len(r.FirstName) > 0 {
if len(r.LastName) > 0 {
last := []byte(r.LastName)
return r.FirstName + " " + string(last[0]) + "."
}
return r.FirstName
}
return "Anonymous"
}
func init() {
database.AddModel(&Review{})
}

View File

@ -1,31 +0,0 @@
package models
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDisplayName(t *testing.T) {
assert := assert.New(t)
r := Review{}
assert.Equal("", r.FirstName, "wrong firstname")
assert.Equal("", r.LastName, "wrong lastname")
assert.Equal("Anonymous", r.DisplayName(), "No name")
r.FirstName = "Max"
assert.Equal("Max", r.FirstName, "wrong firstname")
assert.Equal("", r.LastName, "wrong lastname")
assert.Equal("Max", r.DisplayName(), "Only Firstname")
r.LastName = "Mustermann"
assert.Equal("Max", r.FirstName, "wrong firstname")
assert.Equal("Mustermann", r.LastName, "wrong lastname")
assert.Equal("Max M.", r.DisplayName(), "Shorted Name")
r.FirstName = ""
assert.Equal("", r.FirstName, "wrong firstname")
assert.Equal("Mustermann", r.LastName, "wrong lastname")
assert.Equal("Anonymous", r.DisplayName(), "displayname: no firstname")
}