-
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.
feat: async task: send verification email on user sign up
- Loading branch information
1 parent
aac2956
commit d147720
Showing
15 changed files
with
377 additions
and
109 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package db | ||
|
||
import "context" | ||
|
||
// TransferTxParams contains all input parameters of the transfer transaction | ||
type TransferTxParams struct { | ||
FromAccountID int64 `json:from_account_id` | ||
ToAccountID int64 `json:to_account_id` | ||
Amount int64 `json:amount` | ||
} | ||
|
||
// TransferTxResult is the result of the transfer transaction | ||
type TransferTxResult struct { | ||
Transfer Transfer `json:transfer` | ||
FromAccount Account `json:from_account` | ||
ToAccount Account `json:to_account` | ||
FromEntry Entry `json:from_entry` | ||
ToEntry Entry `json:to_entry` | ||
} | ||
|
||
// TransferTx performs a money transfer from one account to the other | ||
// It creates a transfer record, add account entries, and update accounts' balance within a single DB transaction | ||
func (store *SQLStore) TransferTx(ctx context.Context, arg TransferTxParams) (TransferTxResult, error) { | ||
var result TransferTxResult | ||
|
||
err := store.execTx(ctx, func(q *Queries) error { | ||
var err error | ||
|
||
result.Transfer, err = q.CreateTransfer(ctx, CreateTransferParams{ | ||
FromAccountID: arg.FromAccountID, | ||
ToAccountID: arg.ToAccountID, | ||
Amount: arg.Amount, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result.FromEntry, err = q.CreateEntry(ctx, CreateEntryParams{ | ||
AccountID: arg.FromAccountID, | ||
Amount: -arg.Amount, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result.ToEntry, err = q.CreateEntry(ctx, CreateEntryParams{ | ||
AccountID: arg.ToAccountID, | ||
Amount: arg.Amount, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if arg.FromAccountID < arg.ToAccountID { | ||
result.FromAccount, result.ToAccount, err = addMoney(ctx, q, arg.FromAccountID, -arg.Amount, arg.ToAccountID, arg.Amount) | ||
} else { | ||
result.ToAccount, result.FromAccount, err = addMoney(ctx, q, arg.ToAccountID, arg.Amount, arg.FromAccountID, -arg.Amount) | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
return result, err | ||
} | ||
|
||
func addMoney( | ||
ctx context.Context, | ||
q *Queries, | ||
accountID1 int64, | ||
amount1 int64, | ||
accountID2 int64, | ||
amount2 int64, | ||
) (account1 Account, account2 Account, err error) { | ||
account1, err = q.AddAccountBalance(ctx, AddAccountBalanceParams{ | ||
ID: accountID1, | ||
Amount: amount1, | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
|
||
account2, err = q.AddAccountBalance(ctx, AddAccountBalanceParams{ | ||
ID: accountID2, | ||
Amount: amount2, | ||
}) | ||
return | ||
} |
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
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
Oops, something went wrong.