Esempio n. 1
0
func cancelOrder(w http.ResponseWriter, r *http.Request) errors.Http {
	order := models.Order{}

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

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

	rend.JSON(w, http.StatusOK, order.ID)
	return nil
}
Esempio n. 2
0
func FillOrderIdWithUrlValue(o *models.Order, params url.Values) error {
	id, err := strconv.Atoi(params.Get("id"))
	if err != nil {
		return err
	}
	o.ID = id
	return nil
}
Esempio n. 3
0
func retreiveOrderById(w http.ResponseWriter, r *http.Request) errors.Http {
	o := models.Order{}

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

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

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

	rend.JSON(w, http.StatusOK, orders[0])
	return nil
}
Esempio n. 4
0
func retreiveOrder(w http.ResponseWriter, r *http.Request) errors.Http {
	queryString := r.URL.Query()
	var o models.Order
	decoder := decoder.NewDecoder()

	if err := decoder.DecodeURLValues(&o, queryString); err != nil {
		return errors.BadRequest(err.Error())
	}

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

	if len(orders) == 0 {
		return errors.NotFound("record not found")
	}

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