Files
DAV/server/api/comments/index.post.ts
Do-raa e24b3e1955
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
add interactions to comments
2024-12-23 15:44:36 +01:00

38 lines
868 B
TypeScript

import prisma from '~/lib/prisma';
export default defineEventHandler(async (event) => {
try {
const body = await readBody(event);
const { content, articleId, likes, dislikes } = body;
if (!content || !articleId) {
return {
success: false,
message: 'Content and articleId are required to create a comment.',
};
}
const newComment = await prisma.comment.create({
data: {
content,
articleId,
likes: likes || 0,
dislikes: dislikes || 0,
},
});
return {
success: true,
message: 'Comment created successfully',
data: newComment,
};
} catch (error) {
console.error('Error creating comment:', error);
return {
success: false,
message: 'An error occurred while creating the comment',
error: error.message,
};
}
});