[QS] Comment and document \stock and \http
This commit is contained in:
parent
eb6e3c53d9
commit
0b47b3f9c5
|
@ -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()
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
<!-- Funktionen: -->
|
||||
<!-- process_radius ANZAHL MAXANZAHL RADIUS -->
|
||||
<!-- procent ANZAHL MAXANZAHL -->
|
||||
<!-- ex. {procent .Count 10}%-->
|
||||
<!-- SVG to show the current stock with an traffic light food labeling system -->
|
||||
<!-- Used functions -->
|
||||
<!-- process_radius ANZAHL MAXANZAHL RADIUS -->
|
||||
<!-- procent ANZAHL MAXANZAHL -->
|
||||
<!-- ex. {procent .Count 10}%-->
|
||||
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
|
||||
<g class="arcs">
|
||||
{{if lt .Count 4}}
|
||||
|
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.3 KiB |
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="GO_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="jdk" jdkName="Go 1.8" jdkType="Go SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -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)
|
||||
|
|
14
http/good.go
14
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)
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Reference in New Issue