51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
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 }) => {
|
|
//
|
|
}),
|
|
});
|