When I started writing Devy, I focused on using tools I wanted to get better at. The project was primarily a way for me to learn.
ALTER TABLE post ADD COLUMN IF NOT EXISTS like_count integer DEFAULT 0;
CREATE OR REPLACE FUNCTION update_like_count() RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'INSERT' THEN
UPDATE "post"
SET like_count = like_count + 1
WHERE id = NEW.post_id;
ELSIF TG_OP = 'DELETE' THEN
UPDATE "post"
SET like_count = like_count - 1
WHERE id = OLD.post_id;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE TRIGGER trigger_update_like_count
AFTER INSERT OR DELETE ON "like"
FOR EACH ROW
EXECUTE FUNCTION update_like_count();