From 1f1a6a67846ecf60ff2f7e84ffa1cac832379187 Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Thu, 6 Jun 2024 13:28:20 +0200 Subject: [PATCH] commit --- api/v1/games/getHelpers | 30 ++++++++++++++++++++++++++++++ libs/mysql.js | 17 ++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 api/v1/games/getHelpers diff --git a/api/v1/games/getHelpers b/api/v1/games/getHelpers new file mode 100644 index 0000000..e52e603 --- /dev/null +++ b/api/v1/games/getHelpers @@ -0,0 +1,30 @@ +const express = require('express'); +const jwt = require('jsonwebtoken'); + +const {getHelpers} = require("../../../libs/mysql.js") + +const router = express.Router(); + +router.post('/', async (req, res) => { + const {token, gameid} = req.body; + + if (!token) { + return res.status(400).send({error: "invalid token"}); + } + + try { + jwt.verify(token, process.env.JWTSecret); + } catch { + return res.status(400).send({error: "invalid token"}); + } + + const helpers = await getHelpers(gameid) + + if (!helpers[0]) { + return res.status(400).send({error: "this game doesn't exist in the data base"}) + }; + + res.status(200).send(JSON.parse(helpers[0])); +}); + +module.exports = router; \ No newline at end of file diff --git a/libs/mysql.js b/libs/mysql.js index 70cf3cf..1014f47 100644 --- a/libs/mysql.js +++ b/libs/mysql.js @@ -14,7 +14,7 @@ const con = mysql.createConnection({ function getGames() { return new Promise((resolve, reject) => { con.query( - `SELECT * FROM games`, + `SELECT title, subtitle, type, players, duration, ages FROM games`, (error, result) => { if (error) { reject(new Error(error)); @@ -67,6 +67,20 @@ function removeHelper(username, gameid) { }); } +function getHelpers(gameid) { + return new Promise((resolve, reject) => { + con.query( + `SELECT helpers FROM games WHERE id = "${gameid}"`, + (error, result) => { + if (error) { + reject(new Error(error)); + } + resolve(result); + } + ) + }) +} + // +-----------------------------------+ // | AUTH | // +-----------------------------------+ @@ -102,6 +116,7 @@ module.exports = { getGame, addHelper, removeHelper, + getHelpers, getUser, addUser,