From 3a82baec9d793edf81ac2b151b0f4d4159641375 Mon Sep 17 00:00:00 2001
From: Freya Murphy <freya@freyacat.org>
Date: Mon, 1 Apr 2024 11:09:25 -0400
Subject: login and register, liking on homepage

---
 src/db/rest/like/api_like_insert.sql | 51 ++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 src/db/rest/like/api_like_insert.sql

(limited to 'src/db/rest/like/api_like_insert.sql')

diff --git a/src/db/rest/like/api_like_insert.sql b/src/db/rest/like/api_like_insert.sql
new file mode 100644
index 0000000..a02ad4e
--- /dev/null
+++ b/src/db/rest/like/api_like_insert.sql
@@ -0,0 +1,51 @@
+CREATE FUNCTION _api.like_insert()
+RETURNS TRIGGER
+LANGUAGE plpgsql VOLATILE
+AS $BODY$
+DECLARE
+	_user_id INTEGER;
+BEGIN
+	_user_id = _api.get_user_id();
+
+	IF
+		NEW.post_id IS NULL AND
+		NEW.comment_id IS NULL
+	THEN
+		-- for now
+		PERFORM _api.raise_deny();
+	END IF;
+
+	NEW.value := COALESCE(NEW.value, TRUE);
+
+	INSERT INTO admin.like (
+		user_id,
+		post_id,
+		comment_id,
+		value
+	) VALUES (
+		_user_id,
+		NEW.post_id,
+		NEW.comment_id,
+		NEW.value
+	)
+	RETURNING id
+	INTO NEW.id;
+
+	RETURN NEW;
+END
+$BODY$;
+
+GRANT EXECUTE ON FUNCTION _api.like_insert()
+	TO rest_user;
+GRANT INSERT ON TABLE api.like
+	TO rest_user;
+GRANT INSERT ON TABLE admin.like
+	TO rest_user;
+GRANT UPDATE ON TABLE sys.like_id_seq
+	TO rest_user;
+
+CREATE TRIGGER api_like_insert_trgr
+	INSTEAD OF INSERT
+			ON api.like
+			FOR EACH ROW
+				EXECUTE PROCEDURE _api.like_insert();
-- 
cgit v1.2.3-freya

