2017-05-03 06:52:14 +02:00
|
|
|
// Package that contains all api routes of this microservice
|
2017-03-30 18:43:18 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-03-30 19:05:33 +02:00
|
|
|
"strconv"
|
2017-05-18 23:42:00 +02:00
|
|
|
"time"
|
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-29 18:26:36 +02:00
|
|
|
"github.com/genofire/hs_master-kss-monolith/runtime"
|
2017-05-19 11:02:19 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
2017-03-30 18:43:18 +02:00
|
|
|
)
|
|
|
|
|
2017-05-03 06:52:14 +02:00
|
|
|
// Function to add goods to the stock
|
2017-04-28 12:05:58 +02:00
|
|
|
func addGood(w http.ResponseWriter, r *http.Request) {
|
2017-03-30 18:43:18 +02:00
|
|
|
log := logger.HTTP(r)
|
2017-05-19 11:02:19 +02:00
|
|
|
|
|
|
|
countStr := r.URL.Query().Get("count")
|
|
|
|
count, err := strconv.Atoi(countStr)
|
|
|
|
if err != nil {
|
|
|
|
count = 0
|
|
|
|
}
|
|
|
|
|
2017-03-30 19:05:33 +02:00
|
|
|
id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64)
|
|
|
|
if err != nil {
|
2017-06-21 15:25:18 +02:00
|
|
|
log.Warn("false product id format")
|
|
|
|
http.Error(w, "the product id has a false format", http.StatusNotAcceptable)
|
2017-03-30 19:05:33 +02:00
|
|
|
return
|
|
|
|
}
|
2017-03-31 10:57:01 +02:00
|
|
|
log = log.WithField("productid", id)
|
2017-04-29 18:26:36 +02:00
|
|
|
ok, err := runtime.ProductExists(id)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err.Error())
|
|
|
|
http.Error(w, err.Error(), http.StatusGatewayTimeout)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ok {
|
2017-06-21 15:25:18 +02:00
|
|
|
log.Warn("product not found")
|
2017-05-03 06:52:14 +02:00
|
|
|
http.Error(w, "the product was not found", http.StatusNotFound)
|
2017-04-29 18:26:36 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var obj models.Good
|
2017-05-12 10:17:27 +02:00
|
|
|
err = lib.Read(r, &obj)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err.Error())
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2017-04-04 23:21:05 +02:00
|
|
|
|
2017-04-28 12:05:58 +02:00
|
|
|
obj.ProductID = id
|
2017-04-07 13:13:37 +02:00
|
|
|
|
2017-05-19 11:02:19 +02:00
|
|
|
var db *gorm.DB
|
|
|
|
if count > 0 {
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
db = database.Write.Create(&obj)
|
|
|
|
obj.ID = 0
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
db = database.Write.Create(&obj)
|
|
|
|
}
|
|
|
|
|
2017-04-28 12:05:58 +02:00
|
|
|
if db.Error != nil {
|
2017-06-21 15:25:18 +02:00
|
|
|
log.Error("database unable to write", db.Error)
|
2017-05-03 06:52:14 +02:00
|
|
|
http.Error(w, "the product could not be written into the database", http.StatusInternalServerError)
|
2017-04-04 23:21:05 +02:00
|
|
|
}
|
2017-04-29 18:26:36 +02:00
|
|
|
lib.Write(w, &obj)
|
2017-04-28 12:05:58 +02:00
|
|
|
|
2017-04-04 23:21:05 +02:00
|
|
|
log.Info("done")
|
|
|
|
}
|
2017-05-18 23:42:00 +02:00
|
|
|
|
|
|
|
// Function that returns the freshness of a good
|
|
|
|
func delGood(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log := logger.HTTP(r)
|
|
|
|
id, err := strconv.ParseInt(pat.Param(r, "goodid"), 10, 64)
|
|
|
|
if err != nil {
|
2017-06-21 15:25:18 +02:00
|
|
|
log.Warn("wrong good id format")
|
2017-05-18 23:42:00 +02:00
|
|
|
http.Error(w, "the good id has a false format", http.StatusNotAcceptable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log = log.WithField("goodid", id)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
var good models.Good
|
|
|
|
good.ID = id
|
2017-05-19 12:41:07 +02:00
|
|
|
db := good.FilterAvailable(database.Read).First(&good)
|
|
|
|
if db.RecordNotFound() {
|
2017-06-21 15:25:18 +02:00
|
|
|
log.Warnf("could not find good: %s", db.Error)
|
|
|
|
http.Error(w, "the good was not found", http.StatusNotFound)
|
2017-05-19 12:41:07 +02:00
|
|
|
return
|
|
|
|
}
|
2017-05-18 23:42:00 +02:00
|
|
|
good.ManuelleDelete = true
|
|
|
|
good.DeletedAt = &now
|
2017-05-19 12:41:07 +02:00
|
|
|
|
|
|
|
db = database.Write.Save(&good)
|
2017-05-18 23:42:00 +02:00
|
|
|
if db.Error != nil {
|
2017-06-21 15:25:18 +02:00
|
|
|
log.Warnf("could not delete good: %s", db.Error)
|
|
|
|
http.Error(w, "the good could not be deleted", http.StatusInternalServerError)
|
2017-05-18 23:42:00 +02:00
|
|
|
return
|
|
|
|
}
|
2017-05-19 12:41:07 +02:00
|
|
|
log.Info("done")
|
2017-05-18 23:42:00 +02:00
|
|
|
}
|