From a53cc57b24f8fe023f14fc2040a43ec7208fb23e Mon Sep 17 00:00:00 2001 From: Ishaan Dey Date: Wed, 17 Apr 2024 21:24:57 -0400 Subject: [PATCH] refactor routes everywhere --- backend/database/package-lock.json | 6 --- backend/database/package.json | 1 - backend/database/src/index.ts | 60 +++++++-------------- frontend/app/{ => (app)}/code/[id]/page.tsx | 0 frontend/app/{ => (app)}/code/page.tsx | 0 frontend/app/{ => (app)}/dashboard/page.tsx | 0 frontend/app/(app)/layout.tsx | 34 ++++++++++++ 7 files changed, 54 insertions(+), 47 deletions(-) rename frontend/app/{ => (app)}/code/[id]/page.tsx (100%) rename frontend/app/{ => (app)}/code/page.tsx (100%) rename frontend/app/{ => (app)}/dashboard/page.tsx (100%) create mode 100644 frontend/app/(app)/layout.tsx diff --git a/backend/database/package-lock.json b/backend/database/package-lock.json index aece35d..35c888b 100644 --- a/backend/database/package-lock.json +++ b/backend/database/package-lock.json @@ -12,7 +12,6 @@ "better-sqlite3": "^9.5.0", "cross-env": "^7.0.3", "drizzle-orm": "^0.30.8", - "itty-router": "^5.0.15", "itty-router-extras": "^0.4.6", "zod": "^3.22.4" }, @@ -2936,11 +2935,6 @@ "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==" }, - "node_modules/itty-router": { - "version": "5.0.15", - "resolved": "https://registry.npmjs.org/itty-router/-/itty-router-5.0.15.tgz", - "integrity": "sha512-TGpdnAYdzfUWROKBGDXi2d44taRv1j2B1QG0Yt3NeUhwp8QkfdskF2VfZnFHgUMvYx0YpN99ymACq2mVJbLOIg==" - }, "node_modules/itty-router-extras": { "version": "0.4.6", "resolved": "https://registry.npmjs.org/itty-router-extras/-/itty-router-extras-0.4.6.tgz", diff --git a/backend/database/package.json b/backend/database/package.json index 4cab4b6..d102689 100644 --- a/backend/database/package.json +++ b/backend/database/package.json @@ -25,7 +25,6 @@ "better-sqlite3": "^9.5.0", "cross-env": "^7.0.3", "drizzle-orm": "^0.30.8", - "itty-router": "^5.0.15", "itty-router-extras": "^0.4.6", "zod": "^3.22.4" } diff --git a/backend/database/src/index.ts b/backend/database/src/index.ts index 33fa4bc..42ee34d 100644 --- a/backend/database/src/index.ts +++ b/backend/database/src/index.ts @@ -12,45 +12,25 @@ export interface Env { DB: D1Database; } -interface Request extends IRequest { - db: DrizzleD1Database; -} - -interface Methods { - get: Route; - post: Route; -} - -async function injectDB(request: Request, env: Env) { - const db = drizzle(env.DB); - request.db = db; -} - -const router = Router({ base: "/" }); - -router.get("/user", injectDB, async (req: Request, env: Env) => { - const res = await req.db.select().from(user).all(); - return json(res); -}); - -router.get("/user/:id", injectDB, async (req: Request, env: Env) => { - const res = await req.db.select().from(user).where(eq(user.id, req.params!["id"])).get(); - return json(res ?? {}); -}); - -router.post("/user", injectDB, async (req: Request, env: Env) => { - const userSchema = z.object({ - name: z.string(), - email: z.string().email(), - }); - - const reqJSON = await req.json!(); - const { name, email } = userSchema.parse(reqJSON); - - const res = await req.db.insert(user).values({ name, email }).returning().get(); - return json({ res }); -}); - export default { - fetch: router.handle, + async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise { + const url = new URL(request.url); + const path = url.pathname; + const method = request.method; + + const db = drizzle(env.DB); + + if (path === "/api/user" && method === "GET") { + const params = url.searchParams; + + if (params.has("id")) { + const id = params.get("id") as string; + const res = await db.select().from(user).where(eq(user.id, id)).get(); + return json(res ?? {}); + } else { + const res = await db.select().from(user).all(); + return new Response(JSON.stringify(res)); + } + } else return new Response("Not Found", { status: 404 }); + }, }; diff --git a/frontend/app/code/[id]/page.tsx b/frontend/app/(app)/code/[id]/page.tsx similarity index 100% rename from frontend/app/code/[id]/page.tsx rename to frontend/app/(app)/code/[id]/page.tsx diff --git a/frontend/app/code/page.tsx b/frontend/app/(app)/code/page.tsx similarity index 100% rename from frontend/app/code/page.tsx rename to frontend/app/(app)/code/page.tsx diff --git a/frontend/app/dashboard/page.tsx b/frontend/app/(app)/dashboard/page.tsx similarity index 100% rename from frontend/app/dashboard/page.tsx rename to frontend/app/(app)/dashboard/page.tsx diff --git a/frontend/app/(app)/layout.tsx b/frontend/app/(app)/layout.tsx new file mode 100644 index 0000000..344958d --- /dev/null +++ b/frontend/app/(app)/layout.tsx @@ -0,0 +1,34 @@ +import { currentUser } from "@clerk/nextjs" +import { redirect } from "next/navigation" + +export default async function AppAuthLayout({ + children, +}: { + children: React.ReactNode +}) { + const user = await currentUser() + + if (!user) { + redirect("/") + } + + const dbUser = await fetch(`http://localhost:8787/user?id=${user.id}`) + // const dbUserJSON = await dbUser.json() + + console.log(dbUser) + + // if (!dbUserJSON) { + // const res = await fetch("http://localhost:8787/user", { + // method: "POST", + // headers: { + // "Content-Type": "application/json", + // }, + // body: JSON.stringify({ + // id: user.id, + // email: user.emailAddresses[0].emailAddress, + // }), + // }) + // } + + return <>{children} +}