-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prevent creating user without sending verification email
- Loading branch information
Showing
5 changed files
with
91 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
// ExecTx executes a function within a database transaction | ||
func (store *SQLStore) execTx(ctx context.Context, fn func(*Queries) error) error { | ||
tx, err := store.db.Begin() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
q := New(tx) | ||
err = fn(q) | ||
if err != nil { | ||
if rbErr := tx.Rollback(); rbErr != nil { | ||
return fmt.Errorf("tx err: %v, rb err: %v", err, rbErr) | ||
} | ||
return err | ||
} | ||
|
||
return tx.Commit() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package db | ||
|
||
import "context" | ||
|
||
type CreateUserTxParams struct { | ||
CreateUserParams | ||
AfterCreate func(user User) error | ||
} | ||
|
||
type CreateUserTxResult struct { | ||
User User | ||
} | ||
|
||
func (store *SQLStore) CreateUserTx(ctx context.Context, arg CreateUserTxParams) (CreateUserTxResult, error) { | ||
var result CreateUserTxResult | ||
|
||
err := store.execTx(ctx, func(q *Queries) error { | ||
var err error | ||
|
||
result.User, err = q.CreateUser(ctx, arg.CreateUserParams) | ||
if err != nil { | ||
return err | ||
} | ||
return arg.AfterCreate(result.User) | ||
|
||
}) | ||
|
||
return result, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters