From 44ee572f3d84010dcfa67506baa93d91fb9327d7 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Tue, 14 Feb 2023 15:32:13 -0600 Subject: [PATCH] More cleanup for new DBI changes --- accounts/confirm.go | 13 +++++-------- interfaces.go | 6 +++--- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/accounts/confirm.go b/accounts/confirm.go index c101632..9962925 100644 --- a/accounts/confirm.go +++ b/accounts/confirm.go @@ -47,9 +47,9 @@ func NewConfirmation(ctype int, user gobwebs.User, expires time.Time) *Confirmat } // GetConfirmation will fetch from the db using the given key -func GetConfirmation(db *sql.DB, key string) (*Confirmation, error) { +func GetConfirmation(ctx context.Context, key string) (*Confirmation, error) { c := &Confirmation{Key: key} - if err := c.Load(db); err != nil { + if err := c.Load(ctx); err != nil { return nil, err } return c, nil @@ -64,11 +64,10 @@ func (c *Confirmation) IsValid(ctype int) bool { } // Load will load a confirmation using the Key value -func (c *Confirmation) Load(db *sql.DB) error { +func (c *Confirmation) Load(ctx context.Context) error { if c.Key == "" { return fmt.Errorf("Can not load without a Key value") } - ctx := database.Context(context.Background(), db) if err := database.WithTx(ctx, database.TxOptionsRO, func(tx *sql.Tx) error { row := tx.QueryRowContext(ctx, ` SELECT @@ -96,14 +95,13 @@ func (c *Confirmation) Load(db *sql.DB) error { } // Store saves a new confirmation to the database. -func (c *Confirmation) Store(db *sql.DB) error { +func (c *Confirmation) Store(ctx context.Context) error { if c.Type == 0 || c.UserID == 0 { return fmt.Errorf("Can not store confirmation with either Type or UserID missing") } if c.Key == "" { c.Key = ksuid.New().String() } - ctx := database.Context(context.Background(), db) if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error { var tid uint for { @@ -157,7 +155,7 @@ func (c *Confirmation) Store(db *sql.DB) error { } // Confirm sets `confirm_time`, essentially confirming the confimration request -func (c *Confirmation) Confirm(db *sql.DB) error { +func (c *Confirmation) Confirm(ctx context.Context) error { // The requirement for ID is because we want the confirmation to be // loaded and verify it's valid before confirming. No shortcuts allowed // as that will just lead to trouble @@ -165,7 +163,6 @@ func (c *Confirmation) Confirm(db *sql.DB) error { return fmt.Errorf("Can not confirm an empty confirmation") } - ctx := database.Context(context.Background(), db) if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error { row := tx.QueryRowContext(ctx, ` UPDATE "confirmations" diff --git a/interfaces.go b/interfaces.go index 79f444a..e056b88 100644 --- a/interfaces.go +++ b/interfaces.go @@ -1,7 +1,7 @@ package gobwebs import ( - "database/sql" + "context" "time" "github.com/labstack/echo/v4" @@ -39,10 +39,10 @@ type User interface { // Generally used by accounts and middlewares type UserFetch interface { GetUser() User - FromDB(db *sql.DB, uid any, markAuth bool) (User, error) + FromDB(ctx context.Context, uid any, markAuth bool) (User, error) FromContext(c echo.Context) User - WritePassword(db *sql.DB, user User) error + WritePassword(ctx context.Context, user User) error // Helpers to customize the behavior of accounts handlers LoginType() int -- 2.45.2