update game endpoints

for getting all public games and finding specific games with code
This commit is contained in:
Nathaniel Tampus
2023-03-04 18:36:43 +08:00
parent 572eb22fc6
commit 6dc9f1a18a
2 changed files with 23 additions and 24 deletions

View File

@@ -3,26 +3,32 @@ import { activeGames } from "../db/models/game.model.js";
import type { Game, User } from "@chessu/types"; import type { Game, User } from "@chessu/types";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
export const getActiveGames = async (req: Request, res: Response) => { export const getActivePublicGames = async (req: Request, res: Response) => {
try { try {
//if (!req.query || !req.query.code) {
res.status(200).json(activeGames.filter((g) => !g.unlisted)); res.status(200).json(activeGames.filter((g) => !g.unlisted));
//} } catch (err: unknown) {
console.log(err);
res.status(500).end();
}
};
// const code = export const getActiveGame = async (req: Request, res: Response) => {
// (req.query.code as string).startsWith("http") || try {
// (req.query.code as string).startsWith("ches.su") if (!req.params || !req.params.code) {
// ? path.posix.basename(url.parse(req.query.code as string).pathname as string) res.status(400).end();
// : req.query.code; return;
}
// console.log(code); const code = req.params.code;
// const game = activeGames.find((g) => g.code === code);
// if (!game) { console.log(code);
// res.status(404).end(); const game = activeGames.find((g) => g.code === code);
// } else {
// res.status(200).json(game); if (!game) {
// } res.status(404).end();
} else {
res.status(200).json(game);
}
} catch (err: unknown) { } catch (err: unknown) {
console.log(err); console.log(err);
res.status(500).end(); res.status(500).end();
@@ -64,10 +70,3 @@ export const createGame = async (req: Request, res: Response) => {
res.status(500).end(); res.status(500).end();
} }
}; };
// use sockets for joining games
/*
export const joinGame = async (req: Request, res: Response) => {
console.log("joining game!");
};
*/

View File

@@ -3,9 +3,9 @@ import * as controller from "../controllers/games.controller.js";
const router = Router(); 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 // todo: api for updating games/moves requiring authentication