genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
hs_monolith/http/good.go

76 lines
2.2 KiB
Go
Raw Normal View History

package http
import (
"net/http"
2017-03-30 19:05:33 +02:00
"strconv"
logrus "github.com/Sirupsen/logrus"
2017-03-30 19:05:33 +02:00
"goji.io/pat"
"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"
"github.com/genofire/hs_master-kss-monolith/runtime"
)
func listGoods(w http.ResponseWriter, r *http.Request) {
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)
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 {
log.Warn("no goods found")
http.Error(w, "no goods found", http.StatusNotFound)
2017-03-30 19:05:33 +02:00
return
}
lib.Write(w, list)
log.Info("done")
}
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)
product := runtime.Product{ID: id}
ok, err := product.Exists()
if err != nil {
log.Warn("product could not verified on other microservice")
http.Error(w, "product could not verified on other microservice", http.StatusGatewayTimeout)
return -1, log
}
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
(&models.Good{}).FilterAvailable(database.Read.Where("product_id = ?", product.ID)).Count(&count)
return int(count), log
}
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")
}