diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts new file mode 100644 index 0000000..64e36a1 --- /dev/null +++ b/apps/api/src/index.ts @@ -0,0 +1,13 @@ +import { Hono } from "hono" + +import { problemRoutes } from "./routes/problem" + +const app = new Hono() + +app.get("/health", (c) => c.json({ ok: true })) +app.route("/api", problemRoutes) + +export default { + port: 3000, + fetch: app.fetch, +} diff --git a/apps/api/src/routes/problem.ts b/apps/api/src/routes/problem.ts new file mode 100644 index 0000000..cfda360 --- /dev/null +++ b/apps/api/src/routes/problem.ts @@ -0,0 +1,25 @@ +import { problemSummarySchema } from "@oj2/contract" +import { desc } from "drizzle-orm" +import { Hono } from "hono" + +import { db, schema } from "../db" + +export const problemRoutes = new Hono() + +problemRoutes.get("/problems", async (c) => { + const rows = await db + .select({ + id: schema.problem.id, + _id: schema.problem.displayId, + title: schema.problem.title, + difficulty: schema.problem.difficulty, + submissionNumber: schema.problem.submissionNumber, + acceptedNumber: schema.problem.acceptedNumber, + }) + .from(schema.problem) + .orderBy(desc(schema.problem.id)) + .limit(20) + + const data = rows.map((row) => problemSummarySchema.parse(row)) + return c.json({ data }) +})