fix order, add delete functionality

This commit is contained in:
2026-09-06 22:57:05 -04:00
parent 04ad21d4b3
commit 567d5e0db0
2 changed files with 47 additions and 14 deletions
+19 -5
View File
@@ -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);
}