102 lines
2.1 KiB
Go
102 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
"git.agecem.com/bottin/agendas/queries"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type DBClient struct {
|
|
Pool *pgxpool.Pool
|
|
}
|
|
|
|
func (d DBClient) Ping(ctx context.Context) error {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
default:
|
|
if d.Pool == nil {
|
|
return fmt.Errorf("db: cannot check health using nil connection pool")
|
|
}
|
|
|
|
return d.Pool.Ping(ctx)
|
|
}
|
|
}
|
|
|
|
func (d DBClient) Init(ctx context.Context) error {
|
|
//TODO check context is not closed
|
|
//TODO check *DB is not nil
|
|
log.Println("warning: DBClient [Init] not properly checked")
|
|
// Init
|
|
if _, err := d.Pool.Exec(ctx, queries.SQLSchema()); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d DBClient) CreateTransaction(ctx context.Context, transaction Transaction) error {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
default:
|
|
if d.Pool == nil {
|
|
return fmt.Errorf("db: cannot check health using nil connection pool")
|
|
}
|
|
|
|
// CreateTransaction
|
|
commandTag, err := d.Pool.Exec(ctx, `
|
|
INSERT INTO "transactions" (
|
|
membre_id,
|
|
is_perpetual
|
|
)
|
|
VALUES (
|
|
$1,
|
|
$2
|
|
);
|
|
`, transaction.MembreID, transaction.IsPerpetual)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if commandTag.RowsAffected() == 0 {
|
|
return fmt.Errorf("No rows inserted")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (d DBClient) ReadTransaction(ctx context.Context, membreID string, isPerpetual bool) (Transaction, error) {
|
|
select {
|
|
case <-ctx.Done():
|
|
return Transaction{}, ctx.Err()
|
|
default:
|
|
if d.Pool == nil {
|
|
return Transaction{}, fmt.Errorf("db: cannot check health using nil connection pool")
|
|
}
|
|
|
|
//TODO ReadTransaction
|
|
return Transaction{}, fmt.Errorf("db: ReadTransaction not implemented")
|
|
}
|
|
}
|
|
|
|
func (d DBClient) CountTransactions(ctx context.Context) (membresSansAgenda, membresAvecPerpetuel, membresAvecNonPerpetuel, membresAvecTout int, err error) {
|
|
select {
|
|
case <-ctx.Done():
|
|
err = ctx.Err()
|
|
return
|
|
default:
|
|
if d.Pool == nil {
|
|
err = fmt.Errorf("db: cannot check health using nil connection pool")
|
|
return
|
|
}
|
|
|
|
//TODO CountTransactions
|
|
err = fmt.Errorf("db: CountTransactions not implemented")
|
|
return
|
|
}
|
|
}
|