| 1 | create procedure AddBaggage(
|
|---|
| 2 | TicketId int,
|
|---|
| 3 | BaggageTypeId int,
|
|---|
| 4 | Weight int,
|
|---|
| 5 | Height int,
|
|---|
| 6 | Length int,
|
|---|
| 7 | Width int
|
|---|
| 8 | )
|
|---|
| 9 | as $$
|
|---|
| 10 | declare
|
|---|
| 11 | FlightID_P int;
|
|---|
| 12 | MaxWeight_P int;
|
|---|
| 13 | MaxHeight_P int;
|
|---|
| 14 | MaxLength_P int;
|
|---|
| 15 | MaxWidth_P int;
|
|---|
| 16 | begin
|
|---|
| 17 | select maxweight, maxheight, maxlength, maxwidth
|
|---|
| 18 | into MaxWeight_P, MaxHeight_P, MaxLength_P, MaxWidth_P
|
|---|
| 19 | from baggagetype
|
|---|
| 20 | where id = BaggageTypeId;
|
|---|
| 21 |
|
|---|
| 22 | select flight.id
|
|---|
| 23 | into FlightID_P
|
|---|
| 24 | from flight
|
|---|
| 25 | join ticket on flight.id = ticket.flightid
|
|---|
| 26 | where ticket.id = TicketId;
|
|---|
| 27 |
|
|---|
| 28 | if exists(select * from canceledflight where flightid = FlightID_P) then
|
|---|
| 29 | raise exception 'Flight has been canceled!';
|
|---|
| 30 | end if;
|
|---|
| 31 |
|
|---|
| 32 | if not FOUND then
|
|---|
| 33 | raise exception 'Invalid Baggage Type!';
|
|---|
| 34 | end if;
|
|---|
| 35 |
|
|---|
| 36 | if Weight > MaxWeight_P then
|
|---|
| 37 | raise exception 'Invalid weight!';
|
|---|
| 38 | end if;
|
|---|
| 39 |
|
|---|
| 40 | if Height > MaxHeight_P then
|
|---|
| 41 | raise exception 'Invalid height!';
|
|---|
| 42 | end if;
|
|---|
| 43 |
|
|---|
| 44 | if Length > MaxLength_P then
|
|---|
| 45 | raise exception 'Invalid length!';
|
|---|
| 46 | end if;
|
|---|
| 47 |
|
|---|
| 48 | if Width > MaxWidth_P then
|
|---|
| 49 | raise exception 'Invalid width!';
|
|---|
| 50 | end if;
|
|---|
| 51 |
|
|---|
| 52 | insert into baggage (baggagetypeid, weight, height, length, width, ticketid)
|
|---|
| 53 | values (AddBaggage.BaggageTypeId, AddBaggage.Weight,
|
|---|
| 54 | AddBaggage.Height, AddBaggage.Length, AddBaggage.Width, AddBaggage.TicketId);
|
|---|
| 55 | end;
|
|---|
| 56 | $$ language plpgsql;
|
|---|
| 57 |
|
|---|