inserting a new transaction
This commit is contained in:
parent
85cd4dfc1f
commit
46386b19a3
@ -113,3 +113,31 @@ func (m *DBModel) GetWidget(id int) (Widget, error) {
|
||||
}
|
||||
return widget, nil
|
||||
}
|
||||
|
||||
// InsertTransaction inserts a new txn, and returns its id
|
||||
func (m *DBModel) InsertTransaction(txn Transaction) (int, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stmt := `INSERT INTO transactions
|
||||
(amount, currency, last_four, bank_return_code,
|
||||
transaction_status_id, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
||||
|
||||
result, err := m.DB.ExecContext(ctx, stmt,
|
||||
txn.Amount,
|
||||
txn.Currency,
|
||||
txn.LastFour,
|
||||
txn.BankReturnCode,
|
||||
txn.TransactionStatusID,
|
||||
txn.CreatedAt,
|
||||
txn.UpdatedAt)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(id), nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user