From 6f5b8733b6f9462b2f7c538416e8f37574f1cda6 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Thu, 8 Jun 2023 01:31:34 -0400 Subject: [PATCH] Retourner transactions lors de leur insertion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rendre disponible les id de transactions officiellement ajoutées lors d'un '201 Created' sur 'POST /v2/transactions http/1.1' dans le response body sous 'application/json: data.transactions'. --- data/data.go | 27 +++++++++++++++------------ handlers/transaction.go | 5 ++--- responses/responses.go | 2 +- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/data/data.go b/data/data.go index be809fb..42e3a90 100644 --- a/data/data.go +++ b/data/data.go @@ -72,9 +72,9 @@ func (d *DataClient) Seed() (int64, error) { return rows, nil } -// InsertTransactions inserts a slice of Transaction into a database, returning the amount inserted and any error encountered -func (d *DataClient) InsertTransactions(transactions []models.Transaction) (int64, error) { - var rowsInserted int64 +// InsertTransactions inserts a slice of Transaction into a database, returning the transactions inserted and any error encountered +func (d *DataClient) InsertTransactions(transactions []models.Transaction) ([]models.Transaction, error) { + var rowsInserted []models.Transaction // Start transaction tx, err := d.DB.Beginx() @@ -87,22 +87,25 @@ func (d *DataClient) InsertTransactions(transactions []models.Transaction) (int6 // Check values if transaction.MembreID == "" { tx.Rollback() - return 0, errors.New("Cannot insert transaction with no membre_id") + return rowsInserted, errors.New("Cannot insert transaction with no membre_id") } - result, err := tx.NamedExec("INSERT INTO transactions (membre_id, given_at, is_perpetual) VALUES (:membre_id, current_timestamp, :is_perpetual);", &transaction) + rows, err := tx.NamedQuery("INSERT INTO transactions (membre_id, given_at, is_perpetual) VALUES (:membre_id, current_timestamp, :is_perpetual) RETURNING id, membre_id, is_perpetual;", &transaction) if err != nil { tx.Rollback() - return 0, err + return rowsInserted, err } + defer rows.Close() - rows, err := result.RowsAffected() - if err != nil { - tx.Rollback() - return 0, err + for rows.Next() { + var transactionRow models.Transaction + if err := rows.Scan(&transactionRow.ID, &transactionRow.MembreID, &transactionRow.IsPerpetual); err != nil { + tx.Rollback() + return rowsInserted, err + } + + rowsInserted = append(rowsInserted, transactionRow) } - - rowsInserted += rows } err = tx.Commit() diff --git a/handlers/transaction.go b/handlers/transaction.go index b9141e1..9b313d6 100644 --- a/handlers/transaction.go +++ b/handlers/transaction.go @@ -96,15 +96,14 @@ func PostTransactions(c echo.Context) error { } } - rows, err := client.InsertTransactions(transactions) + insertedTransactions, err := client.InsertTransactions(transactions) + response.Data.Transactions = insertedTransactions if err != nil { response.Message = fmt.Sprintf("Error during client.InsertTransactions(): %s", err) return c.JSON(statusCode, response) } - response.Data.RowsInserted = rows - statusCode = http.StatusCreated response.Message = "Insert successful" diff --git a/responses/responses.go b/responses/responses.go index 1a9a212..fcadcce 100644 --- a/responses/responses.go +++ b/responses/responses.go @@ -19,7 +19,7 @@ type GetMembreResponse struct { } type PostTransactionsResponseData struct { - RowsInserted int64 `json:"rows_inserted"` + Transactions []models.Transaction `json:"transactions"` } type PostTransactionsResponse struct { -- 2.45.2