Ejemplo n.º 1
0
func insertProduct(w http.ResponseWriter, r *http.Request) errors.Http {
	p := models.Product{}
	if err := BuildStructFromReqBody(&p, r.Body); err != nil {
		return errors.BadRequest(err.Error())
	}

	if err := p.Save(db); err != nil {
		return errors.InternalServerError(err.Error())
	}

	rend.JSON(w, http.StatusOK, p)
	return nil
}
Ejemplo n.º 2
0
/*
*	Handlers
 */
func handleProductCreated(msg []byte) {
	fmt.Println("[INFO] Received Kafka message from topic 'product_created'")
	p := models.Product{}
	if err := json.Unmarshal(msg, &p); err != nil {
		fmt.Println("[ERROR] Unable to Unmarshal json from message 'product_created'", err.Error())
		return
	}
	p.CurrQuantity = 100000
	p.MinQuantity = 90

	rand.Seed(time.Now().UTC().UnixNano())
	p.CurrentValue = float64(rand.Intn(10)) / 10.0

	if err := p.Save(db); err != nil {
		producer.Publish("product_created_dead_letter", msg)
	}
}