Corrected list-task relationship
This commit is contained in:
@@ -8,6 +8,11 @@ import {
|
|||||||
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
|
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
|
||||||
import { lists } from "~/server/db/schema";
|
import { lists } from "~/server/db/schema";
|
||||||
|
|
||||||
|
const formatList = (list: typeof lists.$inferInsert) => ({
|
||||||
|
...list,
|
||||||
|
labels: list.labels?.split(",") ?? [],
|
||||||
|
});
|
||||||
|
|
||||||
export const listRouter = createTRPCRouter({
|
export const listRouter = createTRPCRouter({
|
||||||
create: protectedProcedure
|
create: protectedProcedure
|
||||||
.input(listCreationFormSchema)
|
.input(listCreationFormSchema)
|
||||||
@@ -23,24 +28,38 @@ export const listRouter = createTRPCRouter({
|
|||||||
),
|
),
|
||||||
getAll: protectedProcedure.query(async ({ ctx }) => {
|
getAll: protectedProcedure.query(async ({ ctx }) => {
|
||||||
const allLists = await ctx.db.query.lists.findMany({
|
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) => ({
|
const formattedLists = allLists.map(formatList);
|
||||||
...list,
|
|
||||||
labels: list.labels?.split(",") ?? [],
|
|
||||||
}));
|
|
||||||
return formattedLists;
|
return formattedLists;
|
||||||
}),
|
}),
|
||||||
get: protectedProcedure
|
get: protectedProcedure
|
||||||
.input(z.object({ listId: z.coerce.number() }))
|
.input(z.object({ listId: z.coerce.number() }))
|
||||||
.query(async ({ ctx, input }) => {
|
.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
|
update: protectedProcedure
|
||||||
.input(z.object({ listId: z.number(), title: listTitleSchema }))
|
.input(z.object({ listId: z.number(), title: listTitleSchema }))
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) =>
|
||||||
return [];
|
ctx.db
|
||||||
}),
|
.update(lists)
|
||||||
|
.set({ title: input.title })
|
||||||
|
.where(eq(lists.id, input.listId)),
|
||||||
|
),
|
||||||
delete: protectedProcedure
|
delete: protectedProcedure
|
||||||
.input(z.object({ listId: z.number() }))
|
.input(z.object({ listId: z.number() }))
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
|
|||||||
@@ -18,8 +18,9 @@ export const lists = createTable("list", {
|
|||||||
lastTaskId: integer("lastTaskId").default(0).notNull(),
|
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] }),
|
user: one(users, { fields: [lists.userId], references: [users.id] }),
|
||||||
|
tasks: many(tasks),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
export const tasks = createTable("task", {
|
export const tasks = createTable("task", {
|
||||||
|
|||||||
Reference in New Issue
Block a user