2023-02-17 15:37:36 -05:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
2023-06-06 02:22:57 -04:00
|
|
|
"errors"
|
2023-05-29 17:58:23 -04:00
|
|
|
"fmt"
|
2023-02-17 15:37:36 -05:00
|
|
|
|
2023-05-29 17:58:23 -04:00
|
|
|
"git.agecem.com/agecem/bottin-agenda/models"
|
2024-09-16 14:42:32 -04:00
|
|
|
"git.agecem.com/agecem/bottin-agenda/sql"
|
2023-05-29 17:58:23 -04:00
|
|
|
_ "github.com/jackc/pgx/stdlib"
|
|
|
|
"github.com/jmoiron/sqlx"
|
2023-06-03 20:16:41 -04:00
|
|
|
"github.com/spf13/viper"
|
2023-02-17 15:37:36 -05:00
|
|
|
)
|
|
|
|
|
2023-05-29 17:58:23 -04:00
|
|
|
// DataClient is a postgres client based on sqlx
|
|
|
|
type DataClient struct {
|
|
|
|
PostgresConnection PostgresConnection
|
|
|
|
DB sqlx.DB
|
|
|
|
}
|
2023-02-17 15:37:36 -05:00
|
|
|
|
2023-05-29 17:58:23 -04:00
|
|
|
type PostgresConnection struct {
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Database string
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
SSL bool
|
2023-02-17 15:37:36 -05:00
|
|
|
}
|
|
|
|
|
2023-06-03 20:16:41 -04:00
|
|
|
func NewDataClientFromViper() (*DataClient, error) {
|
|
|
|
client, err := NewDataClient(PostgresConnection{
|
|
|
|
User: viper.GetString("db.user"),
|
|
|
|
Password: viper.GetString("db.password"),
|
|
|
|
Host: viper.GetString("db.host"),
|
|
|
|
Port: viper.GetInt("db.port"),
|
|
|
|
Database: viper.GetString("db.database"),
|
|
|
|
})
|
|
|
|
|
|
|
|
return client, err
|
|
|
|
}
|
|
|
|
|
2023-05-29 17:58:23 -04:00
|
|
|
func NewDataClient(connection PostgresConnection) (*DataClient, error) {
|
|
|
|
client := &DataClient{PostgresConnection: connection}
|
2023-02-17 15:37:36 -05:00
|
|
|
|
2023-05-29 17:58:23 -04:00
|
|
|
connectionString := fmt.Sprintf("postgres://%s:%s@%s:%d/%s",
|
|
|
|
client.PostgresConnection.User,
|
|
|
|
client.PostgresConnection.Password,
|
|
|
|
client.PostgresConnection.Host,
|
|
|
|
client.PostgresConnection.Port,
|
|
|
|
client.PostgresConnection.Database,
|
|
|
|
)
|
2023-02-17 15:37:36 -05:00
|
|
|
|
2023-05-29 17:58:23 -04:00
|
|
|
db, err := sqlx.Connect("pgx", connectionString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-17 15:37:36 -05:00
|
|
|
|
2023-05-29 17:58:23 -04:00
|
|
|
client.DB = *db
|
2023-02-17 15:37:36 -05:00
|
|
|
|
2023-05-29 17:58:23 -04:00
|
|
|
return client, nil
|
|
|
|
}
|
2023-02-17 15:37:36 -05:00
|
|
|
|
2023-05-29 17:58:23 -04:00
|
|
|
func (d *DataClient) Seed() (int64, error) {
|
2024-09-16 14:42:32 -04:00
|
|
|
result, err := d.DB.Exec(sql.Schema())
|
2023-05-29 17:58:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-02-17 15:37:36 -05:00
|
|
|
|
2023-05-29 17:58:23 -04:00
|
|
|
rows, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return rows, err
|
2023-02-17 15:37:36 -05:00
|
|
|
}
|
|
|
|
|
2023-05-29 17:58:23 -04:00
|
|
|
return rows, nil
|
|
|
|
}
|
|
|
|
|
2023-06-08 01:31:34 -04:00
|
|
|
// 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
|
2023-06-06 02:22:57 -04:00
|
|
|
|
|
|
|
// Start transaction
|
|
|
|
tx, err := d.DB.Beginx()
|
|
|
|
if err != nil {
|
|
|
|
return rowsInserted, err
|
|
|
|
}
|
2023-12-28 12:54:25 -05:00
|
|
|
defer tx.Rollback()
|
2023-06-06 02:22:57 -04:00
|
|
|
|
|
|
|
for _, transaction := range transactions {
|
|
|
|
// Check values
|
|
|
|
if transaction.MembreID == "" {
|
2023-06-10 00:07:51 -04:00
|
|
|
return rowsInserted, errors.New("Impossible d'insérer une transaction sans membre_id")
|
2023-06-06 02:22:57 -04:00
|
|
|
}
|
|
|
|
|
2024-09-16 14:49:56 -04:00
|
|
|
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)
|
2023-06-06 02:22:57 -04:00
|
|
|
if err != nil {
|
2023-06-08 01:31:34 -04:00
|
|
|
return rowsInserted, err
|
2023-06-06 02:22:57 -04:00
|
|
|
}
|
2023-06-08 01:31:34 -04:00
|
|
|
defer rows.Close()
|
2023-06-06 02:22:57 -04:00
|
|
|
|
2023-06-08 01:31:34 -04:00
|
|
|
for rows.Next() {
|
|
|
|
var transactionRow models.Transaction
|
|
|
|
if err := rows.Scan(&transactionRow.ID, &transactionRow.MembreID, &transactionRow.IsPerpetual); err != nil {
|
|
|
|
return rowsInserted, err
|
|
|
|
}
|
2023-06-06 02:22:57 -04:00
|
|
|
|
2023-06-08 01:31:34 -04:00
|
|
|
rowsInserted = append(rowsInserted, transactionRow)
|
|
|
|
}
|
2023-06-06 02:22:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-09-16 14:49:56 -04:00
|
|
|
err := d.DB.Get(&transaction, `
|
|
|
|
SELECT
|
|
|
|
*
|
|
|
|
FROM
|
|
|
|
transactions
|
|
|
|
WHERE
|
|
|
|
membre_id = $1 AND
|
|
|
|
is_perpetual = $2
|
|
|
|
LIMIT 1;
|
|
|
|
`, membreID, is_perpetual)
|
2023-06-06 02:22:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return transaction, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if transaction.ID == "" {
|
2023-06-10 00:07:51 -04:00
|
|
|
return transaction, fmt.Errorf("Aucune transaction trouvée")
|
2023-06-06 02:22:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return transaction, nil
|
|
|
|
}
|
|
|
|
|
2023-06-08 00:53:05 -04:00
|
|
|
func (d *DataClient) ListTransactions() ([]models.Transaction, error) {
|
|
|
|
var transactions []models.Transaction
|
2024-09-16 14:49:56 -04:00
|
|
|
if err := d.DB.Select(&transactions, `
|
|
|
|
SELECT
|
|
|
|
*
|
|
|
|
FROM
|
|
|
|
transactions
|
|
|
|
LIMIT 20000;
|
|
|
|
`); err != nil {
|
2023-06-08 00:53:05 -04:00
|
|
|
return transactions, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return transactions, nil
|
|
|
|
}
|