getGameInfoU
This commit is contained in:
62
src/routes/api/getGameInfo.js
Normal file
62
src/routes/api/getGameInfo.js
Normal file
@@ -0,0 +1,62 @@
|
||||
const mariadb = require('mariadb');
|
||||
|
||||
const gameInfo = async (req, res) => {
|
||||
|
||||
// Create a connection pool
|
||||
const pool = mariadb.createPool({
|
||||
host: process.env.MARIA_HOST,
|
||||
user: process.env.MARIA_USER,
|
||||
password: process.env.MARIA_PASS,
|
||||
database: process.env.MARIA_DBNM,
|
||||
connectionLimit: 5,
|
||||
});
|
||||
|
||||
const { schoolId, teacherId } = req.query;
|
||||
|
||||
if (!schoolId && !teacherId) {
|
||||
return res.status(400).send({ error: 'Either schoolId or teacherId is required in the query parameters.' });
|
||||
}
|
||||
|
||||
let connection;
|
||||
try {
|
||||
// Establish a connection to the database
|
||||
connection = await pool.getConnection();
|
||||
|
||||
let queryCondition;
|
||||
let queryValue;
|
||||
|
||||
// Determine the query condition based on the parameters provided
|
||||
if (schoolId) {
|
||||
queryCondition = 'schoolId = ?';
|
||||
queryValue = schoolId;
|
||||
} else {
|
||||
queryCondition = 'teacherId = ?';
|
||||
queryValue = teacherId;
|
||||
}
|
||||
|
||||
// Query both game1 and game2 tables
|
||||
const query1 = `SELECT * FROM game_cross_varient1 WHERE ${queryCondition}`;
|
||||
const query2 = `SELECT * FROM game_cross_varient2 WHERE ${queryCondition}`;
|
||||
|
||||
const [game1Results, game2Results] = await Promise.all([
|
||||
connection.query(query1, [queryValue]),
|
||||
connection.query(query2, [queryValue]),
|
||||
]);
|
||||
|
||||
// Combine results from both tables
|
||||
const combinedResults = {
|
||||
game_cross_varient1: game1Results,
|
||||
game_cross_varient2: game2Results,
|
||||
};
|
||||
|
||||
res.status(200).send(combinedResults);
|
||||
} catch (error) {
|
||||
console.error('Error querying game tables:', error);
|
||||
res.status(500).send({ error: 'An error occurred while fetching game data.' });
|
||||
} finally {
|
||||
// Release the connection back to the pool
|
||||
if (connection) connection.release();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = gameInfo;
|
||||
@@ -193,7 +193,7 @@ router.get("/ping", (req, res) => {
|
||||
});
|
||||
|
||||
// getGameInfo.
|
||||
router.post("/getGameInfo", (req, res) => {
|
||||
router.get("/getGameInfo", (req, res) => {
|
||||
getGameInfo(req, res);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user