Instruction
Good stuff! We're familiar with the structure of the newsletter and subscription tables, so we can go ahead and implement a function to unsubscribe a customer from a given newsletter.
Exercise
Create a new function named opt_in_out() that takes three parameters:
p_opt_in boolean– a flag denoting whether a customer wants to opt in (TRUE) or opt out (FALSE).p_cust_id int– the customer's ID.p_newsl_id int– the newsletter's ID.
The function should perform the following operations:
- Create a
p_exists booleanvariable that will storeTRUEif there exists a row in thesubscriptiontable that matches the function's arguments. - Create separate cases:
- If
p_opt_in AND NOT p_exists, then insert these values into thesubscriptiontable:p_cust_id,p_newsl_id, andcurrent_timestamp. - If
NOT p_opt_in AND p_exists, then delete the proper row from the subscription table. -
If
p_opt_in AND p_exists, then log the following information:Argument Value p_process_name 'opt_in_out()' p_message_text 'Customer with ID of {p_cust_id} has already subscribed to the newsletter with ID of {p_newsl_id}' p_level 'info' Finish the execution here.
-
In other cases, log the following information:
Argument Value p_process_name 'opt_in_out()' p_message_text 'Invalid customer with ID of {p_cust_id} or newsletter with ID of {p_newsl_id}' p_level 'error' Finish the execution here.
- If
-
Finally, log the following information:
Argument Value p_process_name 'opt_in_out()' p_message_text 'Opt in/out request for the customer with ID of {p_cust_id} and newsletter with ID of {p_newsl_id} has been processed' p_level 'info'



