This commit is contained in:
Janis
2023-02-02 00:00:44 +01:00
parent 585e36e4b9
commit 2f340537d4
14 changed files with 77 additions and 24 deletions

View File

@@ -35,6 +35,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
res.status(500).json(error);
});
} else if (req.method == "PUT") {//* PUT
console.log("PUT")
const data: UpdateArticle = req.body;
if (!isValidText(data.title)) {
@@ -53,22 +54,23 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
const newArticle: Prisma.ArticleUncheckedUpdateInput = {
title: data.title,
name: formatTextToUrlName(data.title),
introduction: data.introduction,
title: data.title ?? undefined,
name: formatTextToUrlName(data.title) ?? undefined,
introduction: data.introduction ?? undefined,
categoryId: data.categoryId.toString(),
contentTable: data.contentTable,
markdown: data.markdown,
imageId: data.imageId.toString(),
categoryId: data.categoryId?.toString() ?? undefined,
contentTable: data.contentTable ?? undefined,
markdown: data.markdown ?? undefined,
imageId: data.imageId?.toString() ?? undefined,
}
console.log(newArticle)
await prisma.article.update({ data: newArticle, where: { id: articleId }, include: { category: true } })
.then(
(data) => {
res.json({ success: true, data: data });
},
(errorReason) => {
console.log(errorReason)
if (errorReason.code === "P2002") {
res.json({ target: errorReason.meta.target[0], error: "Already exists" });
}