From 1b1767c0f7cb2f9e9c40c345ee3f4630fb7e18c0 Mon Sep 17 00:00:00 2001 From: thiloho <123883702+thiloho@users.noreply.github.com> Date: Tue, 27 Aug 2024 16:39:29 +0200 Subject: [PATCH] Add categories for docs template --- ...40827090504_categories_and_publication.sql | 116 +++++++++++++++ .../src/lib/components/WebsiteEditor.svelte | 5 + .../src/lib/templates/blog/BlogArticle.svelte | 12 +- .../src/lib/templates/blog/BlogIndex.svelte | 12 +- .../lib/templates/blog/common/BlogNav.svelte | 17 --- .../Footer.svelte} | 0 .../BlogHead.svelte => common/Head.svelte} | 0 web-app/src/lib/templates/common/Nav.svelte | 45 ++++++ .../src/lib/templates/docs/DocsArticle.svelte | 51 +++++++ .../src/lib/templates/docs/DocsEntry.svelte | 52 ------- .../src/lib/templates/docs/DocsIndex.svelte | 76 +++++----- web-app/src/lib/utils.ts | 8 +- .../website/[websiteId]/+page.svelte | 1 + .../[websiteId]/articles/+page.server.ts | 1 - .../website/[websiteId]/articles/+page.svelte | 1 + .../articles/[articleId]/+page.server.ts | 17 ++- .../articles/[articleId]/+page.svelte | 12 ++ .../[websiteId]/categories/+page.server.ts | 95 ++++++++++++ .../[websiteId]/categories/+page.svelte | 138 ++++++++++++++++++ .../[websiteId]/collaborators/+page.svelte | 10 +- .../[websiteId]/publish/+page.server.ts | 56 ++++--- .../website/[websiteId]/publish/+page.svelte | 5 +- web-app/template-styles/common-styles.css | 17 ++- web-app/template-styles/docs-styles.css | 58 ++++++++ 24 files changed, 651 insertions(+), 154 deletions(-) create mode 100644 rest-api/db/migrations/20240827090504_categories_and_publication.sql delete mode 100644 web-app/src/lib/templates/blog/common/BlogNav.svelte rename web-app/src/lib/templates/{blog/common/BlogFooter.svelte => common/Footer.svelte} (100%) rename web-app/src/lib/templates/{blog/common/BlogHead.svelte => common/Head.svelte} (100%) create mode 100644 web-app/src/lib/templates/common/Nav.svelte create mode 100644 web-app/src/lib/templates/docs/DocsArticle.svelte delete mode 100644 web-app/src/lib/templates/docs/DocsEntry.svelte create mode 100644 web-app/src/routes/(authenticated)/website/[websiteId]/categories/+page.server.ts create mode 100644 web-app/src/routes/(authenticated)/website/[websiteId]/categories/+page.svelte create mode 100644 web-app/template-styles/docs-styles.css diff --git a/rest-api/db/migrations/20240827090504_categories_and_publication.sql b/rest-api/db/migrations/20240827090504_categories_and_publication.sql new file mode 100644 index 0000000..688f420 --- /dev/null +++ b/rest-api/db/migrations/20240827090504_categories_and_publication.sql @@ -0,0 +1,116 @@ +-- migrate:up +CREATE TABLE internal.docs_category ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid (), + website_id UUID REFERENCES internal.website (id) ON DELETE CASCADE NOT NULL, + user_id UUID REFERENCES internal.user (id) ON DELETE SET NULL DEFAULT (CURRENT_SETTING('request.jwt.claims', TRUE)::JSON ->> 'user_id') ::UUID, + category_name VARCHAR(50) NOT NULL CHECK (TRIM(category_name) != ''), + category_weight INTEGER CHECK (category_weight >= 0) NOT NULL, + UNIQUE (website_id, category_name), + UNIQUE (website_id, category_weight) +); + +ALTER TABLE internal.website + ADD COLUMN is_published BOOLEAN NOT NULL DEFAULT FALSE; + +ALTER TABLE internal.article + ADD COLUMN category UUID REFERENCES internal.docs_category (id) ON DELETE SET NULL; + +ALTER TABLE internal.article + ALTER COLUMN user_id SET DEFAULT (CURRENT_SETTING('request.jwt.claims', TRUE)::JSON ->> 'user_id')::UUID; + +ALTER TABLE internal.docs_category ENABLE ROW LEVEL SECURITY; + +CREATE POLICY view_categories ON internal.docs_category + FOR SELECT + USING (internal.user_has_website_access (website_id, 10)); + +CREATE POLICY update_category ON internal.docs_category + FOR UPDATE + USING (internal.user_has_website_access (website_id, 20)); + +CREATE POLICY delete_category ON internal.docs_category + FOR DELETE + USING (internal.user_has_website_access (website_id, 20, article_user_id => user_id)); + +CREATE POLICY insert_category ON internal.docs_category + FOR INSERT + WITH CHECK (internal.user_has_website_access (website_id, 20)); + +CREATE VIEW api.docs_category WITH ( security_invoker = ON +) AS +SELECT + id, + website_id, + user_id, + category_name, + category_weight +FROM + internal.docs_category; + +CREATE OR REPLACE VIEW api.article WITH ( security_invoker = ON +) AS +SELECT + id, + website_id, + user_id, + title, + meta_description, + meta_author, + cover_image, + publication_date, + main_content, + created_at, + last_modified_at, + last_modified_by, + category -- New column +FROM + internal.article; + +GRANT SELECT, INSERT, UPDATE, DELETE ON internal.docs_category TO authenticated_user; + +GRANT SELECT, INSERT, UPDATE, DELETE ON api.docs_category TO authenticated_user; + +GRANT SELECT, INSERT, UPDATE, DELETE ON api.article TO authenticated_user; + +-- migrate:down +DROP POLICY view_categories ON internal.docs_category; + +DROP POLICY update_category ON internal.docs_category; + +DROP POLICY delete_category ON internal.docs_category; + +DROP POLICY insert_category ON internal.docs_category; + +DROP VIEW api.article; + +CREATE VIEW api.article WITH ( security_invoker = ON +) AS +SELECT + id, + website_id, + user_id, + title, + meta_description, + meta_author, + cover_image, + publication_date, + main_content, + created_at, + last_modified_at, + last_modified_by +FROM + internal.article; + +DROP VIEW api.docs_category; + +ALTER TABLE internal.article + DROP COLUMN category; + +DROP TABLE internal.docs_category; + +ALTER TABLE internal.website + DROP COLUMN is_published; + +ALTER TABLE internal.article + ALTER COLUMN user_id DROP DEFAULT; + diff --git a/web-app/src/lib/components/WebsiteEditor.svelte b/web-app/src/lib/components/WebsiteEditor.svelte index 47da211..9715784 100644 --- a/web-app/src/lib/components/WebsiteEditor.svelte +++ b/web-app/src/lib/components/WebsiteEditor.svelte @@ -5,6 +5,7 @@ const { id, + contentType, title, children, fullPreview = false, @@ -12,6 +13,7 @@ previewScrollTop = 0 }: { id: string; + contentType: string; title: string; children: Snippet; fullPreview?: boolean; @@ -41,6 +43,9 @@