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>
144 lines
3.6 KiB
TypeScript
144 lines
3.6 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) {
|
|
try {
|
|
const adminUser = await verifyAdminAuth(request);
|
|
if (!adminUser) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
const page = parseInt(searchParams.get('page') || '1');
|
|
const limit = parseInt(searchParams.get('limit') || '10');
|
|
const status = searchParams.get('status');
|
|
const search = searchParams.get('search');
|
|
|
|
const skip = (page - 1) * limit;
|
|
|
|
const where: any = {};
|
|
if (status && status !== 'all') {
|
|
where.status = status.toUpperCase();
|
|
}
|
|
if (search) {
|
|
where.OR = [
|
|
{ title: { contains: search, mode: 'insensitive' } },
|
|
{ content: { contains: search, mode: 'insensitive' } },
|
|
{ slug: { contains: search, mode: 'insensitive' } }
|
|
];
|
|
}
|
|
|
|
const [pages, total] = await Promise.all([
|
|
prisma.page.findMany({
|
|
where,
|
|
orderBy: { updatedAt: 'desc' },
|
|
skip,
|
|
take: limit,
|
|
include: {
|
|
creator: { select: { name: true, email: true } },
|
|
updater: { select: { name: true, email: true } }
|
|
}
|
|
}),
|
|
prisma.page.count({ where })
|
|
]);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: pages,
|
|
pagination: {
|
|
page,
|
|
limit,
|
|
total,
|
|
pages: Math.ceil(total / limit)
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error('Error fetching pages:', error);
|
|
return NextResponse.json(
|
|
{ success: false, error: 'Failed to fetch pages' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
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 = 'RICH_TEXT',
|
|
excerpt,
|
|
featuredImage,
|
|
seoTitle,
|
|
seoDescription,
|
|
status = 'DRAFT',
|
|
showInNavigation = false,
|
|
showInFooter = false,
|
|
navigationOrder,
|
|
footerOrder
|
|
} = body;
|
|
|
|
if (!title || !slug || !content) {
|
|
return NextResponse.json(
|
|
{ success: false, error: 'Title, slug, and content are required' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Check if slug already exists
|
|
const existingPage = await prisma.page.findUnique({
|
|
where: { slug }
|
|
});
|
|
|
|
if (existingPage) {
|
|
return NextResponse.json(
|
|
{ success: false, error: 'A page with this slug already exists' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const page = await prisma.page.create({
|
|
data: {
|
|
title,
|
|
slug,
|
|
content,
|
|
contentType,
|
|
excerpt,
|
|
featuredImage,
|
|
seoTitle,
|
|
seoDescription,
|
|
status,
|
|
showInNavigation,
|
|
showInFooter,
|
|
navigationOrder,
|
|
footerOrder,
|
|
createdBy: adminUser.id,
|
|
updatedBy: adminUser.id,
|
|
publishedAt: status === 'PUBLISHED' ? new Date() : null
|
|
},
|
|
include: {
|
|
creator: { select: { name: true, email: true } },
|
|
updater: { select: { name: true, email: true } }
|
|
}
|
|
});
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: page
|
|
});
|
|
} catch (error) {
|
|
console.error('Error creating page:', error);
|
|
return NextResponse.json(
|
|
{ success: false, error: 'Failed to create page' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
} |