feat: add card page

This commit is contained in:
Henry
2023-11-27 23:27:44 +00:00
parent 3d4df5f7e5
commit 1860db7a9c
10 changed files with 728 additions and 110 deletions

View File

@@ -51,48 +51,68 @@ 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;
byId: publicProcedure
.input(z.object({ id: z.string().min(12) }))
.query(({ ctx, input }) =>
ctx.db.query.cards.findFirst({
where: eq(cards.publicId, input.id),
})
),
updateDescription: publicProcedure
.input(
z.object({
cardId: z.string(),
description: z.string(),
}))
.mutation(({ ctx, input }) => {
const userId = ctx.session?.user.id;
if (!userId) return;
if (!userId) return;
return ctx.db.transaction(async (tx) => {
const [currentList] = await tx.select({ id: lists.id }).from(lists).where(eq(lists.publicId, input.currentListId))
const [newList] = await tx.select({ id: lists.id }).from(lists).where(eq(lists.publicId, input.newListId))
return ctx.db.update(cards).set({ description: input.description}).where(eq(cards.publicId, input.cardId));
}),
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 (!currentList?.id || !newList?.id) return;
if (!userId) return;
if (currentList.id === newList.id) {
await tx.execute(sql`
UPDATE ${cards}
SET ${cards.index} =
CASE
WHEN ${cards.index} = ${input.currentIndex} THEN ${input.newIndex}
WHEN ${input.currentIndex} < ${input.newIndex} AND ${cards.index} > ${input.currentIndex} AND ${cards.index} <= ${input.newIndex} THEN ${cards.index} - 1
WHEN ${input.currentIndex} > ${input.newIndex} AND ${cards.index} >= ${input.newIndex} AND ${cards.index} < ${input.currentIndex} THEN ${cards.index} + 1
ELSE ${cards.index}
END
WHERE ${cards.listId} = ${currentList.id};
`);
} else {
await tx.execute(sql`UPDATE ${cards} SET ${cards.index} = ${cards.index} + 1 WHERE ${cards.listId} = ${newList.id} AND ${cards.index} >= ${input.newIndex};`)
return ctx.db.transaction(async (tx) => {
const [currentList] = await tx.select({ id: lists.id }).from(lists).where(eq(lists.publicId, input.currentListId))
const [newList] = await tx.select({ id: lists.id }).from(lists).where(eq(lists.publicId, input.newListId))
await tx.execute(sql`UPDATE ${cards} SET ${cards.index} = ${cards.index} - 1 WHERE ${cards.listId} = ${currentList.id} AND ${cards.index} >= ${input.currentIndex};`)
if (!currentList?.id || !newList?.id) return;
await tx
.update(cards)
.set({ listId: newList.id, index: input.newIndex })
.where(eq(cards.publicId, input.cardId));
}
})
})
if (currentList.id === newList.id) {
await tx.execute(sql`
UPDATE ${cards}
SET ${cards.index} =
CASE
WHEN ${cards.index} = ${input.currentIndex} THEN ${input.newIndex}
WHEN ${input.currentIndex} < ${input.newIndex} AND ${cards.index} > ${input.currentIndex} AND ${cards.index} <= ${input.newIndex} THEN ${cards.index} - 1
WHEN ${input.currentIndex} > ${input.newIndex} AND ${cards.index} >= ${input.newIndex} AND ${cards.index} < ${input.currentIndex} THEN ${cards.index} + 1
ELSE ${cards.index}
END
WHERE ${cards.listId} = ${currentList.id};
`);
} else {
await tx.execute(sql`UPDATE ${cards} SET ${cards.index} = ${cards.index} + 1 WHERE ${cards.listId} = ${newList.id} AND ${cards.index} >= ${input.newIndex};`)
await tx.execute(sql`UPDATE ${cards} SET ${cards.index} = ${cards.index} - 1 WHERE ${cards.listId} = ${currentList.id} AND ${cards.index} >= ${input.currentIndex};`)
await tx
.update(cards)
.set({ listId: newList.id, index: input.newIndex })
.where(eq(cards.publicId, input.cardId));
}
})
})
});