33 lines
950 B
TypeScript
33 lines
950 B
TypeScript
import { NextResponse } from 'next/server';
|
|
import db from '@/lib/db';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
// Body: { counters?: { id: number; order_index: number; group_id: number | null }[], groups?: { id: number; order_index: number }[] }
|
|
export async function PUT(request: Request) {
|
|
const { counters, groups } = await request.json();
|
|
|
|
const updateCounters = db.prepare(
|
|
'UPDATE counters SET order_index = ?, group_id = ? WHERE id = ?'
|
|
);
|
|
const updateGroups = db.prepare(
|
|
'UPDATE groups SET order_index = ? WHERE id = ?'
|
|
);
|
|
|
|
const reorderAll = db.transaction(() => {
|
|
if (Array.isArray(counters)) {
|
|
for (const c of counters) {
|
|
updateCounters.run(c.order_index, c.group_id ?? null, c.id);
|
|
}
|
|
}
|
|
if (Array.isArray(groups)) {
|
|
for (const g of groups) {
|
|
updateGroups.run(g.order_index, g.id);
|
|
}
|
|
}
|
|
});
|
|
|
|
reorderAll();
|
|
return new NextResponse(null, { status: 204 });
|
|
}
|