Restore accidentally deleted media route API file
This commit is contained in:
1 parent
c6878c18d9
commit
d138a5bdd0
2 files changed
+60
-2
No files matched your search
@@ -1 +1,59 @@
|
||||
|
||||
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 });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user