Skip to content

Commit

Permalink
Merge pull request #1 from aradwann/add-acc
Browse files Browse the repository at this point in the history
Add accounts & transfers
  • Loading branch information
aradwann authored Mar 7, 2024
2 parents 4e720dc + aa648b6 commit 7f16db7
Show file tree
Hide file tree
Showing 34 changed files with 1,275 additions and 82 deletions.
15 changes: 15 additions & 0 deletions db/migrations/procs/account/add_account_balance.sql
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;
14 changes: 14 additions & 0 deletions db/migrations/procs/account/create_account.sql
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;
13 changes: 13 additions & 0 deletions db/migrations/procs/account/delete_account.sql
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;
13 changes: 13 additions & 0 deletions db/migrations/procs/account/get_account.sql
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;
$$;
12 changes: 12 additions & 0 deletions db/migrations/procs/account/list_accounts.sql
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;
10 changes: 10 additions & 0 deletions db/migrations/procs/account/update_account.sql
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;
13 changes: 13 additions & 0 deletions db/migrations/procs/entry/create_entry.sql
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;
13 changes: 13 additions & 0 deletions db/migrations/procs/entry/get_entry.sql
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;
$$;
12 changes: 12 additions & 0 deletions db/migrations/procs/entry/list_entry.sql
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;
14 changes: 14 additions & 0 deletions db/migrations/procs/transfer/create_transfer.sql
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;
13 changes: 13 additions & 0 deletions db/migrations/procs/transfer/get_transfer.sql
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;
$$;
12 changes: 12 additions & 0 deletions db/migrations/procs/transfer/list_transfer.sql
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;
Loading

0 comments on commit 7f16db7

Please sign in to comment.