Produit minimal viable #1
2 changed files with 81 additions and 22 deletions
64
db.go
64
db.go
|
@ -39,22 +39,64 @@ func (d DBClient) Init(ctx context.Context) error {
|
|||
}
|
||||
|
||||
func (d DBClient) CreateTransaction(ctx context.Context, transaction Transaction) error {
|
||||
//TODO check context is not closed
|
||||
//TODO check *DB is not nil
|
||||
//TODO CreateTransaction
|
||||
return fmt.Errorf("db: CreateTransaction not implemented")
|
||||
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) error {
|
||||
//TODO check context is not closed
|
||||
//TODO check *DB is not 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 fmt.Errorf("db: ReadTransaction not implemented")
|
||||
return Transaction{}, fmt.Errorf("db: ReadTransaction not implemented")
|
||||
}
|
||||
}
|
||||
|
||||
func (d DBClient) CountTransactions(ctx context.Context) (membresSansAgenda, membresAvecPerpetuel, membresAvecNonPerpetuel, membresAvecTout int, err error) {
|
||||
//TODO check context is not closed
|
||||
//TODO check *DB is not nil
|
||||
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
|
||||
return 0, 0, 0, 0, fmt.Errorf("db: CountTransactions not implemented")
|
||||
err = fmt.Errorf("db: CountTransactions not implemented")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
29
handler.go
29
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.")
|
||||
}
|
||||
|
||||
//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
|
||||
if err := dbClient.CreateTransaction(ctx, Transaction{
|
||||
MembreID: membreID,
|
||||
|
@ -109,16 +124,18 @@ func UICreateTransaction(ctx context.Context, cfg Config, bottinClient *bottin.A
|
|||
}
|
||||
|
||||
// Prepare result message
|
||||
var typeAgenda string
|
||||
d.Result = fmt.Sprintf("👍 Membre %s peut recevoir son agenda %s",
|
||||
membreID,
|
||||
func() string {
|
||||
if isPerpetual {
|
||||
typeAgenda = "perpétuel"
|
||||
return "perpétuel"
|
||||
} else {
|
||||
typeAgenda = "non-perpétuel"
|
||||
return "non-perpétuel"
|
||||
}
|
||||
}(),
|
||||
)
|
||||
|
||||
d.Result = fmt.Sprintf("👍 Membre %s peut recevoir son agenda %s", membreID, typeAgenda)
|
||||
|
||||
return fmt.Errorf("UIIndexPOST not fully implemented")
|
||||
return nil
|
||||
}(); err != nil {
|
||||
d.Error = err.Error()
|
||||
log.Println("err:", d.Error)
|
||||
|
|
Loading…
Reference in a new issue