| | 5 | |
| | 6 | [[Image(старт-возење.png)]] |
| | 7 | |
| | 8 | ==== Тригер во база: |
| | 9 | |
| | 10 | Пред да се внесе возењето во база се извршува тригер во база за да се провери валидноста на билетот: |
| | 11 | |
| | 12 | {{{#!div style="font-size: 80%" |
| | 13 | {{{#!sql |
| | 14 | create or replace function check_expiry_date() returns trigger |
| | 15 | as |
| | 16 | $$ |
| | 17 | begin |
| | 18 | if exists( |
| | 19 | select 1 |
| | 20 | from vozenje v |
| | 21 | where v.patnik_id = new.patnik_id and v.end_date is null |
| | 22 | ) then |
| | 23 | update vozenje set end_date = now(), status = 'FINISHED' where id in (select v.id |
| | 24 | from vozenje v |
| | 25 | where v.patnik_id = new.patnik_id and v.end_date is null); |
| | 26 | end if; |
| | 27 | |
| | 28 | if exists( |
| | 29 | select 1 |
| | 30 | from bilet |
| | 31 | where id = new.bilet_id and datum_aktivacija is null |
| | 32 | ) then |
| | 33 | update bilet set datum_aktivacija = now(), status = 'ACTIVE' where id = new.bilet_id; |
| | 34 | end if; |
| | 35 | |
| | 36 | if exists( |
| | 37 | select 1 |
| | 38 | from bilet b |
| | 39 | join tipbilet tb on b.tip_id = tb.id |
| | 40 | where b.id = new.bilet_id and ((b.datum_aktivacija + (tb.trajnost || ' milliseconds')::interval) < now()) |
| | 41 | ) then |
| | 42 | update bilet set status = 'EXPIRED' where id = new.bilet_id; |
| | 43 | end if; |
| | 44 | |
| | 45 | if exists( |
| | 46 | select 1 |
| | 47 | from bilet b |
| | 48 | join tipbilet tb on b.tip_id = tb.id |
| | 49 | where b.id = new.bilet_id and ((b.datum_aktivacija + (tb.trajnost || ' milliseconds')::interval) < now()) |
| | 50 | ) then |
| | 51 | RAISE exception 'Ticket is expired'; |
| | 52 | end if; |
| | 53 | return new; |
| | 54 | end; |
| | 55 | $$ language plpgsql; |
| | 56 | |
| | 57 | create or replace trigger check_validity_of_ticket |
| | 58 | before insert on vozenje |
| | 59 | for each row |
| | 60 | execute function check_expiry_date(); |
| | 61 | }}} |
| | 62 | }}} |
| | 63 | |
| | 64 | |
| | 65 | ==== Query за да се добијат потрбните инстанци на линии: |
| | 66 | |
| | 67 | {{{#!div style="font-size: 80%" |
| | 68 | {{{#!java |
| | 69 | |
| | 70 | @Query( |
| | 71 | value = |
| | 72 | """ |
| | 73 | select distinct inl.id |
| | 74 | from instanca_na_linija inl |
| | 75 | join instanca_na_linija_postojka_na_linija inlpnl on inl.id = inlpnl.instanca_na_linija_id |
| | 76 | where end_date is null |
| | 77 | and linija_id in ( |
| | 78 | select distinct(pnl.linija_id) |
| | 79 | from postojka_na_linija pnl |
| | 80 | where postojka_id = ?1 |
| | 81 | ) |
| | 82 | except |
| | 83 | select inl.id |
| | 84 | from postojka_na_linija pnl |
| | 85 | join instanca_na_linija inl on inl.linija_id = pnl.linija_id and inl.pravec_id = pnl.pravec_id |
| | 86 | join instanca_na_linija_postojka_na_linija inlpnl on inl.id = inlpnl.instanca_na_linija_id and pnl.id = inlpnl.postojka_na_linija_id |
| | 87 | where inl.end_date is null and postojka_id = ?1; |
| | 88 | """, |
| | 89 | nativeQuery = true, |
| | 90 | ) |
| | 91 | fun findIncomingRouteInstancesForStation(stationId: Int): List<Long> |
| | 92 | |
| | 93 | |
| | 94 | }}} |
| | 95 | }}} |
| | 96 | |