2017-03-30 18:43:18 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-03-30 19:05:33 +02:00
|
|
|
"strconv"
|
|
|
|
|
2017-04-04 23:21:05 +02:00
|
|
|
logrus "github.com/Sirupsen/logrus"
|
2017-03-30 19:05:33 +02:00
|
|
|
"goji.io/pat"
|
2017-03-30 18:43:18 +02:00
|
|
|
|
|
|
|
"github.com/genofire/hs_master-kss-monolith/lib/database"
|
|
|
|
lib "github.com/genofire/hs_master-kss-monolith/lib/http"
|
|
|
|
logger "github.com/genofire/hs_master-kss-monolith/lib/log"
|
|
|
|
"github.com/genofire/hs_master-kss-monolith/models"
|
2017-04-07 11:56:28 +02:00
|
|
|
"github.com/genofire/hs_master-kss-monolith/runtime"
|
2017-03-30 18:43:18 +02:00
|
|
|
)
|
|
|
|
|
2017-04-03 14:59:43 +02:00
|
|
|
func listGoods(w http.ResponseWriter, r *http.Request) {
|
2017-03-30 18:43:18 +02:00
|
|
|
log := logger.HTTP(r)
|
2017-03-30 19:05:33 +02:00
|
|
|
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)
|
|
|
|
return
|
|
|
|
}
|
2017-03-31 10:57:01 +02:00
|
|
|
log = log.WithField("productid", id)
|
2017-04-03 14:59:43 +02:00
|
|
|
var list []*models.Good
|
2017-03-30 19:05:33 +02:00
|
|
|
result := database.Read.Where("product_id = ?", id).Find(&list)
|
|
|
|
if result.RowsAffected == 0 {
|
2017-04-03 14:59:43 +02:00
|
|
|
log.Warn("no goods found")
|
|
|
|
http.Error(w, "no goods found", http.StatusNotFound)
|
2017-03-30 19:05:33 +02:00
|
|
|
return
|
|
|
|
}
|
2017-03-30 18:43:18 +02:00
|
|
|
lib.Write(w, list)
|
|
|
|
log.Info("done")
|
|
|
|
}
|
2017-04-04 23:21:05 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
return -1, log
|
|
|
|
}
|
|
|
|
log = log.WithField("productid", id)
|
2017-04-07 13:13:37 +02:00
|
|
|
ok, err := runtime.ProductExists(id)
|
2017-04-04 23:21:05 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("product could not verified on other microservice")
|
|
|
|
http.Error(w, "product could not verified on other microservice", http.StatusGatewayTimeout)
|
2017-04-07 11:32:49 +02:00
|
|
|
return -1, log
|
2017-04-04 23:21:05 +02:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
log.Warn("product did not exists anymore")
|
|
|
|
http.Error(w, "product did not exists anymore", http.StatusNotFound)
|
|
|
|
return -1, log
|
|
|
|
}
|
|
|
|
var count float64
|
2017-04-07 13:13:37 +02:00
|
|
|
(&models.Good{}).FilterAvailable(database.Read.Where("product_id = ?", id)).Count(&count)
|
2017-04-04 23:21:05 +02:00
|
|
|
return int(count), log
|
|
|
|
}
|
2017-04-07 13:13:37 +02:00
|
|
|
|
2017-04-04 23:21:05 +02:00
|
|
|
func getGoodAvailablity(w http.ResponseWriter, r *http.Request) {
|
|
|
|
count, log := getGoodAvailablityCount(w, r)
|
|
|
|
if count < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log = log.WithField("type", r.Header.Get("Content-Type"))
|
|
|
|
switch r.Header.Get("Content-Type") {
|
|
|
|
case "application/json":
|
|
|
|
lib.Write(w, count)
|
|
|
|
default:
|
|
|
|
getGoodAvailablitySVG(w, count)
|
|
|
|
}
|
|
|
|
log.Info("done")
|
|
|
|
}
|