コード例 #1
0
func CreateExpense(rw http.ResponseWriter, r *http.Request, c *ctx.Context) error {
	form, err := form.New(r)
	if err != nil {
		return httperr.BadRequest(err.Error())
	}

	params := service.CreateExpenseParams{
		Description: form.ReqString("description"),
		Amount:      form.ReqInt("amount"),
	}

	if err := form.Err(); err != nil {
		return err
	}

	con, err := c.TxManager.Begin()
	if err != nil {
		return err
	}

	expense, err := service.CreateExpense(c.Logger, con, params)
	if err != nil {
		return err
	}

	rw.WriteHeader(http.StatusCreated)
	return serialize.Expense(rw, expense)
}
コード例 #2
0
func CreateExpense(rw http.ResponseWriter, r *http.Request, c *ctx.Context) error {
	form, err := form.New(r)
	if err != nil {
		return httperr.BadRequest(err.Error())
	}

	params := service.CreateExpenseParams{
		Description: form.ReqString("description"),
		Amount:      form.ReqInt("amount"),
	}

	con, err := c.TxManager.Begin()
	if err != nil {
		return err
	}

	_, err = service.CreateExpense(c.Logger, con, params)
	if err != nil {
		ve, ok := apperrors.IsValidationError(err)
		if !ok {
			return err
		}
		view := &views.ExpenseForm{
			Description: params.Description,
			Amount:      params.Amount,
			Errors:      ve.FieldErrors,
		}
		return views.RenderWithLayout(rw, view)
	}

	http.Redirect(rw, r, "/expenses", 302)
	return nil
}