diff --git a/cmd/stock/main.go b/cmd/stock/main.go index 49e7d45..4114567 100644 --- a/cmd/stock/main.go +++ b/cmd/stock/main.go @@ -1,4 +1,4 @@ -// The cmd binary of the microservice to run +// Package that containts the cmd binary of the microservice to run it package main import ( @@ -19,11 +19,13 @@ import ( "github.com/genofire/hs_master-kss-monolith/runtime" ) +// Configuration File var ( configFile string config *models.Config ) +// Function to run this go program func main() { flag.StringVar(&configFile, "config", "config.conf", "path of configuration file (default:config.conf)") flag.Parse() diff --git a/contrib/good_availablity.svg b/contrib/good_availablity.svg index fe2e4a3..0d1d1cb 100644 --- a/contrib/good_availablity.svg +++ b/contrib/good_availablity.svg @@ -1,7 +1,8 @@ - - - - + + + + + {{if lt .Count 4}} diff --git a/hs_master-kss-monolith.iml b/hs_master-kss-monolith.iml new file mode 100644 index 0000000..079f520 --- /dev/null +++ b/hs_master-kss-monolith.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/http/bindapi.go b/http/bindapi.go index c752122..ccc8625 100644 --- a/http/bindapi.go +++ b/http/bindapi.go @@ -1,4 +1,4 @@ -// all api routes of this microservice +// Package that contains all api routes of this microservice package http import ( @@ -9,7 +9,7 @@ import ( "github.com/genofire/hs_master-kss-monolith/runtime" ) -// bind all API routes to webserver +// Function to bind all api routes to the webserver func BindAPI(router *goji.Mux) { router.HandleFunc(pat.Get("/api/status"), status) router.HandleFunc(pat.Get("/api/good/:productid"), listGoods) diff --git a/http/good.go b/http/good.go index 64507e4..bb10719 100644 --- a/http/good.go +++ b/http/good.go @@ -1,3 +1,4 @@ +// Package that contains all api routes of this microservice package http import ( @@ -13,12 +14,13 @@ import ( "github.com/genofire/hs_master-kss-monolith/runtime" ) +// Function to add goods to the stock func addGood(w http.ResponseWriter, r *http.Request) { log := logger.HTTP(r) id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64) if err != nil { - log.Warn("wrong productid format") - http.Error(w, "wrong productid", http.StatusNotAcceptable) + log.Warn("false productid format") + http.Error(w, "the product id is false", http.StatusNotAcceptable) return } log = log.WithField("productid", id) @@ -29,8 +31,8 @@ func addGood(w http.ResponseWriter, r *http.Request) { return } if !ok { - log.Warn("wrong product not found") - http.Error(w, "wrong product not found", http.StatusNotFound) + log.Warn("false product, product not found") + http.Error(w, "the product was not found", http.StatusNotFound) return } @@ -41,8 +43,8 @@ func addGood(w http.ResponseWriter, r *http.Request) { db := database.Write.Create(&obj) if db.Error != nil { - log.Error("database could not write", db.Error) - http.Error(w, "was not possible to write", http.StatusInternalServerError) + log.Error("database not able to write", db.Error) + http.Error(w, "the product could not be written into the database", http.StatusInternalServerError) } lib.Write(w, &obj) diff --git a/http/good_show.go b/http/good_show.go index fda4c05..4381711 100644 --- a/http/good_show.go +++ b/http/good_show.go @@ -1,3 +1,4 @@ +// Package that contains all api routes of this microservice package http import ( @@ -14,12 +15,13 @@ import ( "github.com/genofire/hs_master-kss-monolith/runtime" ) +// Function to list all goods func listGoods(w http.ResponseWriter, r *http.Request) { log := logger.HTTP(r) id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64) if err != nil { log.Warn("wrong productid format") - http.Error(w, "wrong productid", http.StatusNotAcceptable) + http.Error(w, "the productid is false", http.StatusNotAcceptable) return } log = log.WithField("productid", id) @@ -27,31 +29,32 @@ func listGoods(w http.ResponseWriter, r *http.Request) { result := database.Read.Where("product_id = ?", id).Find(&list) if result.RowsAffected == 0 { log.Warn("no goods found") - http.Error(w, "no goods found", http.StatusNotFound) + http.Error(w, "no goods found for this product", http.StatusNotFound) return } lib.Write(w, list) log.Info("done") } +// Function that counts als available goods for one product func getGoodAvailablityCount(w http.ResponseWriter, r *http.Request) (int, *logrus.Entry) { log := logger.HTTP(r) id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64) if err != nil { log.Warn("wrong productid format") - http.Error(w, "wrong productid", http.StatusNotAcceptable) + http.Error(w, "the product id has a false format", http.StatusNotAcceptable) return -1, log } log = log.WithField("productid", id) ok, err := runtime.ProductExists(id) if err != nil { - log.Warn("product could not verified on other microservice") - http.Error(w, "product could not verified on other microservice", http.StatusGatewayTimeout) + log.Warn("product could not verified on the microservice") + http.Error(w, "the product could not be verified", http.StatusGatewayTimeout) return -1, log } if !ok { - log.Warn("product did not exists anymore") - http.Error(w, "product did not exists anymore", http.StatusNotFound) + log.Warn("product does not exists anymore") + http.Error(w, "the product does not exists anymore", http.StatusNotFound) return -1, log } var count float64 @@ -59,6 +62,7 @@ func getGoodAvailablityCount(w http.ResponseWriter, r *http.Request) (int, *logr return int(count), log } +// Function that returns the availability of a good func getGoodAvailablity(w http.ResponseWriter, r *http.Request) { count, log := getGoodAvailablityCount(w, r) if count < 0 { diff --git a/http/good_show_test.go b/http/good_show_test.go index 1f6c88e..d5ed3ba 100644 --- a/http/good_show_test.go +++ b/http/good_show_test.go @@ -1,3 +1,4 @@ +// Package that contains all api routes of this microservice package http import ( @@ -13,6 +14,7 @@ import ( "github.com/genofire/hs_master-kss-monolith/test" ) +// Function to test listGoods() func TestListGood(t *testing.T) { assertion, router := test.Init(t) @@ -37,6 +39,7 @@ func TestListGood(t *testing.T) { test.Close() } +// Function to getGoodAvailability() and getGoodAvailabilityCount() func TestGetGoodAvailable(t *testing.T) { now := time.Now() assertion, router := test.Init(t) diff --git a/http/good_temp.go b/http/good_temp.go index b3dd626..06948af 100644 --- a/http/good_temp.go +++ b/http/good_temp.go @@ -1,3 +1,4 @@ +// Package that contains all api routes of this microservice package http import ( @@ -8,21 +9,25 @@ import ( "text/template" ) -// path to the svg image templaes to show availablity of a given good +// Path to the svg image template, that shows the availablity of a given good +// with a traffic light food labeling system var GoodAvailablityTemplate string -func tempProcent(value, max int) int { +// Function to calculate a percent value from a given value and an maximum value +func tempPercent(value, max int) int { return value * 100 / max } +// Function to calculate a partial radius, depending on a percentage value func tempProcessRadius(value, max, radius int) float64 { return (1 - float64(value)/float64(max)) * float64(radius) * 2 * 3.14 } +// Function to get the SVG, that shows the traffic light food labeling system for a given good func getGoodAvailablitySVG(w http.ResponseWriter, count int) { t := template.New("some") - t = t.Funcs(template.FuncMap{"procent": tempProcent, + t = t.Funcs(template.FuncMap{"procent": tempPercent, "process_radius": tempProcessRadius, }) buf := bytes.NewBuffer(nil) diff --git a/http/good_temp_test.go b/http/good_temp_test.go index 1c685f4..0484c90 100644 --- a/http/good_temp_test.go +++ b/http/good_temp_test.go @@ -1,3 +1,4 @@ +// Package that contains all api routes of this microservice package http import ( @@ -6,9 +7,10 @@ import ( "github.com/stretchr/testify/assert" ) +// Function to test tempPercent() and tempProcessRadius() func TestTempFuncs(t *testing.T) { assert := assert.New(t) - resultInt := tempProcent(3, 9) + resultInt := tempPercent(3, 9) assert.Equal(33, resultInt) // TODO is there a other way to calc this? diff --git a/http/good_test.go b/http/good_test.go index 8619d04..ef22a82 100644 --- a/http/good_test.go +++ b/http/good_test.go @@ -1,3 +1,4 @@ +// Package that contains all api routes of this microservice package http import ( @@ -11,6 +12,7 @@ import ( "github.com/genofire/hs_master-kss-monolith/test" ) +// Function to test addGood() func TestAddGood(t *testing.T) { assertion, router := test.Init(t) diff --git a/http/status.go b/http/status.go index 751c702..8a3ad46 100644 --- a/http/status.go +++ b/http/status.go @@ -1,3 +1,4 @@ +// Package that contains all api routes of this microservice package http import ( @@ -9,6 +10,7 @@ import ( "github.com/genofire/hs_master-kss-monolith/models" ) +// Function to get the status of the microservice, the database and the goods func status(w http.ResponseWriter, r *http.Request) { log := logger.HTTP(r) var goods []*models.Good diff --git a/http/status_test.go b/http/status_test.go index 34f9037..5b5a490 100644 --- a/http/status_test.go +++ b/http/status_test.go @@ -1,3 +1,4 @@ +// Package that contains all api routes of this microservice package http import ( @@ -9,6 +10,7 @@ import ( "github.com/genofire/hs_master-kss-monolith/test" ) +// Function to test status() func TestStatus(t *testing.T) { assertion, router := test.Init(t)