save orders

This commit is contained in:
2024-08-10 11:10:27 +02:00
parent 88be25d77f
commit 8e482aa712
8 changed files with 172 additions and 17 deletions

View File

@ -70,6 +70,8 @@ type Transaction struct {
Amount int `json:"amount"`
Currency string `json:"currency"`
LastFour string `json:"last_four"`
ExpiryMonth int `json:"expiry_month"`
ExpiryYear int `json:"expiry_year"`
BankReturnCode string `json:"bank_return_code"`
TransactionStatusID int `json:"transaction_status_id"`
CreatedAt time.Time `json:"-"`
@ -131,14 +133,17 @@ func (m *DBModel) InsertTransaction(txn Transaction) (int, error) {
defer cancel()
stmt := `INSERT INTO transactions
(amount, currency, last_four, bank_return_code,
(amount, currency, last_four, expiry_month, expiry_year,
bank_return_code,
transaction_status_id, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)`
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
result, err := m.DB.ExecContext(ctx, stmt,
txn.Amount,
txn.Currency,
txn.LastFour,
txn.ExpiryMonth,
txn.ExpiryYear,
txn.BankReturnCode,
txn.TransactionStatusID,
time.Now(),
@ -160,17 +165,45 @@ func (m *DBModel) InsertOrder(order Order) (int, error) {
defer cancel()
stmt := `INSERT INTO orders
(widget_id, transaction_id, status_id, quantity,
(widget_id, transaction_id, customer_id, status_id, quantity,
amount, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)`
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
result, err := m.DB.ExecContext(ctx, stmt,
order.WidgetID,
order.TransactionID,
order.CustomerID,
order.StatusID,
order.Quantity,
order.Amount,
time.Now(),
time.Now(),
)
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
if err != nil {
return 0, err
}
return int(id), nil
}
// InsertCustomer inserts a new customer, and returns its id
func (m *DBModel) InsertCustomer(customer Customer) (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
stmt := `INSERT INTO customers
(first_name, last_name, email, created_at, updated_at)
VALUES (?, ?, ?, ?, ?)`
result, err := m.DB.ExecContext(ctx, stmt,
customer.FirstName,
customer.LastName,
customer.Email,
time.Now(),
time.Now(),
)
if err != nil {
return 0, err