You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Portfolio/src/pages/api/blog/new.ts

51 lines
1.1 KiB

import { NextApiHandler } from "next";
import { Post } from "@models/post";
import { Response } from "@models/response";
import { methodNotAllowed, unauthorized } from "@utils/errors";
import prisma from "@utils/prisma";
import { withIronSession } from "@utils/session";
const handle: NextApiHandler<Response> = async (req, res) => {
if (req.method?.toUpperCase() != "POST") {
res.status(405).json(methodNotAllowed);
return;
}
const postData = Post.safeParse(req.body);
if (!postData.success) {
res.status(403).json({ ok: false, error: postData.error });
return;
}
const user = req.session.user;
if (user === undefined) {
res.status(401).json(unauthorized);
return;
}
const { publish, ...data } = postData.data;
await prisma.user
.update({
where: { id: user.id },
data: {
posts: {
create: {
...data,
published: publish,
tags: data.tags.trim().split(" "),
content: data.content.trim()
}
}
}
})
.then(() => res.json({ ok: true, data: "Successfully created new post" }))
.catch((error) => res.status(500).json({ ok: false, error }));
};
export default withIronSession(handle);