feat: create new label
This commit is contained in:
@@ -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,
|
||||
});
|
||||
|
||||
|
||||
54
src/server/api/routers/label.ts
Normal file
54
src/server/api/routers/label.ts
Normal 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),
|
||||
});
|
||||
})
|
||||
}),
|
||||
});
|
||||
@@ -79,7 +79,5 @@ export const listRouter = createTRPCRouter({
|
||||
WHERE ${lists.boardId} = ${list.boardId};
|
||||
`);
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
});
|
||||
Reference in New Issue
Block a user