fix order, add delete functionality
This commit is contained in:
@@ -8,7 +8,6 @@ export async function createPost(
|
|||||||
next: NextFunction,
|
next: NextFunction,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
console.log(req.body);
|
|
||||||
const body = schemas.posts.create.parse(req.body);
|
const body = schemas.posts.create.parse(req.body);
|
||||||
|
|
||||||
const post = await prisma.post.create({
|
const post = await prisma.post.create({
|
||||||
@@ -55,7 +54,11 @@ export async function getAllPosts(
|
|||||||
next: NextFunction,
|
next: NextFunction,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const posts = await prisma.post.findMany();
|
const posts = await prisma.post.findMany({
|
||||||
|
orderBy: {
|
||||||
|
createdAt: "desc",
|
||||||
|
},
|
||||||
|
});
|
||||||
res.status(200).json(posts);
|
res.status(200).json(posts);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
next(e);
|
next(e);
|
||||||
@@ -70,13 +73,24 @@ export async function deletePost(
|
|||||||
try {
|
try {
|
||||||
const slug = schemas.posts.delete.parse(req.params.slug);
|
const slug = schemas.posts.delete.parse(req.params.slug);
|
||||||
|
|
||||||
await prisma.post.findFirst({
|
const post = await prisma.post.findFirst({
|
||||||
where: {
|
where: {
|
||||||
slug,
|
slug,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
if (post) {
|
||||||
res.sendStatus(204);
|
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) {
|
} catch (e) {
|
||||||
next(e);
|
next(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,9 +27,20 @@ export default function Post() {
|
|||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
function deletePost() {
|
||||||
console.log(post);
|
if (!auth.user || !post) return;
|
||||||
}, [post]);
|
|
||||||
|
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) {
|
if (post) {
|
||||||
return (
|
return (
|
||||||
@@ -57,12 +68,20 @@ export default function Post() {
|
|||||||
"- updated on " + new Date(post.updatedAt).toLocaleString()}
|
"- updated on " + new Date(post.updatedAt).toLocaleString()}
|
||||||
</p>
|
</p>
|
||||||
{auth.user && (
|
{auth.user && (
|
||||||
<p
|
<div className="inline-block space-x-2">
|
||||||
className="text-sm opacity-50 underline hover:cursor-pointer"
|
<p
|
||||||
onClick={() => navigate(`/editor/${post.slug}`)}
|
className="text-sm opacity-50 underline hover:cursor-pointer inline"
|
||||||
>
|
onClick={() => navigate(`/editor/${post.slug}`)}
|
||||||
Edit
|
>
|
||||||
</p>
|
Edit
|
||||||
|
</p>
|
||||||
|
<p
|
||||||
|
className="text-sm opacity-50 underline hover:cursor-pointer inline"
|
||||||
|
onClick={deletePost}
|
||||||
|
>
|
||||||
|
Delete
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|||||||
Reference in New Issue
Block a user