diff --git a/apps/api/src/app.ts b/apps/api/src/app.ts index 0f901cf..5a8122a 100644 --- a/apps/api/src/app.ts +++ b/apps/api/src/app.ts @@ -1,12 +1,11 @@ import express from "express"; import type { Express } from "express"; +import { apiRouter } from "./routes/apiRouter.js"; export function createApp() { const app: Express = express(); - app.get("/api/health", (_req, res) => { - res.json({ status: "ok" }); - }); + app.use("/api/v1", apiRouter); return app; } diff --git a/apps/api/src/controllers/gameController.ts b/apps/api/src/controllers/gameController.ts new file mode 100644 index 0000000..ffffbee --- /dev/null +++ b/apps/api/src/controllers/gameController.ts @@ -0,0 +1,8 @@ +import type { Request, Response } from "express"; +import { prepareQuestions } from "../services/gameService.js"; + +export const getGame = async (req: Request, res: Response) => { + const query = gameRequestSchema.parse(req.query); + const questions = await prepareQuestions(query); + res.json({ success: true, data: questions }); +}; diff --git a/apps/api/src/routes/apiRouter.ts b/apps/api/src/routes/apiRouter.ts new file mode 100644 index 0000000..6ad84eb --- /dev/null +++ b/apps/api/src/routes/apiRouter.ts @@ -0,0 +1,9 @@ +import express from "express"; +import { Router } from "express"; +import { healthRouter } from "./healthRouter.js"; +import { gameRouter } from "./gameRouter.js"; + +export const apiRouter: Router = express.Router(); + +apiRouter.use("/health", healthRouter); +apiRouter.use("/game", gameRouter); diff --git a/apps/api/src/routes/gameRouter.ts b/apps/api/src/routes/gameRouter.ts new file mode 100644 index 0000000..7196021 --- /dev/null +++ b/apps/api/src/routes/gameRouter.ts @@ -0,0 +1,7 @@ +import express from "express"; +import type { Router } from "express"; +import { createGame } from "../controllers/gameController.js"; + +export const gameRouter: Router = express.Router(); + +gameRouter.get("/", createGame); diff --git a/apps/api/src/routes/healthRouter.ts b/apps/api/src/routes/healthRouter.ts new file mode 100644 index 0000000..8812a45 --- /dev/null +++ b/apps/api/src/routes/healthRouter.ts @@ -0,0 +1,11 @@ +import type { Request, Response } from "express"; + +export const healthRouter = (_req: Request, res: Response) => { + res + .status(200) + .json({ + status: "ok", + uptime: process.uptime(), + timestamp: new Date().toISOString(), + }); +}; diff --git a/apps/api/src/services/gameService.ts b/apps/api/src/services/gameService.ts new file mode 100644 index 0000000..f4c79d1 --- /dev/null +++ b/apps/api/src/services/gameService.ts @@ -0,0 +1,3 @@ +export const prepareQuestions = async (params: object) => { + console.log(params); +}; diff --git a/packages/shared/src/constants.ts b/packages/shared/src/constants.ts index e868bbb..26b5467 100644 --- a/packages/shared/src/constants.ts +++ b/packages/shared/src/constants.ts @@ -1,3 +1,5 @@ export const SUPPORTED_LANGUAGE_CODES = ["en", "it"] as const; export const SUPPORTED_POS = ["noun"] as const; + +export const GAME_ROUNDS = ["3", "10"] as const; diff --git a/packages/shared/src/schema.ts b/packages/shared/src/schema.ts index ef9f446..aeb8a56 100644 --- a/packages/shared/src/schema.ts +++ b/packages/shared/src/schema.ts @@ -1,5 +1,11 @@ import * as z from "zod"; +import { + SUPPORTED_LANGUAGE_CODES, + SUPPORTED_POS, + GAME_ROUNDS, +} from "./constants.js"; + export const Term = z.object({ id: z.uuid(), synset_id: z.string(), @@ -70,3 +76,16 @@ export const DeckTerm = z.object({ }); export type DeckTerm = z.infer; + +export const GameRequestSchema = z + .object({ + source_language: z.enum(SUPPORTED_LANGUAGE_CODES), + target_language: z.enum(SUPPORTED_LANGUAGE_CODES), + pos: z.enum(SUPPORTED_POS), + rounds: z.enum(GAME_ROUNDS).transform(Number), + }) + .refine((game) => game.target_language !== game.source_language, { + error: "source and target language must be different", + }); + +export type GameRequestSchema = z.infer;