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 || [],
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
26
server/api/charte/index.get.ts
Normal file
26
server/api/charte/index.get.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import prisma from '~/lib/prisma';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const chartes = await prisma.charte.findMany();
|
||||
|
||||
if (!chartes || chartes.length === 0) {
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
message: 'No chartes found.',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: chartes,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error fetching chartes:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
});
|
||||
30
server/api/comments/[id].delete.ts
Normal file
30
server/api/comments/[id].delete.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import prisma from '~/lib/prisma';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = event.context.params?.id;
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: 'Invalid comment ID.',
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
await prisma.comment.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'Comment deleted successfully',
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error deleting comment:', error);
|
||||
return {
|
||||
success: false,
|
||||
message: 'An error occurred while deleting the comment',
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
});
|
||||
37
server/api/comments/[id].put.ts
Normal file
37
server/api/comments/[id].put.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import prisma from '~/lib/prisma';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = event.context.params?.id;
|
||||
const body = await readBody(event);
|
||||
|
||||
if (!id) {
|
||||
return {
|
||||
success: false,
|
||||
message: 'Invalid comment ID.',
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const updatedComment = await prisma.comment.update({
|
||||
where: { id },
|
||||
data: {
|
||||
content: body.content,
|
||||
likes: body.likes,
|
||||
dislikes: body.dislikes,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'Comment updated successfully',
|
||||
data: updatedComment,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error updating comment:', error);
|
||||
return {
|
||||
success: false,
|
||||
message: 'An error occurred while updating the comment',
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
});
|
||||
24
server/api/comments/index.get.ts
Normal file
24
server/api/comments/index.get.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import prisma from '~/lib/prisma';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = getQuery(event);
|
||||
const { articleId } = query;
|
||||
|
||||
try {
|
||||
const comments = await prisma.comment.findMany({
|
||||
where: articleId ? { articleId: String(articleId) } : undefined,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: comments,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error fetching comments:', error);
|
||||
return {
|
||||
success: false,
|
||||
message: 'An error occurred while fetching comments',
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
});
|
||||
37
server/api/comments/index.post.ts
Normal file
37
server/api/comments/index.post.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user