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_show.go

115 lines
3.2 KiB
Go
Raw Normal View History

// Package that contains all api routes of this microservice
2017-04-28 12:05:58 +02:00
package http
import (
"net/http"
"strconv"
logrus "github.com/Sirupsen/logrus"
"goji.io/pat"
2017-05-17 21:33:23 +02:00
"time"
2017-04-28 12:05:58 +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"
"github.com/genofire/hs_master-kss-monolith/runtime"
)
// Function to list all goods
2017-04-28 12:05:58 +02:00
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, "the productid is false", http.StatusNotAcceptable)
2017-04-28 12:05:58 +02:00
return
}
log = log.WithField("productid", id)
var list []*models.Good
result := database.Read.Where("product_id = ?", id).Find(&list)
if result.RowsAffected == 0 {
log.Warn("no goods found")
http.Error(w, "no goods found for this product", http.StatusNotFound)
2017-04-28 12:05:58 +02:00
return
}
lib.Write(w, list)
log.Info("done")
}
2017-05-15 10:22:24 +02:00
// Function that counts all available goods for one product
2017-04-28 12:05:58 +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, "the product id has a false format", http.StatusNotAcceptable)
2017-04-28 12:05:58 +02:00
return -1, log
}
log = log.WithField("productid", id)
ok, err := runtime.ProductExists(id)
if err != nil {
log.Warn("product could not verified on the microservice")
http.Error(w, "the product could not be verified", http.StatusGatewayTimeout)
2017-04-28 12:05:58 +02:00
return -1, log
}
if !ok {
log.Warn("product does not exists anymore")
http.Error(w, "the product does not exists anymore", http.StatusNotFound)
2017-04-28 12:05:58 +02:00
return -1, log
}
var count float64
(&models.Good{}).FilterAvailable(database.Read.Where("product_id = ?", id)).Count(&count)
return int(count), log
}
// Function that returns the availability of a good
2017-05-12 10:54:05 +02:00
func getGoodAvailability(w http.ResponseWriter, r *http.Request) {
2017-04-28 12:05:58 +02:00
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")
}
2017-05-12 10:54:05 +02:00
// Function that returns the freshness of a good
2017-05-17 21:33:23 +02:00
func getGoodFreshness(w http.ResponseWriter, r *http.Request) {
2017-05-12 10:54:05 +02:00
log := logger.HTTP(r)
id, err := strconv.ParseInt(pat.Param(r, "goodid"), 10, 64)
if err != nil {
log.Warn("wrong goodid format")
http.Error(w, "the good id has a false format", http.StatusNotAcceptable)
return
}
log = log.WithField("goodid", id)
var good models.Good
database.Read.Where("id = ?", id).First(&good)
if good.ProductID == 0 {
log.Warn("good not found")
http.Error(w, "the good was not found in database", http.StatusNotFound)
return
}
2017-05-12 11:15:54 +02:00
fresh := false
if good.FouledAt != nil {
fresh = time.Now().Before(*good.FouledAt)
2017-05-12 11:15:54 +02:00
}
2017-05-12 10:54:05 +02:00
log = log.WithField("type", r.Header.Get("Content-Type"))
switch r.Header.Get("Content-Type") {
case "application/json":
lib.Write(w, fresh)
default:
getGoodFreshnessSVG(w, fresh)
}
log.Info("done")
2017-05-17 21:33:23 +02:00
}