drag reorder boards in admin, position respected on public site

This commit is contained in:
2026-03-22 07:55:08 +02:00
parent 393001c07c
commit a530ce67b0
4 changed files with 61 additions and 8 deletions

View File

@@ -44,7 +44,7 @@ export default async function adminBoardRoutes(app: FastifyInstance) {
{ preHandler: [app.requireAdmin, app.requireRole("SUPER_ADMIN", "ADMIN")], config: { rateLimit: { max: 60, timeWindow: "1 minute" } } },
async (_req, reply) => {
const boards = await prisma.board.findMany({
orderBy: { createdAt: "asc" },
orderBy: [{ position: "asc" }, { createdAt: "asc" }],
include: {
_count: { select: { posts: true } },
},
@@ -128,4 +128,17 @@ export default async function adminBoardRoutes(app: FastifyInstance) {
reply.status(204).send();
}
);
// reorder boards
app.put(
"/admin/boards/reorder",
{ preHandler: [app.requireAdmin, app.requireRole("SUPER_ADMIN", "ADMIN")], config: { rateLimit: { max: 10, timeWindow: "1 minute" } } },
async (req, reply) => {
const body = z.object({ boardIds: z.array(z.string().min(1)).min(1) }).parse(req.body);
await prisma.$transaction(
body.boardIds.map((id, i) => prisma.board.update({ where: { id }, data: { position: i } }))
);
reply.send({ ok: true });
}
);
}

View File

@@ -55,7 +55,7 @@ export default async function boardRoutes(app: FastifyInstance) {
include: {
_count: { select: { posts: true } },
},
orderBy: { createdAt: "asc" },
orderBy: [{ position: "asc" }, { createdAt: "asc" }],
});
// use aggregation queries instead of loading all posts into memory