Spring Deals - hours only!Up to 70% off on all courses and bundles.-Close
Introduction
Side-effects in functions
Invoking functions

Instruction

Good job! In a similar way, you can assign a function result to a variable.

Take a look at the function below, which finds the ID of the most common topic and highlights all opening posts in this topic:

CREATE FUNCTION highlight_most_frequent_topic_posts()
RETURNS void AS $$
DECLARE
  topic_id integer;
BEGIN
  topic_id := get_most_frequent_topic();

  UPDATE post SET content = '*** ' + content
  WHERE topic_id = topic_id AND parent_id IS NULL;
END;
$$ LANGUAGE plpgsql;

Exercise

Write a new function named downvote() that takes one input parameter post_id of type integer and returns void.

The function should add 1 to the thumbs_down for the post with ID equal to post_id. If the new score (counted as thumbs_up - thumbs_down) is below -10, then the function should lock post author's account (using the function lock_post_author(post_id)).

Finally, the function should raise a notice saying:

User {user_id} has been locked because of downvotes in post {post_id}

Stuck? Here's a hint!

A refresher: You may use the following syntax to include variables in the notice:

RAISE NOTICE 'Variable a = %, variable b = %', a, b;