wip
This commit is contained in:
parent
294c6221aa
commit
1e19ab222e
2 changed files with 81 additions and 22 deletions
68
db.go
68
db.go
|
@ -39,22 +39,64 @@ func (d DBClient) Init(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d DBClient) CreateTransaction(ctx context.Context, transaction Transaction) error {
|
func (d DBClient) CreateTransaction(ctx context.Context, transaction Transaction) error {
|
||||||
//TODO check context is not closed
|
select {
|
||||||
//TODO check *DB is not nil
|
case <-ctx.Done():
|
||||||
//TODO CreateTransaction
|
return ctx.Err()
|
||||||
return fmt.Errorf("db: CreateTransaction not implemented")
|
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) error {
|
func (d DBClient) ReadTransaction(ctx context.Context, membreID string, isPerpetual bool) (Transaction, error) {
|
||||||
//TODO check context is not closed
|
select {
|
||||||
//TODO check *DB is not nil
|
case <-ctx.Done():
|
||||||
//TODO ReadTransaction
|
return Transaction{}, ctx.Err()
|
||||||
return fmt.Errorf("db: ReadTransaction not implemented")
|
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) {
|
func (d DBClient) CountTransactions(ctx context.Context) (membresSansAgenda, membresAvecPerpetuel, membresAvecNonPerpetuel, membresAvecTout int, err error) {
|
||||||
//TODO check context is not closed
|
select {
|
||||||
//TODO check *DB is not nil
|
case <-ctx.Done():
|
||||||
//TODO CountTransactions
|
err = ctx.Err()
|
||||||
return 0, 0, 0, 0, fmt.Errorf("db: CountTransactions not implemented")
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
35
handler.go
35
handler.go
|
@ -100,6 +100,21 @@ func UICreateTransaction(ctx context.Context, cfg Config, bottinClient *bottin.A
|
||||||
return fmt.Errorf("👎 Aucun numéro étudiant sélectionné. Assurez-vous de cliquer sur la case 'Numéro étudiant:' avant de scanner.")
|
return fmt.Errorf("👎 Aucun numéro étudiant sélectionné. Assurez-vous de cliquer sur la case 'Numéro étudiant:' avant de scanner.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO check if membre already received before checking bottin
|
||||||
|
|
||||||
|
// check if membre exists in bottin
|
||||||
|
membreResponse, err := bottinClient.ReadMembre(ctx, membreID)
|
||||||
|
if err != nil {
|
||||||
|
if err.Error() == "400 no rows in result set" {
|
||||||
|
return fmt.Errorf("👎 Numéro étudiant introuvable %s", membreID)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if membreResponse.Data.Membre.ID != membreID {
|
||||||
|
return fmt.Errorf("👎 Bottin a retourné '%s' en demandant '%s'", membreID, membreResponse.Data.Membre.ID)
|
||||||
|
}
|
||||||
|
|
||||||
// dbclient.CreateTransaction
|
// dbclient.CreateTransaction
|
||||||
if err := dbClient.CreateTransaction(ctx, Transaction{
|
if err := dbClient.CreateTransaction(ctx, Transaction{
|
||||||
MembreID: membreID,
|
MembreID: membreID,
|
||||||
|
@ -109,16 +124,18 @@ func UICreateTransaction(ctx context.Context, cfg Config, bottinClient *bottin.A
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare result message
|
// Prepare result message
|
||||||
var typeAgenda string
|
d.Result = fmt.Sprintf("👍 Membre %s peut recevoir son agenda %s",
|
||||||
if isPerpetual {
|
membreID,
|
||||||
typeAgenda = "perpétuel"
|
func() string {
|
||||||
} else {
|
if isPerpetual {
|
||||||
typeAgenda = "non-perpétuel"
|
return "perpétuel"
|
||||||
}
|
} else {
|
||||||
|
return "non-perpétuel"
|
||||||
|
}
|
||||||
|
}(),
|
||||||
|
)
|
||||||
|
|
||||||
d.Result = fmt.Sprintf("👍 Membre %s peut recevoir son agenda %s", membreID, typeAgenda)
|
return nil
|
||||||
|
|
||||||
return fmt.Errorf("UIIndexPOST not fully implemented")
|
|
||||||
}(); err != nil {
|
}(); err != nil {
|
||||||
d.Error = err.Error()
|
d.Error = err.Error()
|
||||||
log.Println("err:", d.Error)
|
log.Println("err:", d.Error)
|
||||||
|
|
Loading…
Reference in a new issue