65 lines
1.9 KiB
JavaScript
65 lines
1.9 KiB
JavaScript
const fs = require('fs');
|
|
|
|
const routeTsContent = `import { NextRequest, NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/db';
|
|
import { requireAuth } from '@/lib/auth/session';
|
|
import { scheduleZipRebuild } from '@/lib/jobs/queue';
|
|
|
|
export async function PATCH(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
const authError = await requireAuth(request);
|
|
if (authError) return authError;
|
|
|
|
const id = parseInt(params.id, 10);
|
|
if (isNaN(id)) {
|
|
return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
const body = await request.json();
|
|
const {
|
|
published,
|
|
featured,
|
|
placementMode,
|
|
gridColumn,
|
|
gridRow,
|
|
gridWidth,
|
|
gridHeight,
|
|
sortOrder
|
|
} = body;
|
|
|
|
const currentItem = await prisma.mediaItem.findUnique({ where: { id } });
|
|
if (!currentItem) {
|
|
return NextResponse.json({ error: 'Not found' }, { status: 404 });
|
|
}
|
|
|
|
const updated = await prisma.mediaItem.update({
|
|
where: { id },
|
|
data: {
|
|
...(published !== undefined && { published }),
|
|
...(featured !== undefined && { featured }),
|
|
...(placementMode !== undefined && { placementMode }),
|
|
...(gridColumn !== undefined && { gridColumn }),
|
|
...(gridRow !== undefined && { gridRow }),
|
|
...(gridWidth !== undefined && { gridWidth }),
|
|
...(gridHeight !== undefined && { gridHeight }),
|
|
...(sortOrder !== undefined && { sortOrder }),
|
|
}
|
|
});
|
|
|
|
if (published !== undefined && published !== currentItem.published) {
|
|
await scheduleZipRebuild();
|
|
}
|
|
|
|
return NextResponse.json(JSON.parse(JSON.stringify(updated, (k, v) => typeof v === 'bigint' ? Number(v) : v)));
|
|
} catch (error) {
|
|
console.error('Error updating media item:', error);
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
|
|
}
|
|
}
|
|
`;
|
|
|
|
fs.writeFileSync('src/app/api/admin/media/[id]/route.ts', routeTsContent, 'utf8');
|