feat: update card on drop

This commit is contained in:
Henry
2023-11-13 23:39:29 +00:00
parent 7f2ca58679
commit 2604484473
3 changed files with 81 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
import { boardRouter } from "~/server/api/routers/board";
import { cardRouter } from "~/server/api/routers/card";
import { createTRPCRouter } from "~/server/api/trpc";
/**
@@ -8,6 +9,7 @@ import { createTRPCRouter } from "~/server/api/trpc";
*/
export const appRouter = createTRPCRouter({
board: boardRouter,
card: cardRouter
});
// export type definition of API

View File

@@ -0,0 +1,43 @@
import { z } from "zod";
import { and, eq, gte, or} from "drizzle-orm";
import { cards, lists } from "~/server/db/schema";
import {
createTRPCRouter,
publicProcedure,
} from "~/server/api/trpc";
export const cardRouter = createTRPCRouter({
update: publicProcedure
.input(
z.object({
cardId: z.string().min(12),
currentListId: z.string().min(12),
newListId: z.string().min(12),
currentIndex: z.number(),
newIndex: z.number()
}))
.mutation(({ ctx, input }) => {
const userId = ctx.session?.user.id;
if (!userId) return;
return ctx.db.transaction(async (tx) => {
const [currentList, newList] = await tx.select({ id: lists.id }).from(lists).where(or(eq(lists.publicId, input.currentListId), eq(lists.publicId, input.newListId)))
if (!currentList?.id || !newList?.id) return;
await tx
.update(cards)
.set({ index: input.newIndex + 1 })
.where(and(eq(cards.listId, newList.id), gte(cards.index, input.newIndex)))
await tx
.update(cards)
.set({ listId: newList.id, index: input.newIndex })
.where(eq(cards.publicId, input.cardId))
})
}),
});