From 567d5e0db004ec7fd848ae53812a554d53e115c8 Mon Sep 17 00:00:00 2001 From: Alex Frantz Date: Sun, 6 Sep 2026 22:57:05 -0400 Subject: [PATCH] fix order, add delete functionality --- apps/backend/src/controllers/posts.ts | 24 +++++++++++++---- apps/frontend/src/pages/post.tsx | 37 ++++++++++++++++++++------- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/apps/backend/src/controllers/posts.ts b/apps/backend/src/controllers/posts.ts index 5843cfd..51fe4ce 100644 --- a/apps/backend/src/controllers/posts.ts +++ b/apps/backend/src/controllers/posts.ts @@ -8,7 +8,6 @@ export async function createPost( next: NextFunction, ) { try { - console.log(req.body); const body = schemas.posts.create.parse(req.body); const post = await prisma.post.create({ @@ -55,7 +54,11 @@ export async function getAllPosts( next: NextFunction, ) { try { - const posts = await prisma.post.findMany(); + const posts = await prisma.post.findMany({ + orderBy: { + createdAt: "desc", + }, + }); res.status(200).json(posts); } catch (e) { next(e); @@ -70,13 +73,24 @@ export async function deletePost( try { const slug = schemas.posts.delete.parse(req.params.slug); - await prisma.post.findFirst({ + const post = await prisma.post.findFirst({ where: { slug, }, }); - - res.sendStatus(204); + if (post) { + await prisma.post.delete({ + where: { + id: post.id, + }, + }); + res.sendStatus(204); + return; + } else { + res.status(404).json({ + message: "Couldn't find that post", + }); + } } catch (e) { next(e); } diff --git a/apps/frontend/src/pages/post.tsx b/apps/frontend/src/pages/post.tsx index 7bc59b3..cd2e5ad 100644 --- a/apps/frontend/src/pages/post.tsx +++ b/apps/frontend/src/pages/post.tsx @@ -27,9 +27,20 @@ export default function Post() { } }, []); - useEffect(() => { - console.log(post); - }, [post]); + function deletePost() { + if (!auth.user || !post) return; + + fetch("/api/posts/" + post.slug, { + method: "DELETE", + credentials: "include", + }).then((res) => { + if (!res.ok) { + console.error("Failed to delete post " + res.status); + } else { + navigate("/"); + } + }); + } if (post) { return ( @@ -57,12 +68,20 @@ export default function Post() { "- updated on " + new Date(post.updatedAt).toLocaleString()}

{auth.user && ( -

navigate(`/editor/${post.slug}`)} - > - Edit -

+
+

navigate(`/editor/${post.slug}`)} + > + Edit +

+

+ Delete +

+
)}