From 944b6b0526032ad8c1b4a2612d6723bec75e0e4c Mon Sep 17 00:00:00 2001
From: Freya Murphy <freya@freyacat.org>
Date: Fri, 29 Mar 2024 22:29:56 -0400
Subject: start database (user and post), and initial barebones home page

---
 db/rest/post/api_post_insert.sql | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 db/rest/post/api_post_insert.sql

(limited to 'db/rest/post/api_post_insert.sql')

diff --git a/db/rest/post/api_post_insert.sql b/db/rest/post/api_post_insert.sql
new file mode 100644
index 0000000..9eb200c
--- /dev/null
+++ b/db/rest/post/api_post_insert.sql
@@ -0,0 +1,40 @@
+CREATE FUNCTION _api.post_insert()
+RETURNS TRIGGER
+LANGUAGE plpgsql VOLATILE
+AS $BODY$
+DECLARE
+	_user_id INTEGER;
+BEGIN
+	_user_id = _api.get_user_id();
+
+	PERFORM _api.validate_text(
+		_text => NEW.content,
+		_column => 'content',
+		_min => 1,
+		_max => 4096
+	);
+
+	INSERT INTO admin.post (
+		user_id,
+		content
+	) VALUES (
+		_user_id,
+		NEW.content
+	);
+
+	RETURN NEW;
+END
+$BODY$;
+
+GRANT EXECUTE ON FUNCTION _api.post_insert()
+	TO rest_user;
+GRANT INSERT ON TABLE api.post
+	TO rest_user;
+GRANT INSERT ON TABLE admin.post
+	TO rest_user;
+
+CREATE TRIGGER api_post_insert_trgr
+	INSTEAD OF INSERT
+			ON api.post
+			FOR EACH ROW
+				EXECUTE PROCEDURE _api.post_insert();
-- 
cgit v1.2.3-freya

