-
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.
integrate redis worker, update import binary package
- Loading branch information
Showing
15 changed files
with
218 additions
and
124 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,32 @@ | ||
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) | ||
}) | ||
if err != nil { | ||
return CreateUserTxResult{}, err | ||
} | ||
|
||
return result, nil | ||
} |
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,88 @@ | ||
package db | ||
|
||
import "context" | ||
|
||
type TransferTxParams struct { | ||
FromAccountId int64 `json:"from_account_id"` | ||
ToAccountId int64 `json:"to_account_id"` | ||
Amount int64 `json:"amount"` | ||
} | ||
|
||
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"` | ||
} | ||
|
||
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 { | ||
err = addMoney(q, context.Background(), &result, arg.FromAccountId, arg.ToAccountId, -arg.Amount, arg.Amount) | ||
} else { | ||
err = addMoney(q, context.Background(), &result, arg.ToAccountId, arg.FromAccountId, arg.Amount, -arg.Amount) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return TransferTxResult{}, err | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func addMoney(q *Queries, ctx context.Context, result *TransferTxResult, accountId1, accountId2, amount1, amount2 int64) error { | ||
var err error | ||
|
||
result.FromAccount, err = q.AddAccountBalance(ctx, AddAccountBalanceParams{ | ||
ID: accountId1, | ||
Amount: amount1, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result.ToAccount, err = q.AddAccountBalance(ctx, AddAccountBalanceParams{ | ||
ID: accountId2, | ||
Amount: amount2, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Large diffs are not rendered by default.
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
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.