| | 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 | {{{ |
| | 10 | CREATE OR REPLACE FUNCTION update_build_total_price() |
| | 11 | RETURNS TRIGGER |
| | 12 | LANGUAGE plpgsql AS $$ |
| | 13 | DECLARE |
| | 14 | target_build_id INT; |
| | 15 | BEGIN |
| | 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; |
| | 32 | END; |
| | 33 | $$; |
| | 34 | |
| | 35 | DROP TRIGGER IF EXISTS trigger_auto_update_price ON "build_component"; |
| | 36 | CREATE TRIGGER trigger_auto_update_price |
| | 37 | AFTER INSERT OR UPDATE OR DELETE ON "build_component" |
| | 38 | FOR EACH ROW |
| | 39 | EXECUTE 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 | {{{ |
| | 49 | CREATE OR REPLACE FUNCTION check_review_validity() |
| | 50 | RETURNS TRIGGER |
| | 51 | LANGUAGE plpgsql AS $$ |
| | 52 | DECLARE |
| | 53 | build_owner_id INT; |
| | 54 | BEGIN |
| | 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; |
| | 62 | END; |
| | 63 | $$; |
| | 64 | |
| | 65 | DROP TRIGGER IF EXISTS trigger_check_self_review ON "review"; |
| | 66 | CREATE TRIGGER trigger_check_self_review |
| | 67 | BEFORE INSERT ON "review" |
| | 68 | FOR EACH ROW |
| | 69 | EXECUTE 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 | {{{ |
| | 78 | CREATE OR REPLACE FUNCTION check_rating_validity() |
| | 79 | RETURNS TRIGGER |
| | 80 | LANGUAGE plpgsql AS $$ |
| | 81 | DECLARE |
| | 82 | build_owner_id INT; |
| | 83 | BEGIN |
| | 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; |
| | 91 | END; |
| | 92 | $$; |
| | 93 | |
| | 94 | DROP TRIGGER IF EXISTS trigger_check_self_rating ON "rating_build"; |
| | 95 | CREATE TRIGGER trigger_check_self_rating |
| | 96 | BEFORE INSERT ON "rating_build" |
| | 97 | FOR EACH ROW |
| | 98 | EXECUTE 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 | |