forked from zanfranceschi/rinha-de-backend-2024-q1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.sql
87 lines (76 loc) · 2.58 KB
/
init.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
create table if not exists clients
(
id integer primary key,
"limit" bigint not null default 0,
balance bigint not null default 0,
constraint clients_balance_check check (balance >= -"limit")
);
create table if not exists transactions
(
id bigserial primary key,
client_id integer not null,
amount bigint not null,
type char not null,
description text,
created_at timestamp not null default current_timestamp
);
create index if not exists transactions_client_id_index on transactions (client_id);
create or replace function create_transaction(client_id integer, amount bigint, type char, description text)
returns jsonb
language plpgsql
as
$$
DECLARE
updated_balance bigint;
saldo jsonb;
BEGIN
INSERT INTO transactions (client_id, amount, type, description)
VALUES (client_id, amount, type, description);
UPDATE clients
SET balance = balance + CASE type WHEN 'c' THEN amount ELSE -amount END
WHERE id = client_id
RETURNING balance INTO updated_balance;
SELECT JSONB_BUILD_OBJECT(
'limite', (SELECT (SELECT "limit" FROM clients WHERE id = client_id)),
'saldo', updated_balance
)
INTO saldo;
RETURN saldo;
end;
$$;
CREATE OR REPLACE FUNCTION get_extract(client_id integer, OUT response jsonb)
RETURNS jsonb
LANGUAGE SQL
AS
$fn$
WITH client AS (SELECT *
FROM clients
WHERE id = client_id),
transactions AS (SELECT JSONB_BUILD_OBJECT(
'valor', t.amount,
'tipo', t.type,
'descricao', t.description,
'realizada_em', t.created_at
) AS transaction_data
FROM transactions t
WHERE t.client_id = client_id)
SELECT JSONB_BUILD_OBJECT(
'saldo',
JSONB_BUILD_OBJECT(
'total', client.balance,
'data_extrato', to_char(now(), 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"'),
'limite', client.limit
),
'ultimas_transacoes',
case when count(transactions) > 0 then JSONB_AGG(transactions) else '[]' end
)
FROM client
LEFT JOIN transactions ON TRUE
GROUP BY client.balance, client.limit;
$fn$;
insert into clients (id, "limit", balance)
values (1, 100000, 0),
(2, 80000, 0),
(3, 1000000, 0),
(4, 10000000, 0),
(5, 500000, 0);