88 lines
1.9 KiB
JavaScript
88 lines
1.9 KiB
JavaScript
|
const { PrismaClient } = require("@prisma/client");
|
||
|
const prisma = new PrismaClient();
|
||
|
|
||
|
async function createComments(req, res) {
|
||
|
const { comment, blog, blogId } = req.body;
|
||
|
|
||
|
try {
|
||
|
const newComment = await prisma.Comment.create({
|
||
|
data: {
|
||
|
comment,
|
||
|
blog,
|
||
|
blogId,
|
||
|
},
|
||
|
});
|
||
|
res.status(201).json(newComment);
|
||
|
} catch (error) {
|
||
|
console.error("Error creating comment:", error);
|
||
|
res
|
||
|
.status(500)
|
||
|
.json({ message: "Error creating comment", error: error.message });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function getComments(req, res) {
|
||
|
try {
|
||
|
const comments = await prisma.Comment.findMany();
|
||
|
res.json(comments);
|
||
|
} catch (error) {
|
||
|
console.error("Error creating comment:", error);
|
||
|
res
|
||
|
.status(500)
|
||
|
.json({ message: "Error creating comment", error: error.message });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function updateComments(req, res) {
|
||
|
const { comment, blog, blogId } = req.body;
|
||
|
const id = req.params.id;
|
||
|
try {
|
||
|
const comments = await prisma.Comment.findUnique({
|
||
|
where: { id },
|
||
|
});
|
||
|
if (!comments) {
|
||
|
return res.status(404).send({
|
||
|
message: "Comment not found",
|
||
|
});
|
||
|
}
|
||
|
const updatedComment = await prisma.Comment.update({
|
||
|
where: { id },
|
||
|
data: {
|
||
|
comment,
|
||
|
blog,
|
||
|
blogId,
|
||
|
},
|
||
|
});
|
||
|
|
||
|
res.status(200).send(updatedComment);
|
||
|
} catch (error) {
|
||
|
console.error("Error updating comment:", error);
|
||
|
res.status(500).send({
|
||
|
message: "Error updating comment",
|
||
|
error: error.message,
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function deleteComments(req, res) {
|
||
|
const { id } = req.params;
|
||
|
try {
|
||
|
const comment = await prisma.Comment.delete({
|
||
|
where: { id },
|
||
|
});
|
||
|
res.json(comment);
|
||
|
} catch (error) {
|
||
|
console.error("Error deleting comment:", error);
|
||
|
res
|
||
|
.status(500)
|
||
|
.json({ message: "Error deleting comment", error: error.message });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
createComments,
|
||
|
getComments,
|
||
|
updateComments,
|
||
|
deleteComments,
|
||
|
};
|