Merge pull request 'format(db): aérer et standardiser requêtes SQL dans DataClient' (#42) from vlbeaudoin/format/dataclient-sql into main

Reviewed-on: #42
This commit is contained in:
Victor Lacasse-Beaudoin 2024-09-16 14:51:23 -04:00
commit 1a1bf08dbb

View file

@ -90,7 +90,24 @@ func (d *DataClient) InsertTransactions(transactions []models.Transaction) ([]mo
return rowsInserted, errors.New("Impossible d'insérer une transaction sans membre_id") return rowsInserted, errors.New("Impossible d'insérer une transaction sans membre_id")
} }
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) 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 {
return rowsInserted, err return rowsInserted, err
} }
@ -117,8 +134,16 @@ func (d *DataClient) InsertTransactions(transactions []models.Transaction) ([]mo
func (d *DataClient) GetTransaction(membreID string, is_perpetual bool) (models.Transaction, error) { func (d *DataClient) GetTransaction(membreID string, is_perpetual bool) (models.Transaction, error) {
var transaction models.Transaction 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, `
err := d.DB.Get(&transaction, "SELECT * FROM transactions WHERE membre_id = $1 AND is_perpetual = $2 LIMIT 1;", membreID, is_perpetual) SELECT
*
FROM
transactions
WHERE
membre_id = $1 AND
is_perpetual = $2
LIMIT 1;
`, membreID, is_perpetual)
if err != nil { if err != nil {
return transaction, err return transaction, err
} }
@ -132,7 +157,13 @@ func (d *DataClient) GetTransaction(membreID string, is_perpetual bool) (models.
func (d *DataClient) ListTransactions() ([]models.Transaction, error) { func (d *DataClient) ListTransactions() ([]models.Transaction, error) {
var transactions []models.Transaction var transactions []models.Transaction
if err := d.DB.Select(&transactions, "SELECT * FROM transactions LIMIT 20000;"); err != nil { if err := d.DB.Select(&transactions, `
SELECT
*
FROM
transactions
LIMIT 20000;
`); err != nil {
return transactions, err return transactions, err
} }