Exemplo n.º 1
0
func consumeProduct(w http.ResponseWriter, r *http.Request) errors.Http {
	p := models.Product{}
	params := r.URL.Query()
	if err := FillProductIdWithUrlValue(&p, params); err != nil {
		return errors.BadRequest(err.Error())
	}

	qt, err := strconv.Atoi(params.Get("quantity"))

	if err != nil {
		return errors.BadRequest(err.Error())
	}

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

	ps, err := p.Retreive(db)
	if err != nil {
		return errors.InternalServerError(err.Error())
	}

	if len(ps) != 1 {
		return errors.InternalServerError("[ERROR] Unexpected error occured during exeuction of /api/inventory/product/:id/consume/:quantity")
	}

	rend.JSON(w, http.StatusOK, ps[0])
	return nil
}
Exemplo n.º 2
0
func deleteProduct(w http.ResponseWriter, r *http.Request) errors.Http {
	p := models.Product{}
	if err := FillProductIdWithUrlValue(&p, r.URL.Query()); err != nil {
		return errors.BadRequest(err.Error())
	}

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

	rend.JSON(w, http.StatusOK, p)
	return nil
}
Exemplo n.º 3
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
}
Exemplo n.º 4
0
func retreiveProduct(w http.ResponseWriter, r *http.Request) errors.Http {
	p := models.Product{}
	if err := BuildStructFromQueryString(&p, r.URL.Query()); err != nil {
		return errors.BadRequest(err.Error())
	}

	products, err := p.Retreive(db)
	if err != nil {
		return errors.InternalServerError(err.Error())
	}

	if len(products) == 0 {
		return errors.NotFound("record not found")
	}
	rend.JSON(w, http.StatusOK, products)

	return nil
}
Exemplo n.º 5
0
func FillProductIdWithUrlValue(p *models.Product, params url.Values) error {
	id, err := strconv.Atoi(params.Get("id"))
	if err != nil {
		return err
	}
	p.ID = id

	return nil
}
Exemplo n.º 6
0
/*
*	Senders
 */
func sendProductCreated(m *models.Medication) {
	topic, _ := common_io.BuildTopicFromCommonEvent(common_io.EVENT_CREATED, "product")
	p := warehouseModels.Product{}

	/*
	*	Bind
	 */
	p.ID = m.ID
	p.Name = m.Name

	/*
	*	json Marshal
	 */
	b, err := json.Marshal(&p)
	if err != nil {
		fmt.Println("[ERROR] ", err.Error())
		return
	}

	producer.Publish(topic, b)
}
Exemplo n.º 7
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)
	}
}