Ajouter transactions

Ajouter POST /v2/transactions

Valide si une transaction avec la même combinaison membre_id + is_perpetual existe déjà dans une des transactions proposées.
This commit is contained in:
Victor Lacasse-Beaudoin 2023-06-06 02:22:57 -04:00
parent e842982a46
commit 7fa4db7ab9
5 changed files with 145 additions and 8 deletions

View file

@ -1,6 +1,7 @@
package data
import (
"errors"
"fmt"
"git.agecem.com/agecem/bottin-agenda/models"
@ -71,6 +72,63 @@ 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
// Start transaction
tx, err := d.DB.Beginx()
if err != nil {
tx.Rollback()
return rowsInserted, err
}
for _, transaction := range transactions {
// Check values
if transaction.MembreID == "" {
tx.Rollback()
return 0, 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)
if err != nil {
tx.Rollback()
return 0, err
}
rows, err := result.RowsAffected()
if err != nil {
tx.Rollback()
return 0, err
}
rowsInserted += rows
}
err = tx.Commit()
if err != nil {
return rowsInserted, err
}
return rowsInserted, nil
}
func (d *DataClient) GetTransaction(membreID string, is_perpetual bool) (models.Transaction, error) {
var transaction models.Transaction
//err := d.DB.NamedQuery("SELECT * FROM transactions WHERE membre_id=:membre_id AND is_perpetual=:is_perpetual LIMIT 1;"
err := d.DB.Get(&transaction, "SELECT * FROM transactions WHERE membre_id = $1 AND is_perpetual = $2 LIMIT 1;", membreID, is_perpetual)
if err != nil {
return transaction, err
}
if transaction.ID == "" {
return transaction, fmt.Errorf("No transaction found")
}
return transaction, nil
}
/*
// InsertMembres inserts a slice of Membre into a database, returning the amount inserted and any error encountered
func (d *DataClient) InsertMembres(membres []models.Membre) (int64, error) {