fix order, add delete functionality
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user