From 66662220e7a925dc945b3a7ee7ea1e7775d0193a Mon Sep 17 00:00:00 2001 From: Zeke Abshire Date: Sat, 23 Nov 2024 09:05:24 -0600 Subject: [PATCH] created list schema --- src/app/(home)/_components/list-button.tsx | 6 ++-- src/app/(home)/page.tsx | 4 +-- src/server/api/routers/lists.ts | 38 +++++++++++++--------- src/server/db/schema/app.ts | 38 ++++++++++++++++++++++ src/server/db/schema/index.ts | 1 + 5 files changed, 66 insertions(+), 21 deletions(-) create mode 100644 src/server/db/schema/app.ts diff --git a/src/app/(home)/_components/list-button.tsx b/src/app/(home)/_components/list-button.tsx index 6f97500..b422f8e 100644 --- a/src/app/(home)/_components/list-button.tsx +++ b/src/app/(home)/_components/list-button.tsx @@ -16,7 +16,7 @@ import { api } from "~/trpc/react"; const newName = "test new name"; -export const ListButton = ({ id, name }: { id: number; name: string }) => { +export const ListButton = ({ id, title }: { id: number; title: string }) => { const dialogConfirmation = useDialogConfirmation(); const { mutate: updateMutation } = api.list.update.useMutation({ @@ -30,7 +30,7 @@ export const ListButton = ({ id, name }: { id: number; name: string }) => { }, }); - const handleRename = () => updateMutation({ listId: id, name: newName }); + const handleRename = () => updateMutation({ listId: id, title: newName }); const handleDelete = async () => { const didConfirm = await dialogConfirmation({}); @@ -51,7 +51,7 @@ export const ListButton = ({ id, name }: { id: number; name: string }) => { href={`/list/${id}`} className="w-full text-left underline-offset-4 hover:underline" > - {name} + {title} diff --git a/src/app/(home)/page.tsx b/src/app/(home)/page.tsx index 2abc8ef..66b2d8f 100644 --- a/src/app/(home)/page.tsx +++ b/src/app/(home)/page.tsx @@ -28,8 +28,8 @@ export default async function Home() {
- {allLists.map(({ id, name }) => ( - + {allLists.map(({ id, title }) => ( + ))}
diff --git a/src/server/api/routers/lists.ts b/src/server/api/routers/lists.ts index dc2ae3f..cdd8553 100644 --- a/src/server/api/routers/lists.ts +++ b/src/server/api/routers/lists.ts @@ -1,29 +1,35 @@ +import { and, eq } from "drizzle-orm"; import { z } from "zod"; import { listCreationFormSchema, - listNameSchema, + listTitleSchema, } from "~/lib/schemas/list-creation-form"; import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc"; +import { lists } from "~/server/db/schema"; export const listRouter = createTRPCRouter({ create: protectedProcedure .input(listCreationFormSchema) - .mutation(async ({ ctx, input }) => { - return []; - }), + .mutation(({ ctx, input }) => + ctx.db.insert(lists).values({ + userId: ctx.session.user.id, + title: input.title, + variant: input.variant, + labels: input.labels.join(","), + showId: input.id, + idPrefix: input.idPrefix, + }), + ), getAll: protectedProcedure.query(async ({ ctx }) => { - return [ - { id: 0, name: "Groceries" }, - { id: 1, name: "Christmas" }, - { id: 2, name: "Salt Seeker" }, - { id: 3, name: "Maize Walker" }, - { id: 4, name: "Sympathy for the Machine" }, - { - id: 5, - name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor elit incididunt ut labore et dolore magna aliqua.", - }, - ]; + const allLists = await ctx.db.query.lists.findMany({ + where: and(eq(lists.userId, ctx.session.user.id)), + }); + const formattedLists = allLists.map((list) => ({ + ...list, + labels: list.labels?.split(",") ?? [], + })); + return formattedLists; }), get: protectedProcedure .input(z.object({ listId: z.coerce.number() })) @@ -31,7 +37,7 @@ export const listRouter = createTRPCRouter({ return { name: "Groceries", tasks: [] }; }), update: protectedProcedure - .input(z.object({ listId: z.number(), name: listNameSchema })) + .input(z.object({ listId: z.number(), title: listTitleSchema })) .mutation(async ({ ctx, input }) => { return []; }), diff --git a/src/server/db/schema/app.ts b/src/server/db/schema/app.ts new file mode 100644 index 0000000..0820841 --- /dev/null +++ b/src/server/db/schema/app.ts @@ -0,0 +1,38 @@ +import { relations } from "drizzle-orm"; +import { boolean, integer, serial, text, varchar } from "drizzle-orm/pg-core"; +import { VARIANTS } from "~/lib/data/list-variants"; +import { LABELS } from "~/lib/data/task-labels"; +import { PRIORITIES } from "~/lib/data/task-priority"; +import { STATUSES } from "~/lib/data/task-status"; +import { users } from "~/server/db/schema/auth"; +import { createTable } from "~/server/db/schema/create-table"; + +export const lists = createTable("list", { + id: serial("id").primaryKey(), + userId: varchar("userId", { length: 255 }).notNull(), + title: varchar("title", { length: 128 }).notNull(), + variant: varchar("variant", { enum: VARIANTS }).notNull(), + labels: text("labels"), + showId: boolean("showId").default(false).notNull(), + idPrefix: varchar("idPrefix", { length: 8 }), + lastTaskId: integer("lastTaskId").default(0).notNull(), +}); + +export const listRelations = relations(lists, ({ one }) => ({ + user: one(users, { fields: [lists.userId], references: [users.id] }), +})); + +export const tasks = createTable("task", { + id: serial("id").primaryKey(), + listId: serial("listId").notNull(), + title: varchar("title", { length: 128 }).notNull(), + isChecked: boolean("isChecked").default(false).notNull(), + visibleId: varchar("visibleId", { length: 16 }), + label: varchar("label", { enum: LABELS }), + status: varchar("status", { enum: STATUSES }), + priority: varchar("priority", { enum: PRIORITIES }), +}); + +export const taskRelations = relations(tasks, ({ one }) => ({ + list: one(lists, { fields: [tasks.listId], references: [lists.id] }), +})); diff --git a/src/server/db/schema/index.ts b/src/server/db/schema/index.ts index 97ccf76..cc08e1a 100644 --- a/src/server/db/schema/index.ts +++ b/src/server/db/schema/index.ts @@ -1 +1,2 @@ export * from "./auth"; +export * from "./app";