diff --git a/src/server/api/routers/lists.ts b/src/server/api/routers/lists.ts index 562e7c3..e76c7f4 100644 --- a/src/server/api/routers/lists.ts +++ b/src/server/api/routers/lists.ts @@ -8,11 +8,6 @@ import { import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc"; import { lists } from "~/server/db/schema"; -const formatList = (list: typeof lists.$inferInsert) => ({ - ...list, - labels: list.labels?.split(",") ?? [], -}); - export const listRouter = createTRPCRouter({ create: protectedProcedure .input(listCreationFormSchema) @@ -30,8 +25,7 @@ export const listRouter = createTRPCRouter({ const allLists = await ctx.db.query.lists.findMany({ where: eq(lists.userId, ctx.session.user.id), }); - const formattedLists = allLists.map(formatList); - return formattedLists; + return allLists; }), get: protectedProcedure .input(z.object({ listId: z.coerce.number() })) @@ -49,8 +43,7 @@ export const listRouter = createTRPCRouter({ return; } - const formattedList = { ...formatList(list), tasks: list.tasks }; - return formattedList; + return list; }), update: protectedProcedure .input(z.object({ listId: z.number(), title: listTitleSchema })) diff --git a/src/server/api/routers/tasks.ts b/src/server/api/routers/tasks.ts index 205f6b7..f7deab7 100644 --- a/src/server/api/routers/tasks.ts +++ b/src/server/api/routers/tasks.ts @@ -1,11 +1,49 @@ +import { eq } from "drizzle-orm"; import { z } from "zod"; import { taskCreationFormSchema } from "~/lib/schemas/task-creation-form"; import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc"; +import { lists, tasks } from "~/server/db/schema"; export const tasksRouter = createTRPCRouter({ create: protectedProcedure .input(taskCreationFormSchema) + .mutation(async ({ ctx, input }) => { + const list = await ctx.db.query.lists.findFirst({ + where: eq(lists.id, input.listId), + }); + + // TODO: Error - no list found + if (!list) { + return; + } + + const taskId = list.lastTaskId + 1; + const visibleId = list.showId ? `${list.idPrefix}${taskId}` : undefined; + + await ctx.db + .insert(tasks) + .values({ listId: input.listId, title: input.title, visibleId }); + + await ctx.db + .update(lists) + .set({ lastTaskId: taskId }) + .where(eq(lists.id, input.listId)); + }), + getAll: protectedProcedure + .input(z.object({ listId: z.number() })) + .query(({ ctx, input }) => + ctx.db.query.tasks.findMany({ + where: eq(lists.id, input.listId), + }), + ), + update: protectedProcedure + .input(taskCreationFormSchema) + .mutation(async ({ ctx, input }) => { + // + }), + delete: protectedProcedure + .input(z.object({ listId: z.number() })) .mutation(async ({ ctx, input }) => { // }),