commit
This commit is contained in:
parent
2aa4016a0a
commit
1f1a6a6784
2 changed files with 46 additions and 1 deletions
30
api/v1/games/getHelpers
Normal file
30
api/v1/games/getHelpers
Normal file
|
@ -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;
|
|
@ -14,7 +14,7 @@ const con = mysql.createConnection({
|
||||||
function getGames() {
|
function getGames() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
con.query(
|
con.query(
|
||||||
`SELECT * FROM games`,
|
`SELECT title, subtitle, type, players, duration, ages FROM games`,
|
||||||
(error, result) => {
|
(error, result) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(new Error(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 |
|
// | AUTH |
|
||||||
// +-----------------------------------+
|
// +-----------------------------------+
|
||||||
|
@ -102,6 +116,7 @@ module.exports = {
|
||||||
getGame,
|
getGame,
|
||||||
addHelper,
|
addHelper,
|
||||||
removeHelper,
|
removeHelper,
|
||||||
|
getHelpers,
|
||||||
|
|
||||||
getUser,
|
getUser,
|
||||||
addUser,
|
addUser,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue