| 98 | | }}} |
| 99 | | == Validate the dates of a booking |
| 100 | | {{{ |
| 101 | | create or replace function trg_validate_booking_dates() |
| 102 | | returns trigger as $$ |
| 103 | | begin |
| 104 | | if new.check_in_date >= new.check_out_date then |
| 105 | | raise exception 'Check out date must be after check in date'; |
| 106 | | end if; |
| 107 | | if new.check_in_date < now() then |
| 108 | | raise exception 'Check in date cannot be in the past'; |
| 109 | | end if; |
| 110 | | return new; |
| 111 | | end; |
| 112 | | $$ language plpgsql; |
| 113 | | |
| 114 | | create or replace trigger validate_booking_dates |
| 115 | | before insert or update on bookings for each row |
| 116 | | execute function trg_validate_booking_dates(); |