Files
wikiplus/app/api/article/[slug]/save/route.ts
T
2026-01-24 16:52:19 -06:00

59 lines
1.4 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/db';
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ slug: string }> }
) {
const { slug } = await params;
const body = await request.json();
const { newContent, reason } = body;
if (!newContent || typeof newContent !== 'string') {
return NextResponse.json({ error: 'Missing newContent' }, { status: 400 });
}
if (!reason || typeof reason !== 'string') {
return NextResponse.json({ error: 'Missing reason' }, { status: 400 });
}
// Fetch current article
const article = await prisma.article.findUnique({
where: { slug },
});
if (!article) {
return NextResponse.json({ error: 'Article not found' }, { status: 404 });
}
// Transaction: create history entry and update article
const result = await prisma.$transaction(async (tx) => {
// Create history entry
const historyEntry = await tx.articleHistory.create({
data: {
articleId: article.id,
oldContent: article.content,
newContent: newContent,
reason: reason,
},
});
// Update article content
await tx.article.update({
where: { id: article.id },
data: {
content: newContent,
updatedAt: new Date(),
},
});
return historyEntry;
});
return NextResponse.json({
success: true,
historyId: result.id,
});
}