conflicts resolved
This commit is contained in:
27
server/api/articles/[id].get.ts
Normal file
27
server/api/articles/[id].get.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
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 } });
|
||||
|
||||
if (!article) {
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: article,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error fetching article:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
});
|
||||
36
server/api/articles/[id].put.ts
Normal file
36
server/api/articles/[id].put.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
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 article ID.',
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
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) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
});
|
||||
26
server/api/articles/index.get.ts
Normal file
26
server/api/articles/index.get.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import prisma from '~/lib/prisma';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const articles = await prisma.article.findMany();
|
||||
|
||||
if (!articles || articles.length === 0) {
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
message: 'No articles found.',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: articles,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error fetching articles:', error);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
});
|
||||
32
server/api/articles/index.post.ts
Normal file
32
server/api/articles/index.post.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
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 newArticle = await prisma.article.create({
|
||||
data: {
|
||||
id,
|
||||
title,
|
||||
description,
|
||||
likes: likes || 0,
|
||||
dislikes: dislikes || 0,
|
||||
comments: comments || [],
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'Article created successfully',
|
||||
data: newArticle,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error creating article:', error);
|
||||
return {
|
||||
success: false,
|
||||
message: 'An error occurred while creating the article',
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user