add interactions to comments
This commit is contained in:
@@ -2,14 +2,18 @@ import prisma from '~/lib/prisma';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = event.context.params?.id;
|
||||
console.log('prisma' + event.context.params?.id)
|
||||
|
||||
try {
|
||||
const article = await prisma.article.findUnique({ where: { id } });
|
||||
const article = await prisma.article.findUnique({
|
||||
where: { id },
|
||||
include: { comments: true },
|
||||
});
|
||||
|
||||
if (!article) {
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
data: null,
|
||||
message: 'Article not found.',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ export default defineEventHandler(async (event) => {
|
||||
const id = event.context.params?.id;
|
||||
const body = await readBody(event);
|
||||
|
||||
if (!id ) {
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: 'Invalid article ID.',
|
||||
@@ -15,19 +15,19 @@ export default defineEventHandler(async (event) => {
|
||||
const updatedArticle = await prisma.article.update({
|
||||
where: { id },
|
||||
data: {
|
||||
id: body.id,
|
||||
title: body.title,
|
||||
description: body.description,
|
||||
likes: body.likes,
|
||||
dislikes: body.dislikes,
|
||||
comments: body.comments,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: updatedArticle,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error updating article:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
|
||||
@@ -2,12 +2,14 @@ import prisma from '~/lib/prisma';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const articles = await prisma.article.findMany();
|
||||
const articles = await prisma.article.findMany({
|
||||
include: { comments: true },
|
||||
});
|
||||
|
||||
if (!articles || articles.length === 0) {
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
data: null,
|
||||
message: 'No articles found.',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,16 +3,14 @@ import prisma from '~/lib/prisma';
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const body = await readBody(event);
|
||||
const { id, title, description, likes, dislikes, comments } = body;
|
||||
const { title, description, likes, dislikes } = body;
|
||||
|
||||
const newArticle = await prisma.article.create({
|
||||
data: {
|
||||
id,
|
||||
title,
|
||||
description,
|
||||
likes: likes || 0,
|
||||
dislikes: dislikes || 0,
|
||||
comments: comments || [],
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user