From 181ae982820bb2d74fed2c60fc6e86524432d335 Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 1 Apr 2024 14:42:16 +0100 Subject: [PATCH] fix: tweak reorder queries --- src/server/api/routers/card.ts | 7 ++++--- src/server/api/routers/list.ts | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/server/api/routers/card.ts b/src/server/api/routers/card.ts index ecb01386..24d584bb 100644 --- a/src/server/api/routers/card.ts +++ b/src/server/api/routers/card.ts @@ -330,10 +330,11 @@ export const cardRouter = createTRPCRouter({ if (!currentList?.id || !newList?.id) return; + if (currentList.id === newList.id) { await tx.execute(sql` UPDATE ${cards} - SET ${cards.index} = + SET index = CASE WHEN ${cards.index} = ${currentIndex} THEN ${newIndex} WHEN ${currentIndex} < ${newIndex} AND ${cards.index} > ${currentIndex} AND ${cards.index} <= ${newIndex} THEN ${cards.index} - 1 @@ -343,9 +344,9 @@ export const cardRouter = createTRPCRouter({ WHERE ${cards.listId} = ${currentList.id} AND ${cards.deletedAt} IS NULL; `); } else { - await tx.execute(sql`UPDATE ${cards} SET ${cards.index} = ${cards.index} + 1 WHERE ${cards.listId} = ${newList.id} AND ${cards.index} >= ${newIndex} AND ${cards.deletedAt} IS NULL;`) + await tx.execute(sql`UPDATE ${cards} SET index = index + 1 WHERE ${cards.listId} = ${newList.id} AND ${cards.index} >= ${newIndex} AND ${cards.deletedAt} IS NULL;`) - await tx.execute(sql`UPDATE ${cards} SET ${cards.index} = ${cards.index} - 1 WHERE ${cards.listId} = ${currentList.id} AND ${cards.index} >= ${currentIndex} AND ${cards.deletedAt} IS NULL;`) + await tx.execute(sql`UPDATE ${cards} SET index = index - 1 WHERE ${cards.listId} = ${currentList.id} AND ${cards.index} >= ${currentIndex} AND ${cards.deletedAt} IS NULL;`) await tx .update(cards) diff --git a/src/server/api/routers/list.ts b/src/server/api/routers/list.ts index c28b0c1d..72f493f4 100644 --- a/src/server/api/routers/list.ts +++ b/src/server/api/routers/list.ts @@ -69,7 +69,7 @@ export const listRouter = createTRPCRouter({ await tx.execute(sql` UPDATE ${lists} - SET ${lists.index} = + SET index = CASE WHEN ${lists.index} = ${input.currentIndex} AND ${lists.id} = ${list.id} THEN ${input.newIndex} WHEN ${input.currentIndex} < ${input.newIndex} AND ${lists.index} > ${input.currentIndex} AND ${lists.index} <= ${input.newIndex} THEN ${lists.index} - 1