diff --git a/server/src/controllers/games.controller.ts b/server/src/controllers/games.controller.ts index 9d34288..b888ca2 100644 --- a/server/src/controllers/games.controller.ts +++ b/server/src/controllers/games.controller.ts @@ -3,26 +3,32 @@ import { activeGames } from "../db/models/game.model.js"; import type { Game, User } from "@chessu/types"; import { nanoid } from "nanoid"; -export const getActiveGames = async (req: Request, res: Response) => { +export const getActivePublicGames = async (req: Request, res: Response) => { try { - //if (!req.query || !req.query.code) { res.status(200).json(activeGames.filter((g) => !g.unlisted)); - //} + } catch (err: unknown) { + console.log(err); + res.status(500).end(); + } +}; - // const code = - // (req.query.code as string).startsWith("http") || - // (req.query.code as string).startsWith("ches.su") - // ? path.posix.basename(url.parse(req.query.code as string).pathname as string) - // : req.query.code; +export const getActiveGame = async (req: Request, res: Response) => { + try { + if (!req.params || !req.params.code) { + res.status(400).end(); + return; + } - // console.log(code); - // const game = activeGames.find((g) => g.code === code); + const code = req.params.code; - // if (!game) { - // res.status(404).end(); - // } else { - // res.status(200).json(game); - // } + console.log(code); + const game = activeGames.find((g) => g.code === code); + + if (!game) { + res.status(404).end(); + } else { + res.status(200).json(game); + } } catch (err: unknown) { console.log(err); res.status(500).end(); @@ -64,10 +70,3 @@ export const createGame = async (req: Request, res: Response) => { res.status(500).end(); } }; - -// use sockets for joining games -/* -export const joinGame = async (req: Request, res: Response) => { - console.log("joining game!"); -}; -*/ diff --git a/server/src/routes/games.route.ts b/server/src/routes/games.route.ts index a912f8c..03036eb 100644 --- a/server/src/routes/games.route.ts +++ b/server/src/routes/games.route.ts @@ -3,9 +3,9 @@ import * as controller from "../controllers/games.controller.js"; const router = Router(); -router.route("/").get(controller.getActiveGames).post(controller.createGame); +router.route("/").get(controller.getActivePublicGames).post(controller.createGame); -//router.route("/:id").put(controller.joinGame); +router.route("/:code").get(controller.getActiveGame); // todo: api for updating games/moves requiring authentication