bank docker

This commit is contained in:
2025-11-16 09:34:19 +01:00
parent 4f38a8793a
commit e55c60504e
7 changed files with 7029 additions and 270 deletions

View File

@@ -1,14 +1,7 @@
drop schema if exists bank cascade;
create schema bank;
set search_path TO bank;
/************************************************************************
************************************************************************/
create type holder_type as enum ('BANK', 'PERSON', 'COMPANY');
create table holder (
"id" bigint primary key generated always as identity,
"id" bigint primary key generated always as identity,
"type" holder_type not null,
"created_at" timestamp not null default current_timestamp
);
@@ -41,7 +34,7 @@ create table person (
/************************************************************************
* Company
*
*
************************************************************************/
create table company (
"id" bigint primary key references holder(id) on delete cascade,
@@ -52,13 +45,13 @@ create table company (
/************************************************************************
* Company
*
*
************************************************************************/
create table currency (
"code" text primary key
);
insert into currency
insert into currency
values ('EUR'), ('YEN'), ('USD');
/************************************************************************
@@ -85,9 +78,9 @@ create or replace function latest_exchange_rate (
returns decimal
language sql
as $$
select rate
select rate
from exchange_rate
where currency_code = p_code
where currency_code = p_code
and date < p_date
order by date desc
limit 1;
@@ -184,7 +177,7 @@ call add_person('Justin', 'Hébrard', '1993-03-11');
create or replace procedure add_company (
p_name text,
p_registration_number text,
p_created_at date
p_creation_date date
)
language plpgsql
as $$
@@ -195,8 +188,8 @@ begin
insert into holder(type) values ('COMPANY')
returning id into v_holder_id;
insert into company(id, name, registration_number, created_at)
values (v_holder_id, p_name, p_registration_number, p_created_at);
insert into company(id, name, registration_number, creation_date)
values (v_holder_id, p_name, p_registration_number, p_creation_date);
raise notice 'Titulaire créé : % = % %',
v_holder_id, p_name, p_registration_number;
@@ -279,21 +272,21 @@ call add_account('EUR', array[7], array[1]);
call add_account('USD', array[7], array[1]);
create or replace view holder_detail as
select h.id, h.type,
case
select h.id, h.type,
case
when type = 'PERSON' then firstname || ' ' || lastname
else name
end as nom,
case
case
when type = 'PERSON' then age(birthdate)
when type = 'COMPANY' then age(c.created_at)
end as age
when type = 'COMPANY' then age(c.creation_date)
end as age
from holder h
left join person p on p.id = h.id
left join company c on c.id = h.id;
create or replace view account_detail as
select a.balance,
select a.balance,
a.balance * ah.share as balance_currency,
a.balance * ah.share / latest_exchange_rate(a.currency_code, current_date),
a.currency_code,
@@ -309,16 +302,16 @@ join holder_detail hd on ah.holder_id = hd.id;
insert into transaction (amount) values (100);
insert into operation (transaction_id, account_id, amount, direction)
insert into operation (transaction_id, account_id, amount, direction)
values (1, 4, 100, 'CREDIT');
insert into operation (transaction_id, account_id, amount, direction)
insert into operation (transaction_id, account_id, amount, direction)
values (1, 1, 100, 'DEBIT');
insert into transaction (amount) values (10000);
insert into operation (transaction_id, account_id, amount, direction)
insert into operation (transaction_id, account_id, amount, direction)
values (2, 5, 10000, 'CREDIT');
insert into operation (transaction_id, account_id, amount, direction)
insert into operation (transaction_id, account_id, amount, direction)
values (2, 3, 10000, 'DEBIT');