From 2690a50efb41a48a34d9a860a7257cf178965cb9 Mon Sep 17 00:00:00 2001 From: carlospelazas Date: Tue, 30 Apr 2024 08:17:38 +0200 Subject: [PATCH] add server validation --- game/gameservice/GameController.js | 4 ++++ game/gameservice/queries/CreateGame.js | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/game/gameservice/GameController.js b/game/gameservice/GameController.js index cd6746e8..dfce9b6a 100644 --- a/game/gameservice/GameController.js +++ b/game/gameservice/GameController.js @@ -10,10 +10,14 @@ let GameController = { res.json(response); },*/ create: async (req, res) => { + try{ const { questions, players } = req.body; console.log(questions, players) const game = await createGame(questions, players); res.json(game); + } catch(error){ + res.status(500).json({ message: error.message }); + } }, delete: async (req, res) => { await Game.findOneAndDelete({uuid: req.params.id}); diff --git a/game/gameservice/queries/CreateGame.js b/game/gameservice/queries/CreateGame.js index 044a6d3b..b1712e93 100644 --- a/game/gameservice/queries/CreateGame.js +++ b/game/gameservice/queries/CreateGame.js @@ -6,6 +6,12 @@ async function createGame(questions, players) { try { // Create a new Game instance console.log(players) + if(players.length == 0){ + throw new Error('No players found') + } + if(players[0].uuid == null || players[0].uuid == undefined){ + throw new Error('No players found') + } const game = new Game({ uuid: uuid.v4(), players: players.map(user => user.uuid),