コード例 #1
0
func FillPurchaseProductProductIdWithUrlValue(p *models.PurchaseProduct, params url.Values) error {
	id, err := strconv.Atoi(params.Get("product_id"))
	if err != nil {
		return err
	}
	p.ProductId = id

	return nil
}
コード例 #2
0
func retreivePurchaseProducts(w http.ResponseWriter, r *http.Request) errors.Http {
	pp := models.PurchaseProduct{}
	if err := BuildStructFromQueryString(&pp, r.URL.Query()); err != nil {
		return errors.BadRequest(err.Error())
	}

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

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

	return nil
}
コード例 #3
0
func updatePurchaseProductOnQuantity(w http.ResponseWriter, r *http.Request) errors.Http {
	pp := models.PurchaseProduct{}

	if err := FillPurchaseProductIdWithUrlValue(&pp, r.URL.Query()); err != nil {
		return errors.BadRequest(err.Error())
	}

	quantity, err := strconv.Atoi(r.URL.Query().Get("quantity"))
	if err != nil {
		errors.BadRequest(err.Error())
	}

	if err := pp.UpdateQuantity(db, quantity); err != nil {
		return errors.InternalServerError(err.Error())
	}

	rend.JSON(w, http.StatusOK, pp)
	return nil
}
コード例 #4
0
func retreivePurchaseProductsById(w http.ResponseWriter, r *http.Request) errors.Http {
	pp := models.PurchaseProduct{}

	if err := FillPurchaseProductIdWithUrlValue(&pp, r.URL.Query()); err != nil {
		return errors.BadRequest(err.Error())
	}

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

	if len(pproducts) != 1 {
		return errors.NotFound("record not found")
	}

	rend.JSON(w, http.StatusOK, pproducts[0])
	return nil
}
コード例 #5
0
func updatePurchaseProductOnValue(w http.ResponseWriter, r *http.Request) errors.Http {
	pp := models.PurchaseProduct{}

	if err := FillPurchaseProductIdWithUrlValue(&pp, r.URL.Query()); err != nil {
		return errors.BadRequest(err.Error())
	}

	value, err := strconv.ParseFloat(r.URL.Query().Get("value"), 64)
	if err != nil {
		errors.BadRequest(err.Error())
	}

	if err := pp.UpdateValue(db, value); err != nil {
		return errors.InternalServerError(err.Error())
	}

	rend.JSON(w, http.StatusOK, pp)
	return nil
}