Example #1
0
func CreateExpense(l logger.Logger, con db.Con, params CreateExpenseParams) (*model.Expense, error) {
	expense := model.NewExpense()

	expense.Description = params.Description
	expense.Amount = params.Amount

	err := validate.Expense(expense)
	if err != nil {
		return nil, err
	}

	err = db.InsertExpense(con, expense)
	if err != nil {
		return nil, err
	}

	return expense, nil
}
Example #2
0
func TestInsertExpense(t *testing.T) {
	tx := BeginTx()
	defer tx.Rollback()

	expense := model.NewExpense()
	expense.Description = "some description"
	expense.Amount = 23
	err := db.InsertExpense(tx, expense)
	assert.MustBeNil(t, err)

	var amount int
	var description string
	row := tx.QueryRow("SELECT description, amount FROM expenses")
	err = row.Scan(&description, &amount)

	assert.MustBeNil(t, err)
	assert.MustBeEqual(t, "some description", description)
	assert.MustBeEqual(t, 23, amount)
}
func (f *ExpenseFabricator) Exec() (*model.Expense, error) {
	return f.expense, db.InsertExpense(f.con, f.expense)
}