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,
|
||||
};
|
||||
}
|
||||
});
|
||||
49
server/api/charte/index.post.ts
Normal file
49
server/api/charte/index.post.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import prisma from '~/lib/prisma';
|
||||
import { defineEventHandler, readBody, createError } from 'h3';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Read the request body
|
||||
const body = await readBody(event);
|
||||
|
||||
// Validate the input: Name, Email, and optionally Comment
|
||||
const { name, email, comment } = body;
|
||||
|
||||
if (!name || !email) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'Name and Email are required',
|
||||
});
|
||||
}
|
||||
|
||||
// Create the charte entry in the database
|
||||
const charte = await prisma.charte.create({
|
||||
data: {
|
||||
name,
|
||||
email,
|
||||
comment: comment || '', // Default to an empty string if no comment is provided
|
||||
},
|
||||
});
|
||||
|
||||
// Return success and the created charte data
|
||||
return { success: true, charte };
|
||||
} catch (error) {
|
||||
// Log the error for debugging
|
||||
console.error('Error in charte API:', error);
|
||||
|
||||
// Handle specific Prisma or validation errors
|
||||
if (error.code === 'P2002') {
|
||||
// Prisma-specific error for unique constraint violations (e.g., unique email)
|
||||
throw createError({
|
||||
statusCode: 409,
|
||||
message: 'A charte entry with this email already exists.',
|
||||
});
|
||||
}
|
||||
|
||||
// Return a general error for unexpected issues
|
||||
throw createError({
|
||||
statusCode: error.statusCode || 500,
|
||||
message: error.message || 'Internal Server Error',
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user