Corrected list-task relationship
This commit is contained in:
@@ -8,6 +8,11 @@ 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)
|
||||
@@ -23,24 +28,38 @@ export const listRouter = createTRPCRouter({
|
||||
),
|
||||
getAll: protectedProcedure.query(async ({ ctx }) => {
|
||||
const allLists = await ctx.db.query.lists.findMany({
|
||||
where: and(eq(lists.userId, ctx.session.user.id)),
|
||||
where: eq(lists.userId, ctx.session.user.id),
|
||||
});
|
||||
const formattedLists = allLists.map((list) => ({
|
||||
...list,
|
||||
labels: list.labels?.split(",") ?? [],
|
||||
}));
|
||||
const formattedLists = allLists.map(formatList);
|
||||
return formattedLists;
|
||||
}),
|
||||
get: protectedProcedure
|
||||
.input(z.object({ listId: z.coerce.number() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
return { name: "Groceries", tasks: [] };
|
||||
const list = await ctx.db.query.lists.findFirst({
|
||||
where: and(
|
||||
eq(lists.userId, ctx.session.user.id),
|
||||
eq(lists.id, input.listId),
|
||||
),
|
||||
with: { tasks: true },
|
||||
});
|
||||
|
||||
// TODO: Error - no list found
|
||||
if (!list) {
|
||||
return;
|
||||
}
|
||||
|
||||
const formattedList = { ...formatList(list), tasks: list.tasks };
|
||||
return formattedList;
|
||||
}),
|
||||
update: protectedProcedure
|
||||
.input(z.object({ listId: z.number(), title: listTitleSchema }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
return [];
|
||||
}),
|
||||
.mutation(({ ctx, input }) =>
|
||||
ctx.db
|
||||
.update(lists)
|
||||
.set({ title: input.title })
|
||||
.where(eq(lists.id, input.listId)),
|
||||
),
|
||||
delete: protectedProcedure
|
||||
.input(z.object({ listId: z.number() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
|
||||
@@ -18,8 +18,9 @@ export const lists = createTable("list", {
|
||||
lastTaskId: integer("lastTaskId").default(0).notNull(),
|
||||
});
|
||||
|
||||
export const listRelations = relations(lists, ({ one }) => ({
|
||||
export const listRelations = relations(lists, ({ one, many }) => ({
|
||||
user: one(users, { fields: [lists.userId], references: [users.id] }),
|
||||
tasks: many(tasks),
|
||||
}));
|
||||
|
||||
export const tasks = createTable("task", {
|
||||
|
||||
Reference in New Issue
Block a user