This commit is contained in:
Lukian LEIZOUR 2024-06-02 16:48:15 +02:00
parent bf00593732
commit b00a7c0af0
2 changed files with 61 additions and 0 deletions

36
api/v1/games/addHelper.js Normal file
View file

@ -0,0 +1,36 @@
const express = require('express');
const jwt = require('jsonwebtoken');
const {addHelper, getGame} = 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 {
const user = jwt.verify(token, process.env.JWTSecret);
const game = await getGame(gameid);
if (!game[0]) {
return res.status(400).send({error: "this game doesn't exist"});
}
if (JSON.parse(game[0].helpers).includes(user.user.id)) {
return res.status(400).send({error: "you are already an helper for this game"});
}
await addHelper(user.user.id, gameid);
} catch {
return res.status(400).send({error: "invalid token"});
}
res.status(200).send({message: "success"});
});
module.exports = router;

View file

@ -0,0 +1,25 @@
const express = require('express');
const jwt = require('jsonwebtoken');
const {removeHelper} = 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 {
const user = jwt.verify(token, process.env.JWTSecret);
await removeHelper(user.user.id, gameid);
} catch {
return res.status(400).send({error: "invalid token"});
}
res.status(200).send({message: "success"});
});
module.exports = router;