Next.js 14 App Router application for managing homophone pun game levels: - Better Auth with Prisma adapter for authentication - MySQL database with Prisma ORM - Level CRUD operations with drag-and-drop reordering - Tencent COS integration for image uploads - shadcn/ui components with Tailwind CSS - TanStack Query for server state management
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { auth } from '@/lib/auth'
|
|
|
|
// PUT /api/levels/reorder - Batch update sort order
|
|
export async function PUT(request: NextRequest) {
|
|
try {
|
|
const session = await auth.api.getSession({
|
|
headers: request.headers,
|
|
})
|
|
|
|
if (!session) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const body = await request.json()
|
|
const { orders } = body as { orders: { id: string; sortOrder: number }[] }
|
|
|
|
if (!Array.isArray(orders)) {
|
|
return NextResponse.json(
|
|
{ error: 'orders must be an array' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Update each level's sort order in a transaction
|
|
await prisma.$transaction(
|
|
orders.map((item) =>
|
|
prisma.level.update({
|
|
where: { id: item.id },
|
|
data: { sortOrder: item.sortOrder },
|
|
})
|
|
)
|
|
)
|
|
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
console.error('Error reordering levels:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to reorder levels' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|