| 1 | create or replace procedure Board(TicketID_P int)
|
|---|
| 2 | as $$
|
|---|
| 3 | declare
|
|---|
| 4 | TicketStatusID_V int;
|
|---|
| 5 | TicketFlightID int;
|
|---|
| 6 | FlightDeparture timestamp;
|
|---|
| 7 | CheckedInStatus int;
|
|---|
| 8 | BoardedStatus int;
|
|---|
| 9 | begin
|
|---|
| 10 | select id into strict CheckedInStatus
|
|---|
| 11 | from ticketstatus where name = 'Checked In';
|
|---|
| 12 |
|
|---|
| 13 | select id into strict BoardedStatus
|
|---|
| 14 | from ticketstatus where name = 'Boarded';
|
|---|
| 15 |
|
|---|
| 16 | select t.ticketstatusid, t.flightid, f.departure
|
|---|
| 17 | into TicketStatusID_V, TicketFlightID, FlightDeparture
|
|---|
| 18 | from ticket t
|
|---|
| 19 | join flight f on f.id = t.flightid
|
|---|
| 20 | where t.id = TicketID_P
|
|---|
| 21 | for update of t;
|
|---|
| 22 |
|
|---|
| 23 | if not found then
|
|---|
| 24 | raise exception 'Ticket not found!';
|
|---|
| 25 | end if;
|
|---|
| 26 |
|
|---|
| 27 | if TicketStatusID_V = BoardedStatus then
|
|---|
| 28 | raise exception 'Ticket already boarded!';
|
|---|
| 29 | end if;
|
|---|
| 30 |
|
|---|
| 31 | if TicketStatusID_V <> CheckedInStatus then
|
|---|
| 32 | raise exception 'Ticket not checked in!';
|
|---|
| 33 | end if;
|
|---|
| 34 |
|
|---|
| 35 | if exists (select 1 from canceledflight where flightid = TicketFlightID) then
|
|---|
| 36 | raise exception 'Flight has been canceled!';
|
|---|
| 37 | end if;
|
|---|
| 38 |
|
|---|
| 39 | if FlightDeparture > now() + interval '1 hour' then
|
|---|
| 40 | raise exception 'Boarding not allowed yet!';
|
|---|
| 41 | end if;
|
|---|
| 42 |
|
|---|
| 43 | if FlightDeparture < now() + interval '15 minutes' then
|
|---|
| 44 | raise exception 'Boarding closed!';
|
|---|
| 45 | end if;
|
|---|
| 46 |
|
|---|
| 47 | update ticket
|
|---|
| 48 | set ticketstatusid = BoardedStatus
|
|---|
| 49 | where id = TicketID_P;
|
|---|
| 50 | end;
|
|---|
| 51 | $$ language plpgsql; |
|---|