conflicts resolved
This commit is contained in:
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