Add comprehensive page management system to admin dashboard
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>
This commit is contained in:
137
app/api/admin/media/route.ts
Normal file
137
app/api/admin/media/route.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { writeFile, mkdir } from 'fs/promises';
|
||||
import { existsSync } from 'fs';
|
||||
import path from 'path';
|
||||
import { prisma } from '@/lib/db';
|
||||
import { verifyAdminAuth } from '@/lib/admin-auth';
|
||||
|
||||
const UPLOAD_DIR = path.join(process.cwd(), 'public', 'uploads');
|
||||
const ALLOWED_TYPES = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp', 'image/gif'];
|
||||
const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const adminUser = await verifyAdminAuth(request);
|
||||
if (!adminUser) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const formData = await request.formData();
|
||||
const file = formData.get('file') as File;
|
||||
const alt = formData.get('alt') as string;
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'No file provided' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!ALLOWED_TYPES.includes(file.type)) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Invalid file type. Only images are allowed.' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (file.size > MAX_FILE_SIZE) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'File too large. Maximum size is 5MB.' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Ensure upload directory exists
|
||||
if (!existsSync(UPLOAD_DIR)) {
|
||||
await mkdir(UPLOAD_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
// Generate unique filename
|
||||
const timestamp = Date.now();
|
||||
const extension = path.extname(file.name);
|
||||
const baseFilename = file.name.replace(extension, '').replace(/[^a-zA-Z0-9]/g, '-');
|
||||
const filename = `${timestamp}-${baseFilename}${extension}`;
|
||||
const filePath = path.join(UPLOAD_DIR, filename);
|
||||
|
||||
// Write file to disk
|
||||
const bytes = await file.arrayBuffer();
|
||||
const buffer = Buffer.from(bytes);
|
||||
await writeFile(filePath, buffer);
|
||||
|
||||
// Save to database
|
||||
const mediaFile = await prisma.mediaFile.create({
|
||||
data: {
|
||||
filename,
|
||||
originalName: file.name,
|
||||
mimeType: file.type,
|
||||
size: file.size,
|
||||
path: filePath,
|
||||
url: `/uploads/${filename}`,
|
||||
alt: alt || null,
|
||||
uploadedBy: adminUser.id
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: mediaFile
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error uploading file:', error);
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Failed to upload file' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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') || '20');
|
||||
const type = searchParams.get('type');
|
||||
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where: any = {};
|
||||
if (type) {
|
||||
where.mimeType = { startsWith: type };
|
||||
}
|
||||
|
||||
const [files, total] = await Promise.all([
|
||||
prisma.mediaFile.findMany({
|
||||
where,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
skip,
|
||||
take: limit,
|
||||
include: {
|
||||
uploader: { select: { name: true, email: true } }
|
||||
}
|
||||
}),
|
||||
prisma.mediaFile.count({ where })
|
||||
]);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: files,
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
pages: Math.ceil(total / limit)
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching media files:', error);
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Failed to fetch media files' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
174
app/api/admin/pages/[id]/route.ts
Normal file
174
app/api/admin/pages/[id]/route.ts
Normal file
@@ -0,0 +1,174 @@
|
||||
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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
144
app/api/admin/pages/route.ts
Normal file
144
app/api/admin/pages/route.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -9,17 +9,17 @@ export async function GET(request: Request) {
|
||||
console.log('Books API called')
|
||||
const { searchParams } = new URL(request.url)
|
||||
const locale = searchParams.get('locale') || 'ro'
|
||||
const versionAbbr = searchParams.get('version') // Optional specific version
|
||||
console.log('Locale:', locale, 'Version:', versionAbbr)
|
||||
const versionId = searchParams.get('version') // Optional specific version ID
|
||||
console.log('Locale:', locale, 'Version ID:', versionId)
|
||||
|
||||
// Get the appropriate Bible version
|
||||
let bibleVersion
|
||||
const langCandidates = Array.from(new Set([locale, locale.toLowerCase(), locale.toUpperCase()]))
|
||||
if (versionAbbr) {
|
||||
if (versionId) {
|
||||
// Use specific version if provided
|
||||
bibleVersion = await prisma.bibleVersion.findFirst({
|
||||
where: {
|
||||
abbreviation: versionAbbr,
|
||||
id: versionId,
|
||||
language: { in: langCandidates }
|
||||
}
|
||||
})
|
||||
|
||||
@@ -9,9 +9,10 @@ export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const bookId = searchParams.get('book') || ''
|
||||
const chapterNum = parseInt(searchParams.get('chapter') || '1')
|
||||
const versionId = searchParams.get('version') || ''
|
||||
|
||||
// Check cache first
|
||||
const cacheKey = CacheManager.getChapterKey(bookId, chapterNum)
|
||||
// Check cache first (include version in cache key)
|
||||
const cacheKey = CacheManager.getChapterKey(bookId, chapterNum, versionId)
|
||||
const cachedChapter = await CacheManager.get(cacheKey)
|
||||
|
||||
if (cachedChapter) {
|
||||
@@ -25,7 +26,8 @@ export async function GET(request: Request) {
|
||||
const chapter = await prisma.bibleChapter.findFirst({
|
||||
where: {
|
||||
bookId,
|
||||
chapterNum
|
||||
chapterNum,
|
||||
book: versionId ? { versionId } : undefined
|
||||
},
|
||||
include: {
|
||||
verses: {
|
||||
@@ -33,7 +35,11 @@ export async function GET(request: Request) {
|
||||
verseNum: 'asc'
|
||||
}
|
||||
},
|
||||
book: true
|
||||
book: {
|
||||
include: {
|
||||
version: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
47
app/api/pages/[slug]/route.ts
Normal file
47
app/api/pages/[slug]/route.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/db';
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { slug: string } }
|
||||
) {
|
||||
try {
|
||||
const page = await prisma.page.findUnique({
|
||||
where: {
|
||||
slug: params.slug,
|
||||
status: 'PUBLISHED'
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
slug: true,
|
||||
content: true,
|
||||
contentType: true,
|
||||
excerpt: true,
|
||||
featuredImage: true,
|
||||
seoTitle: true,
|
||||
seoDescription: true,
|
||||
publishedAt: true,
|
||||
updatedAt: 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 by slug:', error);
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Failed to fetch page' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
53
app/api/pages/route.ts
Normal file
53
app/api/pages/route.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/db';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const location = searchParams.get('location'); // 'navigation', 'footer', or 'all'
|
||||
|
||||
const where: any = {
|
||||
status: 'PUBLISHED'
|
||||
};
|
||||
|
||||
if (location === 'navigation') {
|
||||
where.showInNavigation = true;
|
||||
} else if (location === 'footer') {
|
||||
where.showInFooter = true;
|
||||
}
|
||||
|
||||
const orderBy: any = [];
|
||||
if (location === 'navigation') {
|
||||
orderBy.push({ navigationOrder: 'asc' });
|
||||
} else if (location === 'footer') {
|
||||
orderBy.push({ footerOrder: 'asc' });
|
||||
}
|
||||
orderBy.push({ title: 'asc' });
|
||||
|
||||
const pages = await prisma.page.findMany({
|
||||
where,
|
||||
orderBy,
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
slug: true,
|
||||
excerpt: true,
|
||||
showInNavigation: true,
|
||||
showInFooter: true,
|
||||
navigationOrder: true,
|
||||
footerOrder: true
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: pages
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching public pages:', error);
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Failed to fetch pages' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user