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" });
}

View File

@@ -72,6 +72,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
data.name = formatTextToUrlName(data.title);
data.categoryId = data.categoryId.toString();
prisma.article
.create({ data: data, include: { category: true } })
.then(

View File

@@ -22,7 +22,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
code: "404",
message: "No article with this name found!",
};
res.status(404).send(JSON.stringify(error));
res.status(404).json(error);
}
})
.catch((err) => {
@@ -31,6 +31,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
code: "500",
message: err,
};
res.status(500).send(JSON.stringify(error));
res.status(500).json(error);
});
}