Changes between Version 4 and Version 5 of ApplicationDevelopment


Ignore:
Timestamp:
09/29/25 14:42:31 (2 weeks ago)
Author:
221507
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ApplicationDevelopment

    v4 v5  
    6262
    6363
    64 
    65 
     64== Triggers
     65
     66=== 1. Trigger to update the number of free seats based on ticket insertion/deletion
     67
     68This trigger automatically adjusts the number of available seats in a trip whenever a ticket is inserted or deleted. On insert, it decreases the free_seats count by one, and on delete, it increases it by one. This ensures that trip capacity stays accurate without requiring manual updates.
     69
     70{{{
     71CREATE OR REPLACE FUNCTION update_free_seats()
     72RETURNS TRIGGER AS $$
     73BEGIN
     74    IF TG_OP = 'INSERT' THEN
     75UPDATE trip
     76SET free_seats = free_seats - 1
     77WHERE trip_id = NEW.trip_id;
     78RETURN NEW;
     79ELSIF TG_OP = 'DELETE' THEN
     80UPDATE trip
     81SET free_seats = free_seats + 1
     82WHERE trip_id = OLD.trip_id;
     83RETURN OLD;
     84END IF;
     85RETURN NULL;
     86END;
     87$$ LANGUAGE plpgsql;
     88
     89CREATE TRIGGER ticket_insert_update_seats
     90    AFTER INSERT ON ticket
     91    FOR EACH ROW
     92    EXECUTE FUNCTION update_free_seats();
     93
     94CREATE TRIGGER ticket_delete_update_seats
     95    AFTER DELETE ON ticket
     96    FOR EACH ROW
     97    EXECUTE FUNCTION update_free_seats();
     98}}}
     99
     100
     101=== 2. Ticket discount and total price triggers
     102
     103These two triggers work together in a way that the discount triggers first calculates and applies the reduced ticket price before the ticket is officially inserted into the database. Once the discounted ticket is saved, the payment triggers recalculate the total payment by summing all ticket prices linked to the same payment record, ensuring the final total reflects the applied discount.
     104
     105{{{
     106CREATE OR REPLACE FUNCTION apply_ticket_discount()
     107RETURNS TRIGGER AS $$
     108BEGIN
     109UPDATE ticket
     110SET price = tr.base_price - (tr.base_price * NEW.discount / 100)
     111    FROM trip tr
     112WHERE ticket.ticket_id = NEW.ticket_id AND ticket.trip_id = tr.trip_id;
     113RETURN NEW;
     114END;
     115$$ LANGUAGE plpgsql;
     116CREATE TRIGGER apply_student_ticket_discount
     117    BEFORE INSERT ON student_ticket
     118    FOR EACH ROW EXECUTE FUNCTION apply_ticket_discount();
     119
     120CREATE TRIGGER apply_child_ticket_discount
     121    BEFORE INSERT ON child_ticket
     122    FOR EACH ROW EXECUTE FUNCTION apply_ticket_discount();
     123
     124
     125-- UPDATE TOTAL PAYMENT TOTAL PRICE (TRIGGER AFTER CHILD/STUDENT DISCOUNT IS APPLIED)
     126CREATE OR REPLACE FUNCTION update_payment_total()
     127RETURNS TRIGGER AS $$
     128BEGIN UPDATE payment
     129      SET total_price = ( SELECT COALESCE(SUM(price), 0)
     130                          FROM ticket
     131                          WHERE payment_id = NEW.payment_id)
     132      WHERE payment_id = NEW.payment_id;
     133
     134RETURN NEW; END; $$
     135LANGUAGE plpgsql;
     136
     137CREATE TRIGGER trg_update_payment_total_insert
     138    AFTER INSERT ON ticket
     139    FOR EACH ROW EXECUTE FUNCTION update_payment_total();
     140
     141CREATE TRIGGER trg_update_payment_total_update
     142    AFTER UPDATE OF price, payment_id ON ticket
     143    FOR EACH ROW EXECUTE FUNCTION update_payment_total();
     144
     145CREATE TRIGGER trg_update_payment_total_delete
     146    AFTER DELETE ON ticket
     147    FOR EACH ROW EXECUTE FUNCTION update_payment_total();
     148}}}
     149
     150
     151=== 3. Trigger to automatically set trip status
     152
     153This trigger assigns the default status 'NOT_STARTED' to any new trip that doesn't explicitly define a status. It guarantees consistent initialization of trip state at creation time. This prevents null or undefined statuses in downstream queries and logic.
     154
     155{{{
     156CREATE OR REPLACE FUNCTION set_default_trip_status()
     157RETURNS TRIGGER AS $$
     158BEGIN
     159    IF NEW.status IS NULL THEN
     160        NEW.status = 'NOT_STARTED';
     161END IF;
     162RETURN NEW;
     163END;
     164$$ LANGUAGE plpgsql;
     165
     166CREATE TRIGGER trg_set_default_trip_status
     167    BEFORE INSERT ON trip
     168    FOR EACH ROW EXECUTE FUNCTION set_default_trip_status();
     169}}}
     170
     171
     172=== 4. Trigger to update trip status
     173
     174This function adjusts a trip's status based on available seats and trip date. It sets the trip to 'FULL' when no seats remain or resets it to 'NOT_STARTED' if seats reopen. If the trip date is in the past, it marks the trip as 'COMPLETED' to reflect its final state.
     175
     176
     177{{{
     178CREATE OR REPLACE FUNCTION update_trip_status()
     179RETURNS TRIGGER AS $$
     180BEGIN
     181    IF NEW.free_seats = 0 THEN
     182        NEW.status = 'FULL';
     183    ELSIF NEW.free_seats > 0 AND OLD.status = 'FULL' THEN
     184        NEW.status = 'NOT_STARTED';
     185END IF;
     186
     187    IF NEW.date < CURRENT_DATE THEN
     188        NEW.status = 'COMPLETED';
     189END IF;
     190
     191RETURN NEW;
     192END;
     193$$ LANGUAGE plpgsql;
     194
     195CREATE TRIGGER trg_update_trip_status
     196    BEFORE UPDATE OF free_seats, date ON trip
     197    FOR EACH ROW
     198    EXECUTE FUNCTION update_trip_status();
     199}}}
     200
     201=== 5. Trigger to automatically set the ticket purchase time and date
     202
     203This trigger ensures that every ticket has a purchase date and time set automatically. If either field is missing at insertion, it fills them with the current date and time. It prevents incomplete timestamps and simplifies data entry.
     204
     205
     206{{{
     207CREATE OR REPLACE FUNCTION set_purchase_timestamp()
     208RETURNS TRIGGER AS $$
     209BEGIN
     210    IF NEW.date_purchased IS NULL THEN
     211        NEW.date_purchased = CURRENT_DATE;
     212END IF;
     213    IF NEW.time_purchased IS NULL THEN
     214        NEW.time_purchased = CURRENT_TIME;
     215END IF;
     216RETURN NEW;
     217END;
     218$$ LANGUAGE plpgsql;
     219
     220CREATE TRIGGER trg_set_purchase_timestamp
     221    BEFORE INSERT ON ticket
     222    FOR EACH ROW EXECUTE FUNCTION set_purchase_timestamp();
     223}}}
     224
     225
     226
     227