| 66 | === Trigger 1 |
| 67 | {{{ |
| 68 | create or replace function after_transaction_trigger() |
| 69 | returns trigger as $$ |
| 70 | begin |
| 71 | |
| 72 | if new.type = 'Deposit' then |
| 73 | insert into Deposit(account_id, transaction_id, amount) |
| 74 | values (new.account_id, new.transaction_id, new.amount); |
| 75 | |
| 76 | elsif new.type = 'Withdrawal' then |
| 77 | insert into Deposit(account_id, transaction_id, amount) |
| 78 | values (new.account_id, new.transaction_id, new.amount); |
| 79 | |
| 80 | elsif new.type like '(MKD) Transfer Out to%' then |
| 81 | insert into transferred_money(s_account_id, r_account_id, t_id, currency) |
| 82 | select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1), |
| 83 | new.id, new.currency; |
| 84 | |
| 85 | elsif new.type like '(USD) Transfer Out to%' then |
| 86 | insert into transferred_money(s_account_id, r_account_id, t_id, currency) |
| 87 | select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1), |
| 88 | new.id, new.currency; |
| 89 | |
| 90 | elsif new.type like '(EUR) Transfer Out to%' then |
| 91 | insert into transferred_money(s_account_id, r_account_id, t_id, currency) |
| 92 | select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1), |
| 93 | new.id, new.currency; |
| 94 | |
| 95 | elsif new.type like '(GBP) Transfer Out to%' then |
| 96 | insert into transferred_money(s_account_id, r_account_id, t_id, currency) |
| 97 | select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1), |
| 98 | new.id, new.currency; |
| 99 | |
| 100 | end if; |
| 101 | return new; |
| 102 | end; |
| 103 | $$ language plpgsql; |
| 104 | |
| 105 | create trigger after_transaction |
| 106 | after insert on transaction |
| 107 | for each row |
| 108 | execute function after_transaction_trigger(); |
| 109 | }}} |
| 110 | |