Add EmailTemplate model and CRUD UI in admin console

This commit is contained in:
Ken Yasue
2025-03-25 11:40:45 +01:00
parent 42f2a30610
commit 6b9e208214
13 changed files with 664 additions and 3 deletions

View 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 }
);
}
}