From b4dc7057fc537973574354a601a4bcab9561d56d Mon Sep 17 00:00:00 2001 From: Zokhoi <20432565+Zokhoi@users.noreply.github.com> Date: Sun, 10 Sep 2023 23:42:43 +0800 Subject: [PATCH] Allow page deletion --- framerail/src/lib/server/page/delete.ts | 28 +++++++++++++++++++ .../src/lib/server/{deepwell => page}/edit.ts | 0 .../src/routes/[slug]/[...extra]/+server.ts | 16 ++++++++++- .../src/routes/[slug]/[...extra]/page.svelte | 18 +++++++++++- 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 framerail/src/lib/server/page/delete.ts rename framerail/src/lib/server/{deepwell => page}/edit.ts (100%) diff --git a/framerail/src/lib/server/page/delete.ts b/framerail/src/lib/server/page/delete.ts new file mode 100644 index 0000000000..3d1d0f5d89 --- /dev/null +++ b/framerail/src/lib/server/page/delete.ts @@ -0,0 +1,28 @@ +import { wellfetch } from "$lib/server/deepwell/index.ts" +import type { Optional } from "$lib/types.ts" + +export async function pageDelete( + siteId: number, + pageId: Optional, + slug: string, + revisionComments: Optional, +): object { + const response = await wellfetch("/page", { + method: "DELETE", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + siteId, + page: pageId ?? slug, + userId: 1, // TODO: identify user session and pass the user to the API request + revisionComments, + }) + }) + + if (!response.ok) { + throw new Error("Unable to delete page") + } + + return response.json() +} diff --git a/framerail/src/lib/server/deepwell/edit.ts b/framerail/src/lib/server/page/edit.ts similarity index 100% rename from framerail/src/lib/server/deepwell/edit.ts rename to framerail/src/lib/server/page/edit.ts diff --git a/framerail/src/routes/[slug]/[...extra]/+server.ts b/framerail/src/routes/[slug]/[...extra]/+server.ts index d925433438..ca0fa7eae6 100644 --- a/framerail/src/routes/[slug]/[...extra]/+server.ts +++ b/framerail/src/routes/[slug]/[...extra]/+server.ts @@ -1,4 +1,5 @@ -import { pageEdit } from "$lib/server/deepwell/edit.ts" +import { pageEdit } from "$lib/server/page/edit" +import { pageDelete } from "$lib/server/page/delete" export async function POST(event) { let data = await event.request.formData() @@ -19,3 +20,16 @@ export async function POST(event) { return new Response(JSON.stringify(res)); } + +export async function DELETE(event) { + let data = await event.request.formData() + let slug = event.params.slug + + let pageIdVal = data.get("page-id")?.toString() + let pageId = pageIdVal ? parseInt(pageIdVal) : null + let siteId = parseInt(data.get("site-id")?.toString() ?? "1") + let comments = data.get("comments")?.toString() ?? "" + + let res = await pageDelete(siteId, pageId, slug, comments) + return new Response(JSON.stringify(res)); +} diff --git a/framerail/src/routes/[slug]/[...extra]/page.svelte b/framerail/src/routes/[slug]/[...extra]/page.svelte index 9e3b51403f..d671c137c2 100644 --- a/framerail/src/routes/[slug]/[...extra]/page.svelte +++ b/framerail/src/routes/[slug]/[...extra]/page.svelte @@ -1,6 +1,17 @@