2017-03-30 18:43:18 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-03-30 19:05:33 +02:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"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-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-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-28 12:05:58 +02:00
|
|
|
var obj *models.Good
|
|
|
|
lib.Read(r, obj)
|
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-04-28 12:05:58 +02:00
|
|
|
db := database.Write.Create(obj)
|
|
|
|
if db.Error != nil {
|
|
|
|
log.Error("database could not write", db.Error)
|
|
|
|
http.Error(w, "was not possible to write", http.StatusInternalServerError)
|
2017-04-04 23:21:05 +02:00
|
|
|
}
|
2017-04-28 12:05:58 +02:00
|
|
|
lib.Write(w, obj)
|
|
|
|
|
2017-04-04 23:21:05 +02:00
|
|
|
log.Info("done")
|
|
|
|
}
|