Merge pull request 'Retourner transactions lors de leur insertion' (#8) from feature/return-inserted-rows into main
Reviewed-on: #8
This commit is contained in:
commit
fccca1f339
3 changed files with 18 additions and 16 deletions
23
data/data.go
23
data/data.go
|
@ -72,9 +72,9 @@ func (d *DataClient) Seed() (int64, error) {
|
||||||
return rows, nil
|
return rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertTransactions inserts a slice of Transaction into a database, returning the amount inserted and any error encountered
|
// InsertTransactions inserts a slice of Transaction into a database, returning the transactions inserted and any error encountered
|
||||||
func (d *DataClient) InsertTransactions(transactions []models.Transaction) (int64, error) {
|
func (d *DataClient) InsertTransactions(transactions []models.Transaction) ([]models.Transaction, error) {
|
||||||
var rowsInserted int64
|
var rowsInserted []models.Transaction
|
||||||
|
|
||||||
// Start transaction
|
// Start transaction
|
||||||
tx, err := d.DB.Beginx()
|
tx, err := d.DB.Beginx()
|
||||||
|
@ -87,22 +87,25 @@ func (d *DataClient) InsertTransactions(transactions []models.Transaction) (int6
|
||||||
// Check values
|
// Check values
|
||||||
if transaction.MembreID == "" {
|
if transaction.MembreID == "" {
|
||||||
tx.Rollback()
|
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 {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return 0, err
|
return rowsInserted, err
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
rows, err := result.RowsAffected()
|
for rows.Next() {
|
||||||
if err != nil {
|
var transactionRow models.Transaction
|
||||||
|
if err := rows.Scan(&transactionRow.ID, &transactionRow.MembreID, &transactionRow.IsPerpetual); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return 0, err
|
return rowsInserted, err
|
||||||
}
|
}
|
||||||
|
|
||||||
rowsInserted += rows
|
rowsInserted = append(rowsInserted, transactionRow)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = tx.Commit()
|
err = tx.Commit()
|
||||||
|
|
|
@ -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 {
|
if err != nil {
|
||||||
response.Message = fmt.Sprintf("Error during client.InsertTransactions(): %s", err)
|
response.Message = fmt.Sprintf("Error during client.InsertTransactions(): %s", err)
|
||||||
|
|
||||||
return c.JSON(statusCode, response)
|
return c.JSON(statusCode, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
response.Data.RowsInserted = rows
|
|
||||||
|
|
||||||
statusCode = http.StatusCreated
|
statusCode = http.StatusCreated
|
||||||
response.Message = "Insert successful"
|
response.Message = "Insert successful"
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ type GetMembreResponse struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostTransactionsResponseData struct {
|
type PostTransactionsResponseData struct {
|
||||||
RowsInserted int64 `json:"rows_inserted"`
|
Transactions []models.Transaction `json:"transactions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostTransactionsResponse struct {
|
type PostTransactionsResponse struct {
|
||||||
|
|
Loading…
Reference in a new issue