-
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.
Merge pull request #1 from aradwann/add-acc
Add accounts & transfers
- Loading branch information
Showing
34 changed files
with
1,275 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
DROP FUNCTION IF EXISTS add_account_balance; | ||
CREATE OR REPLACE FUNCTION add_account_balance( | ||
p_amount bigint, | ||
p_id bigint | ||
) | ||
RETURNS TABLE(id bigint, owner varchar, balance bigint, unit varchar, created_at timestamp with time zone) | ||
AS $$ | ||
BEGIN | ||
RETURN QUERY | ||
UPDATE accounts | ||
SET balance = accounts.balance + p_amount | ||
WHERE accounts.id = p_id | ||
RETURNING *; | ||
END; | ||
$$ LANGUAGE plpgsql; |
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,14 @@ | ||
DROP FUNCTION IF EXISTS create_account; | ||
CREATE OR REPLACE FUNCTION create_account( | ||
p_owner VARCHAR, | ||
p_balance bigint, | ||
p_unit VARCHAR | ||
) | ||
RETURNS TABLE(id BIGINT, owner VARCHAR, balance bigint, unit VARCHAR, created_at timestamptz) AS $$ | ||
BEGIN | ||
RETURN QUERY | ||
INSERT INTO accounts (owner, balance, unit) | ||
VALUES (p_owner, p_balance, p_unit) | ||
RETURNING *; | ||
END; | ||
$$ LANGUAGE plpgsql; |
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,13 @@ | ||
DROP FUNCTION IF EXISTS delete_account; | ||
CREATE OR REPLACE FUNCTION delete_account(p_id INT, OUT result BOOLEAN) | ||
RETURNS BOOLEAN | ||
AS $$ | ||
BEGIN | ||
DELETE FROM accounts WHERE id = p_id; | ||
IF FOUND THEN | ||
result := TRUE; | ||
ELSE | ||
result := FALSE; | ||
END IF; | ||
END; | ||
$$ LANGUAGE plpgsql; |
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,13 @@ | ||
DROP FUNCTION IF EXISTS get_account; | ||
CREATE OR REPLACE FUNCTION get_account(p_id BIGINT) | ||
RETURNS TABLE(id BIGINT, owner VARCHAR, balance bigint, unit VARCHAR, created_at TIMESTAMP WITH TIME ZONE) | ||
LANGUAGE plpgsql | ||
AS $$ | ||
BEGIN | ||
RETURN QUERY | ||
SELECT a.id, a.owner, a.balance, a.unit, a.created_at | ||
FROM accounts a | ||
WHERE a.id = p_id | ||
LIMIT 1; | ||
END; | ||
$$; |
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,12 @@ | ||
DROP FUNCTION IF EXISTS list_accounts; | ||
CREATE OR REPLACE FUNCTION list_accounts(owner_value VARCHAR, limit_value INTEGER, offset_value INTEGER) | ||
RETURNS TABLE(id BIGINT, owner VARCHAR, balance bigint, unit VARCHAR, created_at timestamptz) AS $$ | ||
BEGIN | ||
RETURN QUERY SELECT a.id, a.owner, a.balance, a.unit, a.created_at | ||
FROM accounts a | ||
WHERE a.owner = owner_value | ||
ORDER BY a.id | ||
LIMIT limit_value | ||
OFFSET offset_value; | ||
END; | ||
$$ LANGUAGE plpgsql; |
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,10 @@ | ||
DROP FUNCTION IF EXISTS update_account; | ||
CREATE OR REPLACE FUNCTION update_account(account_id BIGINT, new_balance bigint) | ||
RETURNS TABLE(id BIGINT, owner VARCHAR, balance bigint, unit VARCHAR, created_at TIMESTAMP WITH TIME ZONE) AS $$ | ||
BEGIN | ||
RETURN QUERY UPDATE accounts | ||
SET balance = new_balance | ||
WHERE accounts.id = account_id | ||
RETURNING *; | ||
END; | ||
$$ LANGUAGE plpgsql; |
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,13 @@ | ||
DROP FUNCTION IF EXISTS create_entry; | ||
CREATE OR REPLACE FUNCTION create_entry( | ||
p_account_id bigint, | ||
p_amount bigint | ||
) | ||
RETURNS TABLE(id BIGINT, account_id bigint, amount bigint, created_at timestamptz) AS $$ | ||
BEGIN | ||
RETURN QUERY | ||
INSERT INTO entries (account_id, amount) | ||
VALUES (p_account_id, p_amount) | ||
RETURNING *; | ||
END; | ||
$$ LANGUAGE plpgsql; |
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,13 @@ | ||
DROP FUNCTION IF EXISTS get_entry; | ||
CREATE OR REPLACE FUNCTION get_entry(p_id BIGINT) | ||
RETURNS TABLE(id BIGINT, account_id bigint, amount bigint, created_at TIMESTAMP WITH TIME ZONE) | ||
LANGUAGE plpgsql | ||
AS $$ | ||
BEGIN | ||
RETURN QUERY | ||
SELECT e.id, e.account_id, e.amount, e.created_at | ||
FROM entries e | ||
WHERE e.id = p_id | ||
LIMIT 1; | ||
END; | ||
$$; |
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,12 @@ | ||
DROP FUNCTION IF EXISTS list_entries; | ||
CREATE OR REPLACE FUNCTION list_entries(p_account_id BIGINT, limit_value INTEGER, offset_value INTEGER) | ||
RETURNS TABLE(id BIGINT, account_id BIGINT, amount bigint, created_at timestamptz) AS $$ | ||
BEGIN | ||
RETURN QUERY SELECT e.id, e.account_id, e.amount, e.created_at | ||
FROM entries e | ||
WHERE e.account_id = p_account_id | ||
ORDER BY e.id | ||
LIMIT limit_value | ||
OFFSET offset_value; | ||
END; | ||
$$ LANGUAGE plpgsql; |
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,14 @@ | ||
DROP FUNCTION IF EXISTS create_transfer; | ||
CREATE OR REPLACE FUNCTION create_transfer( | ||
p_from_account_id bigint, | ||
p_to_account_id bigint, | ||
p_amount bigint | ||
) | ||
RETURNS TABLE(id BIGINT, from_account_id bigint, to_account_id bigint, amount bigint, created_at timestamptz) AS $$ | ||
BEGIN | ||
RETURN QUERY | ||
INSERT INTO transfers (from_account_id, to_account_id, amount) | ||
VALUES (p_from_account_id, p_to_account_id, p_amount) | ||
RETURNING *; | ||
END; | ||
$$ LANGUAGE plpgsql; |
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,13 @@ | ||
DROP FUNCTION IF EXISTS get_transfer; | ||
CREATE OR REPLACE FUNCTION get_transfer(p_id BIGINT) | ||
RETURNS TABLE(id BIGINT, from_account_id bigint, to_account_id bigint,amount bigint, created_at TIMESTAMP WITH TIME ZONE) | ||
LANGUAGE plpgsql | ||
AS $$ | ||
BEGIN | ||
RETURN QUERY | ||
SELECT t.id, t.from_account_id, t.to_account_id, t.amount, t.created_at | ||
FROM transfers t | ||
WHERE t.id = p_id | ||
LIMIT 1; | ||
END; | ||
$$; |
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,12 @@ | ||
DROP FUNCTION IF EXISTS list_transfers; | ||
CREATE OR REPLACE FUNCTION list_transfers(p_from_account_id BIGINT, p_to_account_id BIGINT, limit_value INTEGER, offset_value INTEGER) | ||
RETURNS TABLE(id BIGINT, from_account_id BIGINT, to_account_id BIGINT, amount bigint, created_at timestamptz) AS $$ | ||
BEGIN | ||
RETURN QUERY SELECT t.id, t.from_account_id, t.to_account_id, t.amount, t.created_at | ||
FROM transfers t | ||
WHERE t.from_account_id = p_from_account_id OR t.to_account_id = p_to_account_id | ||
ORDER BY t.id | ||
LIMIT limit_value | ||
OFFSET offset_value; | ||
END; | ||
$$ LANGUAGE plpgsql; |
Oops, something went wrong.