Skip to content

Commit

Permalink
fixed account query to change the updateAccount func
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiraj-ku committed Sep 19, 2024
1 parent 69c195f commit 56ce9fb
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 27 deletions.
37 changes: 28 additions & 9 deletions db/query/account.sql
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
-- name: CreateAccount :one
insert into accounts(
INSERT INTO accounts (
owner,
balance,
currency
)values(
$1,$2,$3
) VALUES (
$1, $2, $3
) RETURNING *;


-- name: GetAccount :one
select * from accounts where id=$1 limit 1;
SELECT * FROM accounts
WHERE id = $1 LIMIT 1;

-- name: GetAccountForUpdate :one
SELECT * FROM accounts
WHERE id = $1 LIMIT 1
FOR NO KEY UPDATE;

-- name: ListAccounts :many
select * from accounts order by id limit $1 offset 2;
SELECT * FROM accounts
WHERE owner = $1
ORDER BY id
LIMIT $2
OFFSET $3;

-- name: UpdateAccount :one
UPDATE accounts
SET balance = $2
WHERE id = $1
RETURNING *;

-- name: UpdateAccount :exec
update accounts set balance= $2 where id =$1;
-- name: AddAccountBalance :one
UPDATE accounts
SET balance = balance + sqlc.arg(amount)
WHERE id = sqlc.arg(id)
RETURNING *;

-- name: DeleteAccount :exec
delete from accounts where id = $1;
DELETE FROM accounts
WHERE id = $1;
95 changes: 81 additions & 14 deletions db/sqlc/account.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion db/sqlc/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion db/sqlc/entry.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion db/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions db/sqlc/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (store *Store) TransferTX(ctx context.Context, arg TransferTXParams) (Trans

err := store.execTX(ctx, func(q *Queries) error {
var err error

// create transfer record
result.Transfer, err = q.CreateTransfer(ctx, CreateTransferParams{
FromAccountID: arg.FromAccountID,
ToAccountID: arg.ToAccountID,
Expand All @@ -68,22 +70,55 @@ func (store *Store) TransferTX(ctx context.Context, arg TransferTXParams) (Trans
if err != nil {
return err
}

// substract money from 'Fromaccount/senders account'
result.FromEntry, err = q.CreateEntry(ctx, CreateEntryParams{
AccountID: arg.FromAccountID,
Amount: -arg.Amount,
})
if err != nil {
return err
}

// Add money to 'ToAccount/reciever's account'
result.ToEntry, err = q.CreateEntry(ctx, CreateEntryParams{
AccountID: arg.ToAccountID,
Amount: arg.Amount,
})
if err != nil {
return err
}

// TODO: create update account and implement locking mechanism to handle deadlock

// transfer/move money from account 1
account1, err := q.GetAccount(ctx, arg.FromAccountID)
if err != nil {
return nil
}

result.FromAccount, err = q.UpdateAccount(ctx, UpdateAccountParams{
ID: arg.FromAccountID,
Balance: account1.Balance - arg.Amount,
})
if err != nil {
return err
}

// add money to account 2
account2, err := q.GetAccount(ctx, arg.FromAccountID)
if err != nil {
return nil
}

result.ToAccount, err = q.UpdateAccount(ctx, UpdateAccountParams{
ID: arg.ToAccountID,
Balance: account2.Balance + arg.Amount,
})
if err != nil {
return err
}

return nil
})
return result, err
Expand Down
2 changes: 1 addition & 1 deletion db/sqlc/transfer.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 56ce9fb

Please sign in to comment.