Add EmailTemplate model and CRUD UI in admin console
This commit is contained in:
55
src/app/api/email-templates/route.ts
Normal file
55
src/app/api/email-templates/route.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getDataSource, EmailTemplate } from '@/lib/database';
|
||||
|
||||
// GET /api/email-templates - Get all email templates
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const dataSource = await getDataSource();
|
||||
const emailTemplateRepository = dataSource.getRepository(EmailTemplate);
|
||||
|
||||
const emailTemplates = await emailTemplateRepository.find({
|
||||
order: { createdAt: 'DESC' }
|
||||
});
|
||||
|
||||
return NextResponse.json(emailTemplates);
|
||||
} catch (error) {
|
||||
console.error('Error fetching email templates:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch email templates' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST /api/email-templates - Create a new email template
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const dataSource = await getDataSource();
|
||||
const emailTemplateRepository = dataSource.getRepository(EmailTemplate);
|
||||
|
||||
const data = await request.json();
|
||||
const { content } = data;
|
||||
|
||||
// Validate required fields
|
||||
if (!content) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Content is required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Create and save the new email template
|
||||
const emailTemplate = new EmailTemplate();
|
||||
emailTemplate.content = content;
|
||||
|
||||
const savedEmailTemplate = await emailTemplateRepository.save(emailTemplate);
|
||||
|
||||
return NextResponse.json(savedEmailTemplate, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Error creating email template:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create email template' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user