feat: create new label

This commit is contained in:
Henry
2023-12-30 13:44:20 +00:00
parent 0116a1ae2c
commit 0a0bfe9c76
7 changed files with 236 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
import { boardRouter } from "~/server/api/routers/board";
import { cardRouter } from "~/server/api/routers/card";
import { labelRouter } from "~/server/api/routers/label";
import { listRouter } from "~/server/api/routers/list";
import { createTRPCRouter } from "~/server/api/trpc";
@@ -11,6 +12,7 @@ import { createTRPCRouter } from "~/server/api/trpc";
export const appRouter = createTRPCRouter({
board: boardRouter,
card: cardRouter,
label: labelRouter,
list: listRouter,
});

View File

@@ -0,0 +1,54 @@
import { z } from "zod";
import { and, eq, isNull } from "drizzle-orm";
import { cards, cardsToLabels, labels } from "~/server/db/schema";
import { generateUID } from "~/utils/generateUID";
import {
createTRPCRouter,
publicProcedure,
} from "~/server/api/trpc";
export const labelRouter = createTRPCRouter({
create: publicProcedure
.input(
z.object({
name: z.string().min(1).max(36),
cardPublicId: z.string().min(12),
colourCode: z.string().length(7),
}),
)
.mutation(({ ctx, input }) => {
const userId = ctx.session?.user.id;
if (!userId) return;
return ctx.db.transaction(async (tx) => {
const card = await tx.query.cards.findFirst({
where: and(eq(cards.publicId, input.cardPublicId), isNull(cards.deletedAt)),
with: {
list: {
with: {
board: true
}
}
}
});
if (!card) return;
const newLabel = await tx.insert(labels).values({
publicId: generateUID(),
name: input.name,
colourCode: input.colourCode,
createdBy: userId,
boardId: card.list.boardId,
});
return tx.insert(cardsToLabels).values({
cardId: card.id,
labelId: Number(newLabel.insertId),
});
})
}),
});

View File

@@ -79,7 +79,5 @@ export const listRouter = createTRPCRouter({
WHERE ${lists.boardId} = ${list.boardId};
`);
})
})
});