code structure improvement

This commit is contained in:
Lukian 2024-09-16 17:21:57 +02:00
parent 3a5ae2dd7d
commit 5e8ada02f3
12 changed files with 68 additions and 60 deletions

View file

@ -1,7 +1,7 @@
const express = require('express');
const jwt = require('jsonwebtoken');
const {getUnverifiedUsers} = require("../../../libs/mysql.js")
const { getConnection, getUnverifiedUsers } = require("../../../libs/mysql.js")
const router = express.Router();
@ -26,7 +26,9 @@ router.post('/', async (req, res) => {
return res.status(400).send({error: "invalid token"});
}
const users = await getUnverifiedUsers();
const connection = await getConnection();
const users = await getUnverifiedUsers(connection);
connection.end();
res.status(200).send(users);
});

View file

@ -1,7 +1,7 @@
const express = require('express');
const jwt = require('jsonwebtoken');
const {setVerified, getUser} = require("../../../libs/mysql.js")
const { getConnection, setVerified, getUser } = require("../../../libs/mysql.js")
const router = express.Router();
@ -26,7 +26,9 @@ router.post('/', async (req, res) => {
return res.status(400).send({error: "invalid token"});
}
const user = await getUser(username);
const connection = await getConnection();
const user = await getUser(connection, username);
if (!user[0]) {
return res.status(400).send({error: "invalid userid"});
@ -37,6 +39,7 @@ router.post('/', async (req, res) => {
}
await setVerified(username);
connection.end()
res.status(200).send({message: "user verified"});
});

View file

@ -2,14 +2,16 @@ const express = require('express');
const jwt = require('jsonwebtoken');
const sha256 = require("sha256");
const { getUser } = require("../../../libs/mysql");
const { getConnection, getUser } = require("../../../libs/mysql");
const router = express.Router();
router.post('/', async (req, res) => {
const {username, password} = req.body;
const user = await getUser(username);
const connection = await getConnection();
const user = await getUser(connection, username);
if (!user[0]) {
return res.status(400).send({error: "wrong login informations"});

View file

@ -1,7 +1,7 @@
const express = require('express');
const sha256 = require("sha256");
const { getUser, addUser } = require("../../../libs/mysql");
const { getConnection, getUser, addUser } = require("../../../libs/mysql");
const router = express.Router();
@ -12,13 +12,16 @@ router.post('/', async (req, res) => {
return res.status(400).send({error: "invalid request"});
}
const user = await getUser(username);
const connection = await getConnection();
const user = await getUser(connection, username);
if (user[0]) {
return res.status(400).send({error: "user already exist"});
}
await addUser(username, name, lastname, sha256(password));
await addUser(connection, username, name, lastname, sha256(password));
connection.end();
res.status(200).send({message: "success"});
});

View file

@ -1,7 +1,7 @@
const express = require('express');
const jwt = require('jsonwebtoken');
const {addHelper, getGame} = require("../../../libs/mysql.js")
const { getConnection, addHelper, getGame } = require("../../../libs/mysql.js")
const router = express.Router();
@ -22,8 +22,10 @@ router.post('/', async (req, res) => {
if (user.expiration < Date.now()) {
return res.status(400).send({error: "token expired"});
}
const connection = getConnection();
const game = await getGame(gameid);
const game = await getGame(connection, gameid);
if (!game[0]) {
return res.status(400).send({error: "this game doesn't exist"});
@ -33,7 +35,8 @@ router.post('/', async (req, res) => {
return res.status(400).send({error: "you are already an helper for this game"});
}
await addHelper(user.user.username, gameid);
await addHelper(connection, user.user.username, gameid);
connection.end();
} catch (error) {
console.log(error);
return res.status(400).send({error: "invalid token"});

View file

@ -1,7 +1,7 @@
const express = require('express');
const jwt = require('jsonwebtoken');
const {getGame} = require("../../../libs/mysql.js")
const { getConnection, getGame } = require("../../../libs/mysql.js")
const router = express.Router();
@ -26,7 +26,9 @@ router.post('/', async (req, res) => {
return res.status(400).send({error: "invalid token"});
}
const game = await getGame(gameid)
const connection = getConnection();
const game = await getGame(connection, gameid)
connection.end();
if (!game[0]) {
return res.status(400).send({error: "this game doesn't exist in the data base"})

View file

@ -1,7 +1,7 @@
const express = require('express');
const jwt = require('jsonwebtoken');
const {getHelpers} = require("../../../libs/mysql.js")
const { getConnection, getHelpers } = require("../../../libs/mysql.js")
const router = express.Router();
@ -26,7 +26,9 @@ router.post('/', async (req, res) => {
return res.status(400).send({error: "invalid token"});
}
const connection = getConnection();
const helpers = await getHelpers(gameid)
connection.end();
if (!helpers[0]) {
return res.status(400).send({error: "this game doesn't exist in the data base"})

View file

@ -1,7 +1,7 @@
const express = require('express');
const jwt = require('jsonwebtoken');
const {getGames} = require("../../../libs/mysql.js")
const { getConnection, getGames } = require("../../../libs/mysql.js")
const router = express.Router();
@ -22,7 +22,9 @@ router.post('/', async (req, res) => {
return res.status(400).send({error: "invalid token"});
}
const connection = getConnection();
const games = await getGames();
connection.end();
res.status(200).send(games);
});

View file

@ -1,7 +1,7 @@
const express = require('express');
const jwt = require('jsonwebtoken');
const {removeHelper, getGame} = require("../../../libs/mysql.js")
const { getConnection, removeHelper, getGame } = require("../../../libs/mysql.js")
const router = express.Router();
@ -23,6 +23,7 @@ router.post('/', async (req, res) => {
return res.status(400).send({error: "token expired"});
}
const connection = getConnection();
const game = await getGame(gameid);
if (!game[0]) {
@ -34,6 +35,7 @@ router.post('/', async (req, res) => {
}
await removeHelper(user.user.username, gameid);
connection.end()
} catch {
return res.status(400).send({error: "invalid token"});
}