Changes between Initial Version and Version 1 of Triggers


Ignore:
Timestamp:
01/29/26 01:54:26 (11 days ago)
Author:
233051
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Triggers

    v1 v1  
     1== Triggers ==
     2
     3=== Build Price Calculation Trigger (build_total_price) ===
     4
     5**What it does:** Automatically recalculates the `total_price` of a build whenever components are added, removed, or modified. \\
     6**When it triggers:** AFTER `INSERT`, `UPDATE`, or `DELETE` on the `build_component` table. \\
     7**Why it is useful:** Ensures the build price is always mathematically correct and consistent with the sum of its parts, removing the need for manual calculation in the application layer. \\
     8
     9{{{
     10CREATE OR REPLACE FUNCTION update_build_total_price()
     11RETURNS TRIGGER
     12LANGUAGE plpgsql AS $$
     13DECLARE
     14    target_build_id INT;
     15BEGIN
     16    IF (TG_OP = 'DELETE') THEN
     17        target_build_id := OLD.build_id;
     18    ELSE
     19        target_build_id := NEW.build_id;
     20    END IF;
     21
     22    UPDATE "build"
     23    SET "total_price" = (
     24        SELECT COALESCE(SUM(c.price * bc.num_components), 0)
     25        FROM "build_component" bc
     26        JOIN "components" c ON bc.component_id = c.id
     27        WHERE bc.build_id = target_build_id
     28    )
     29    WHERE id = target_build_id;
     30
     31    RETURN NULL;
     32END;
     33$$;
     34
     35DROP TRIGGER IF EXISTS trigger_auto_update_price ON "build_component";
     36CREATE TRIGGER trigger_auto_update_price
     37AFTER INSERT OR UPDATE OR DELETE ON "build_component"
     38FOR EACH ROW
     39EXECUTE FUNCTION update_build_total_price();
     40}}}
     41
     42=== Self-Review Prevention Trigger (check_review_validity) ===
     43
     44**What it does:** Checks if the user trying to write a review is the creator of the build. If they are, it raises an exception. \\
     45**When it triggers:** BEFORE `INSERT` on the `review` table. \\
     46**Why it is useful:** Enforces data integrity by preventing users from artificially boosting their own build's popularity with fake reviews. \\
     47
     48{{{
     49CREATE OR REPLACE FUNCTION check_review_validity()
     50RETURNS TRIGGER
     51LANGUAGE plpgsql AS $$
     52DECLARE
     53    build_owner_id INT;
     54BEGIN
     55    SELECT user_id INTO build_owner_id FROM "build" WHERE id = NEW.build_id;
     56
     57    IF NEW.user_id = build_owner_id THEN
     58        RAISE EXCEPTION 'Cannot review own builds';
     59    END IF;
     60
     61    RETURN NEW;
     62END;
     63$$;
     64
     65DROP TRIGGER IF EXISTS trigger_check_self_review ON "review";
     66CREATE TRIGGER trigger_check_self_review
     67BEFORE INSERT ON "review"
     68FOR EACH ROW
     69EXECUTE FUNCTION check_review_validity();
     70}}}
     71
     72=== Self-Rating Prevention Trigger (check_rating_validity) ===
     73**What it does:** Checks if the user trying to rate a build (1-5 stars) is the creator of the build. If so, it blocks the action. \\
     74**When it triggers:** BEFORE `INSERT` on the `rating_build` table. \\
     75**Why it is useful:** Enforces data integrity by preventing users from artificially boosting their own build's popularity with 5 star ratings. \\
     76
     77{{{
     78CREATE OR REPLACE FUNCTION check_rating_validity()
     79RETURNS TRIGGER
     80LANGUAGE plpgsql AS $$
     81DECLARE
     82    build_owner_id INT;
     83BEGIN
     84    SELECT user_id INTO build_owner_id FROM "build" WHERE id = NEW.build_id;
     85
     86    IF NEW.user_id = build_owner_id THEN
     87        RAISE EXCEPTION 'Cannot rate own builds.';
     88    END IF;
     89
     90    RETURN NEW;
     91END;
     92$$;
     93
     94DROP TRIGGER IF EXISTS trigger_check_self_rating ON "rating_build";
     95CREATE TRIGGER trigger_check_self_rating
     96BEFORE INSERT ON "rating_build"
     97FOR EACH ROW
     98EXECUTE FUNCTION check_rating_validity();
     99}}}
     100
     101
     102
     103
     104
     105
     106
     107
     108
     109
     110
     111
     112
     113
     114
     115
     116
     117
     118
     119
     120
     121
     122
     123