Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Request} request The incoming request
|
||||
* @param {Response} response The response to send back
|
||||
* @param {Express} app Express app instance
|
||||
* @param {Pool} pool MariaDB pool instance
|
||||
*/
|
||||
function handleIncoming(request, response, app, pool)
|
||||
{
|
||||
|
||||
let query = 'SELECT * FROM Exercise WHERE ExerciseID = ?';
|
||||
let parameters = [];
|
||||
|
||||
if (!request.hasOwnProperty('uid') || typeof request.uid !== 'number')
|
||||
{
|
||||
query = 'SELECT * FROM Exercise';
|
||||
} else parameters.push(request.uid);
|
||||
|
||||
// Acquire database connection
|
||||
pool.getConnection()
|
||||
.then(conn => {
|
||||
conn.query(query, parameters)
|
||||
.then(rows => {
|
||||
if (rows.length === 0)
|
||||
{
|
||||
response
|
||||
.status(404)
|
||||
.send(JSON.stringify({error: 'Exercise not found'}));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Send back the data in the right format
|
||||
let converted = rows.map(row => {
|
||||
return {
|
||||
name: row.Name,
|
||||
description: row.Description,
|
||||
muscleGroup: row.MuscleGroup,
|
||||
imageUrl: row.ImageURL,
|
||||
videoUrl: row.VideoURL
|
||||
};
|
||||
});
|
||||
|
||||
response
|
||||
.status(200)
|
||||
.send(JSON.stringify(converted));
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
response
|
||||
.status(500)
|
||||
.send(JSON.stringify({error: 'Internal server error (Querying)'}));
|
||||
})
|
||||
.finally(() => {
|
||||
conn.end();
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
response
|
||||
.status(500)
|
||||
.send(JSON.stringify({error: 'Internal server error (Connection)'}));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// Export the function that registers the incoming request handlers
|
||||
module.exports = function(app, pool) {
|
||||
app.post('/', (req, res) => handleIncoming(req, res, app, pool));
|
||||
};
|
@@ -1,27 +0,0 @@
|
||||
const mariadb = require('mariadb');
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const bodyParser = require('body-parser');
|
||||
|
||||
app.use(bodyParser.json());
|
||||
const serverPort = 3000;
|
||||
|
||||
const databaseCredentials = {
|
||||
host: '127.0.0.1',
|
||||
user: 'fitbot',
|
||||
password: 'fitbot123',
|
||||
database: 'fitbot',
|
||||
connectionLimit: 5,
|
||||
allowUnauthorized: true
|
||||
};
|
||||
|
||||
// Create connection pool
|
||||
const pool = mariadb.createPool(databaseCredentials);
|
||||
|
||||
// Register incoming HTTP request handlers
|
||||
require('./incoming_request_handlers')(app, pool);
|
||||
|
||||
// Start server
|
||||
app.listen(serverPort, () => {
|
||||
console.log(`Server running on port ${serverPort}`);
|
||||
});
|
Reference in New Issue
Block a user