Features added: - Database schema for pages and media files with content types (Rich Text, HTML, Markdown) - Admin API routes for full page CRUD operations - Image upload functionality with file management - Rich text editor using TinyMCE with image insertion - Admin interface for creating/editing pages with SEO options - Dynamic navigation and footer integration - Public page display routes with proper SEO metadata - Support for featured images and content excerpts Admin features: - Create/edit/delete pages with rich content editor - Upload and manage images through media library - Configure pages to appear in navigation or footer - Set page status (Draft, Published, Archived) - SEO title and description management - Real-time preview of content changes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
174 lines
4.1 KiB
TypeScript
174 lines
4.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/db';
|
|
import { verifyAdminAuth } from '@/lib/admin-auth';
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const adminUser = await verifyAdminAuth(request);
|
|
if (!adminUser) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const page = await prisma.page.findUnique({
|
|
where: { id: params.id },
|
|
include: {
|
|
creator: { select: { name: true, email: true } },
|
|
updater: { select: { name: true, email: true } }
|
|
}
|
|
});
|
|
|
|
if (!page) {
|
|
return NextResponse.json(
|
|
{ success: false, error: 'Page not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: page
|
|
});
|
|
} catch (error) {
|
|
console.error('Error fetching page:', error);
|
|
return NextResponse.json(
|
|
{ success: false, error: 'Failed to fetch page' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function PUT(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const adminUser = await verifyAdminAuth(request);
|
|
if (!adminUser) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const body = await request.json();
|
|
const {
|
|
title,
|
|
slug,
|
|
content,
|
|
contentType,
|
|
excerpt,
|
|
featuredImage,
|
|
seoTitle,
|
|
seoDescription,
|
|
status,
|
|
showInNavigation,
|
|
showInFooter,
|
|
navigationOrder,
|
|
footerOrder
|
|
} = body;
|
|
|
|
// Check if page exists
|
|
const existingPage = await prisma.page.findUnique({
|
|
where: { id: params.id }
|
|
});
|
|
|
|
if (!existingPage) {
|
|
return NextResponse.json(
|
|
{ success: false, error: 'Page not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// Check if slug is being changed and conflicts with another page
|
|
if (slug && slug !== existingPage.slug) {
|
|
const conflictingPage = await prisma.page.findUnique({
|
|
where: { slug }
|
|
});
|
|
|
|
if (conflictingPage && conflictingPage.id !== params.id) {
|
|
return NextResponse.json(
|
|
{ success: false, error: 'A page with this slug already exists' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
}
|
|
|
|
const updatedPage = await prisma.page.update({
|
|
where: { id: params.id },
|
|
data: {
|
|
title,
|
|
slug,
|
|
content,
|
|
contentType,
|
|
excerpt,
|
|
featuredImage,
|
|
seoTitle,
|
|
seoDescription,
|
|
status,
|
|
showInNavigation,
|
|
showInFooter,
|
|
navigationOrder,
|
|
footerOrder,
|
|
updatedBy: adminUser.id,
|
|
publishedAt: status === 'PUBLISHED' && !existingPage.publishedAt
|
|
? new Date()
|
|
: status === 'PUBLISHED'
|
|
? existingPage.publishedAt
|
|
: null
|
|
},
|
|
include: {
|
|
creator: { select: { name: true, email: true } },
|
|
updater: { select: { name: true, email: true } }
|
|
}
|
|
});
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: updatedPage
|
|
});
|
|
} catch (error) {
|
|
console.error('Error updating page:', error);
|
|
return NextResponse.json(
|
|
{ success: false, error: 'Failed to update page' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function DELETE(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const adminUser = await verifyAdminAuth(request);
|
|
if (!adminUser) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const page = await prisma.page.findUnique({
|
|
where: { id: params.id }
|
|
});
|
|
|
|
if (!page) {
|
|
return NextResponse.json(
|
|
{ success: false, error: 'Page not found' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
await prisma.page.delete({
|
|
where: { id: params.id }
|
|
});
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: 'Page deleted successfully'
|
|
});
|
|
} catch (error) {
|
|
console.error('Error deleting page:', error);
|
|
return NextResponse.json(
|
|
{ success: false, error: 'Failed to delete page' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
} |