refactor routes everywhere
This commit is contained in:
parent
d8d639510c
commit
a53cc57b24
6
backend/database/package-lock.json
generated
6
backend/database/package-lock.json
generated
@ -12,7 +12,6 @@
|
|||||||
"better-sqlite3": "^9.5.0",
|
"better-sqlite3": "^9.5.0",
|
||||||
"cross-env": "^7.0.3",
|
"cross-env": "^7.0.3",
|
||||||
"drizzle-orm": "^0.30.8",
|
"drizzle-orm": "^0.30.8",
|
||||||
"itty-router": "^5.0.15",
|
|
||||||
"itty-router-extras": "^0.4.6",
|
"itty-router-extras": "^0.4.6",
|
||||||
"zod": "^3.22.4"
|
"zod": "^3.22.4"
|
||||||
},
|
},
|
||||||
@ -2936,11 +2935,6 @@
|
|||||||
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
|
||||||
"integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw=="
|
"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": {
|
"node_modules/itty-router-extras": {
|
||||||
"version": "0.4.6",
|
"version": "0.4.6",
|
||||||
"resolved": "https://registry.npmjs.org/itty-router-extras/-/itty-router-extras-0.4.6.tgz",
|
"resolved": "https://registry.npmjs.org/itty-router-extras/-/itty-router-extras-0.4.6.tgz",
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
"better-sqlite3": "^9.5.0",
|
"better-sqlite3": "^9.5.0",
|
||||||
"cross-env": "^7.0.3",
|
"cross-env": "^7.0.3",
|
||||||
"drizzle-orm": "^0.30.8",
|
"drizzle-orm": "^0.30.8",
|
||||||
"itty-router": "^5.0.15",
|
|
||||||
"itty-router-extras": "^0.4.6",
|
"itty-router-extras": "^0.4.6",
|
||||||
"zod": "^3.22.4"
|
"zod": "^3.22.4"
|
||||||
}
|
}
|
||||||
|
@ -12,45 +12,25 @@ export interface Env {
|
|||||||
DB: D1Database;
|
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<Request>({ 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 {
|
export default {
|
||||||
fetch: router.handle,
|
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
|
||||||
|
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 });
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
34
frontend/app/(app)/layout.tsx
Normal file
34
frontend/app/(app)/layout.tsx
Normal file
@ -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}</>
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user