Пример #1
0
func GetPrice(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params) string {
	var c customer.Price
	var err error

	id := r.FormValue("id")
	if id == "" {
		id = params["id"]
	}

	if c.ID, err = strconv.Atoi(id); err != nil {
		apierror.GenerateError("Trouble getting price ID", err, rw, r)
		return ""
	}

	if err = c.Get(); err != nil {
		apierror.GenerateError("Trouble getting price", err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(c))
}
Пример #2
0
func CreateUpdatePrice(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params) string {
	var w customer.Price
	var err error

	if r.FormValue("id") != "" || params["id"] != "" {
		id := r.FormValue("id")
		if id == "" {
			id = params["id"]
		}

		if w.ID, err = strconv.Atoi(id); err != nil {
			apierror.GenerateError("Trouble getting price ID", err, rw, r)
			return ""
		}

		if err = w.Get(); err != nil {
			apierror.GenerateError("Trouble getting price", err, rw, r)
			return ""
		}
	}

	custID := r.FormValue("custID")
	partID := r.FormValue("partID")
	price := r.FormValue("price")
	isSale := r.FormValue("isSale")
	saleStart := r.FormValue("saleStart")
	saleEnd := r.FormValue("saleEnd")

	if custID != "" {
		if w.CustID, err = strconv.Atoi(custID); err != nil {
			apierror.GenerateError("Trouble getting customer ID", err, rw, r)
			return ""
		}
	}

	if partID != "" {
		if w.PartID, err = strconv.Atoi(partID); err != nil {
			apierror.GenerateError("Trouble getting part ID", err, rw, r)
			return ""
		}
	}

	if price != "" {
		if w.Price, err = strconv.ParseFloat(price, 64); err != nil {
			apierror.GenerateError("Trouble getting price", err, rw, r)
			return ""
		}
	}

	if isSale != "" {
		saleBool := false
		if saleBool, err = strconv.ParseBool(isSale); err != nil {
			apierror.GenerateError("Trouble setting sale", err, rw, r)
			return ""
		}
		if saleBool {
			w.IsSale = 1
		}
	}

	if saleStart != "" {
		if w.SaleStart, err = time.Parse(inputTimeFormat, saleStart); err != nil {
			apierror.GenerateError("Trouble getting sale start", err, rw, r)
			return ""
		}
	}

	if saleEnd != "" {
		if w.SaleEnd, err = time.Parse(inputTimeFormat, saleEnd); err != nil {
			apierror.GenerateError("Trouble getting sale end", err, rw, r)
			return ""
		}
	}

	if w.ID > 0 {
		err = w.Update()
	} else {
		err = w.Create()
	}

	if err != nil {
		msg := "Trouble creating customer price"
		if w.ID > 0 {
			msg = "Trouble updating customer price"
		}
		apierror.GenerateError(msg, err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(w))
}