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

58 lines
1.4 KiB
Go
Raw Normal View History

// Package that contains all api routes of this microservice
package http
import (
"net/http"
2017-03-30 19:05:33 +02:00
"strconv"
"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"
2017-04-29 18:26:36 +02:00
"github.com/genofire/hs_master-kss-monolith/runtime"
)
// Function to add goods to the stock
2017-04-28 12:05:58 +02:00
func addGood(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("false productid format")
http.Error(w, "the product id is false", 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 {
log.Warn("false product, product not found")
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-28 12:05:58 +02:00
obj.ProductID = id
2017-04-29 18:26:36 +02:00
db := database.Write.Create(&obj)
2017-04-28 12:05:58 +02:00
if db.Error != nil {
log.Error("database not able to write", db.Error)
http.Error(w, "the product could not be written into the database", http.StatusInternalServerError)
}
2017-04-29 18:26:36 +02:00
lib.Write(w, &obj)
2017-04-28 12:05:58 +02:00
log.Info("done")
}