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 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
}