diff --git a/package-lock.json b/package-lock.json index 5b697d5..ca3c333 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,6 +25,7 @@ "http-status": "^1.4.0", "joi": "^17.3.0", "jsonwebtoken": "^8.5.1", + "mariadb": "^3.4.0", "moment": "^2.24.0", "mongoose": "^8.7.1", "morgan": "^1.9.1", @@ -1731,6 +1732,12 @@ "@babel/types": "^7.20.7" } }, + "node_modules/@types/geojson": { + "version": "7946.0.15", + "resolved": "https://registry.npmjs.org/@types/geojson/-/geojson-7946.0.15.tgz", + "integrity": "sha512-9oSxFzDCT2Rj6DfcHF8G++jxBKS7mBqXl5xrRW+Kbvjry6Uduya2iiwqHPhVXpasAVMBYKkEPGgKhd3+/HZ6xA==", + "license": "MIT" + }, "node_modules/@types/graceful-fs": { "version": "4.1.9", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.9.tgz", @@ -1785,7 +1792,6 @@ "version": "22.10.2", "resolved": "https://registry.npmjs.org/@types/node/-/node-22.10.2.tgz", "integrity": "sha512-Xxr6BBRCAOQixvonOye19wnzyDiUtTeqldOOmj3CkeblonbccA12PFwlufvRdrpjXxqnmUaeiU5EOA+7s5diUQ==", - "dev": true, "license": "MIT", "dependencies": { "undici-types": "~6.20.0" @@ -8412,6 +8418,40 @@ "node": ">=0.10.0" } }, + "node_modules/mariadb": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/mariadb/-/mariadb-3.4.0.tgz", + "integrity": "sha512-hdRPcAzs+MTxK5VG1thBW18gGTlw6yWBe9YnLB65GLo7q0fO5DWsgomIevV/pXSaWRmD3qi6ka4oSFRTExRiEQ==", + "license": "LGPL-2.1-or-later", + "dependencies": { + "@types/geojson": "^7946.0.14", + "@types/node": "^22.5.4", + "denque": "^2.1.0", + "iconv-lite": "^0.6.3", + "lru-cache": "^10.3.0" + }, + "engines": { + "node": ">= 14" + } + }, + "node_modules/mariadb/node_modules/iconv-lite": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", + "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", + "license": "MIT", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/mariadb/node_modules/lru-cache": { + "version": "10.4.3", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.4.3.tgz", + "integrity": "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==", + "license": "ISC" + }, "node_modules/math-intrinsics": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", @@ -13225,7 +13265,6 @@ "version": "6.20.0", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.20.0.tgz", "integrity": "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==", - "dev": true, "license": "MIT" }, "node_modules/union-value": { diff --git a/package.json b/package.json index 236b74e..77f68d2 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "http-status": "^1.4.0", "joi": "^17.3.0", "jsonwebtoken": "^8.5.1", + "mariadb": "^3.4.0", "moment": "^2.24.0", "mongoose": "^8.7.1", "morgan": "^1.9.1", diff --git a/src/routes/api/getGameInfo.js b/src/routes/api/getGameInfo.js new file mode 100644 index 0000000..07f3c68 --- /dev/null +++ b/src/routes/api/getGameInfo.js @@ -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; diff --git a/src/routes/v1/api.route.js b/src/routes/v1/api.route.js index 6aefaee..b674cef 100644 --- a/src/routes/v1/api.route.js +++ b/src/routes/v1/api.route.js @@ -193,7 +193,7 @@ router.get("/ping", (req, res) => { }); // getGameInfo. - router.post("/getGameInfo", (req, res) => { + router.get("/getGameInfo", (req, res) => { getGameInfo(req, res); });