inserting a new transaction

This commit is contained in:
vinchent 2024-08-07 12:07:21 +02:00
parent 85cd4dfc1f
commit 46386b19a3

View File

@ -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
}