| | 11 | |
| | 12 | {{{ |
| | 13 | CREATE OR REPLACE PROCEDURE project.register_new_consumer_with_gift( |
| | 14 | p_email VARCHAR, |
| | 15 | p_username VARCHAR, |
| | 16 | p_password VARCHAR, |
| | 17 | p_shipping_address VARCHAR, |
| | 18 | p_telephone_number VARCHAR |
| | 19 | ) |
| | 20 | LANGUAGE plpgsql |
| | 21 | AS $$ |
| | 22 | DECLARE |
| | 23 | new_user_id BIGINT; |
| | 24 | new_wishlist_id BIGINT; |
| | 25 | BEGIN |
| | 26 | |
| | 27 | IF EXISTS ( |
| | 28 | SELECT 1 |
| | 29 | FROM project.users |
| | 30 | WHERE email = p_email |
| | 31 | OR username = p_username |
| | 32 | OR telephone_number = p_telephone_number |
| | 33 | ) THEN |
| | 34 | RAISE EXCEPTION |
| | 35 | 'A user with the same email, username, or telephone number already exists.'; |
| | 36 | END IF; |
| | 37 | |
| | 38 | SELECT COALESCE(MAX(user_id), 0) + 1 |
| | 39 | INTO new_user_id |
| | 40 | FROM project.users; |
| | 41 | |
| | 42 | SELECT COALESCE(MAX(wishlist_id), 0) + 1 |
| | 43 | INTO new_wishlist_id |
| | 44 | FROM project.wishlists; |
| | 45 | |
| | 46 | INSERT INTO project.users |
| | 47 | ( |
| | 48 | user_id, |
| | 49 | email, |
| | 50 | username, |
| | 51 | password, |
| | 52 | date_created, |
| | 53 | shipping_address, |
| | 54 | telephone_number |
| | 55 | ) |
| | 56 | VALUES |
| | 57 | ( |
| | 58 | new_user_id, |
| | 59 | p_email, |
| | 60 | p_username, |
| | 61 | p_password, |
| | 62 | CURRENT_DATE, |
| | 63 | p_shipping_address, |
| | 64 | p_telephone_number |
| | 65 | ); |
| | 66 | |
| | 67 | INSERT INTO project.consumers |
| | 68 | ( |
| | 69 | user_id, |
| | 70 | points_collected |
| | 71 | ) |
| | 72 | VALUES |
| | 73 | ( |
| | 74 | new_user_id, |
| | 75 | 50 |
| | 76 | ); |
| | 77 | |
| | 78 | INSERT INTO project.wishlists |
| | 79 | ( |
| | 80 | wishlist_id, |
| | 81 | user_id |
| | 82 | ) |
| | 83 | VALUES |
| | 84 | ( |
| | 85 | new_wishlist_id, |
| | 86 | new_user_id |
| | 87 | ); |
| | 88 | |
| | 89 | END; |
| | 90 | $$; |
| | 91 | }}} |