| 95 | | == 4. generate_rsvp_summary() == |
| | 95 | == 4. is_band_available() == |
| | 96 | |
| | 97 | === Description === |
| | 98 | |
| | 99 | Checks whether a band is available for a selected date and time interval. |
| | 100 | |
| | 101 | This function uses full timestamp interval comparison by combining the booking date with start and end time. This avoids incorrect availability results when intervals overlap in different ways. |
| | 102 | |
| | 103 | === SQL Code === |
| | 104 | |
| | 105 | {{{ |
| | 106 | #!sql |
| | 107 | CREATE OR REPLACE FUNCTION is_band_available( |
| | 108 | p_band_id INTEGER, |
| | 109 | p_date DATE, |
| | 110 | p_start TIME, |
| | 111 | p_end TIME |
| | 112 | ) |
| | 113 | RETURNS BOOLEAN AS $$ |
| | 114 | BEGIN |
| | 115 | IF p_end <= p_start THEN |
| | 116 | RAISE EXCEPTION 'Invalid interval: end time must be after start time.'; |
| | 117 | END IF; |
| | 118 | |
| | 119 | RETURN NOT EXISTS ( |
| | 120 | SELECT 1 |
| | 121 | FROM band_booking bb |
| | 122 | WHERE bb.band_id = p_band_id |
| | 123 | AND bb.status <> 'cancelled' |
| | 124 | AND ( |
| | 125 | (p_date + p_start) < (bb."date" + bb.end_time) |
| | 126 | AND (p_date + p_end) > (bb."date" + bb.start_time) |
| | 127 | ) |
| | 128 | ); |
| | 129 | END; |
| | 130 | $$ LANGUAGE plpgsql; |
| | 131 | }}} |
| | 132 | |
| | 133 | == 5. generate_rsvp_summary() == |