fix: tweak reorder queries

This commit is contained in:
Henry
2024-04-01 14:42:16 +01:00
parent 82a3cad3f5
commit 181ae98282
2 changed files with 5 additions and 4 deletions

View File

@@ -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)

View File

@@ -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