Fixed database request handling
This commit is contained in:
22
.idea/workspace.xml
generated
22
.idea/workspace.xml
generated
@@ -14,9 +14,9 @@
|
|||||||
</configurations>
|
</configurations>
|
||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="00599d5b-7eb5-44da-ad7f-98bf42384c16" name="Changes" comment="Updated incoming_request_handlers.js to match database querying">
|
<list default="true" id="00599d5b-7eb5-44da-ad7f-98bf42384c16" name="Changes" comment="Yebal">
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/code/web/pepper_data_test.js" beforeDir="false" afterPath="$PROJECT_DIR$/code/web/pepper_data_test.js" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/code/server/incoming_request_handlers.js" beforeDir="false" afterPath="$PROJECT_DIR$/code/server/incoming_request_handlers.js" afterDir="false" />
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
@@ -210,7 +210,10 @@
|
|||||||
<workItem from="1716889548355" duration="3475000" />
|
<workItem from="1716889548355" duration="3475000" />
|
||||||
<workItem from="1716904079045" duration="2259000" />
|
<workItem from="1716904079045" duration="2259000" />
|
||||||
<workItem from="1716977405893" duration="466000" />
|
<workItem from="1716977405893" duration="466000" />
|
||||||
<workItem from="1716983084935" duration="3759000" />
|
<workItem from="1716983084935" duration="4041000" />
|
||||||
|
<workItem from="1716992784136" duration="136000" />
|
||||||
|
<workItem from="1717093210968" duration="6000" />
|
||||||
|
<workItem from="1717416227033" duration="368000" />
|
||||||
</task>
|
</task>
|
||||||
<task id="LOCAL-00001" summary="Changes">
|
<task id="LOCAL-00001" summary="Changes">
|
||||||
<created>1713528225837</created>
|
<created>1713528225837</created>
|
||||||
@@ -392,7 +395,15 @@
|
|||||||
<option name="project" value="LOCAL" />
|
<option name="project" value="LOCAL" />
|
||||||
<updated>1716977853269</updated>
|
<updated>1716977853269</updated>
|
||||||
</task>
|
</task>
|
||||||
<option name="localTasksCounter" value="24" />
|
<task id="LOCAL-00024" summary="Yebal">
|
||||||
|
<option name="closed" value="true" />
|
||||||
|
<created>1716988959836</created>
|
||||||
|
<option name="number" value="00024" />
|
||||||
|
<option name="presentableId" value="LOCAL-00024" />
|
||||||
|
<option name="project" value="LOCAL" />
|
||||||
|
<updated>1716988959836</updated>
|
||||||
|
</task>
|
||||||
|
<option name="localTasksCounter" value="25" />
|
||||||
<servers />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
<component name="TypeScriptGeneratedFilesManager">
|
<component name="TypeScriptGeneratedFilesManager">
|
||||||
@@ -433,6 +444,7 @@
|
|||||||
<MESSAGE value="Commit war crimes in formal Yugoslavia" />
|
<MESSAGE value="Commit war crimes in formal Yugoslavia" />
|
||||||
<MESSAGE value="Crack butt" />
|
<MESSAGE value="Crack butt" />
|
||||||
<MESSAGE value="Updated incoming_request_handlers.js to match database querying" />
|
<MESSAGE value="Updated incoming_request_handlers.js to match database querying" />
|
||||||
<option name="LAST_COMMIT_MESSAGE" value="Updated incoming_request_handlers.js to match database querying" />
|
<MESSAGE value="Yebal" />
|
||||||
|
<option name="LAST_COMMIT_MESSAGE" value="Yebal" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@@ -1,3 +1,6 @@
|
|||||||
|
|
||||||
|
const databaseQuery = 'SELECT * FROM `Exercise` ORDER BY RAND() LIMIT 1';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {Request} request The incoming request
|
* @param {Request} request The incoming request
|
||||||
@@ -8,45 +11,33 @@
|
|||||||
function handleIncoming(request, response, app, pool)
|
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 ORDER BY RAND() LIMIT 1';
|
|
||||||
} else parameters.push(request.uid);
|
|
||||||
|
|
||||||
// Acquire database connection
|
// Acquire database connection
|
||||||
pool.getConnection()
|
pool.getConnection()
|
||||||
.then(conn => {
|
.then(conn => {
|
||||||
conn.query(query, parameters)
|
conn.query(
|
||||||
|
databaseQuery)
|
||||||
.then(rows => {
|
.then(rows => {
|
||||||
if (rows.length === 0)
|
if (rows.length === 0)
|
||||||
{
|
{
|
||||||
response
|
response
|
||||||
.status(404)
|
.status(404)
|
||||||
.send(JSON.stringify({error: 'Exercise not found'}));
|
.send(JSON.stringify({error: 'No exercises found in the database.'}));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Send back the data in the right format
|
let row = rows[0];
|
||||||
let converted = rows.map(row => {
|
response.status(200)
|
||||||
return {
|
.send(JSON.stringify({
|
||||||
exerciseId: row.ExerciseID,
|
exerciseId: row['ExerciseID'],
|
||||||
name: row.Name,
|
name: row['Name'],
|
||||||
muscleGroup: row.MuscleGroup,
|
muscleGroup: row['MuscleGroup'],
|
||||||
shortDescription: row.ShortDescription,
|
shortDescription: row['ShortDescription'],
|
||||||
description: row.Description,
|
description: row['Description'],
|
||||||
imageUrl: row.ImageURL,
|
imageUrl: row['ImageURL'],
|
||||||
videoUrl: row.VideoURL,
|
videoUrl: row['VideoURL'],
|
||||||
path: row.Path,
|
path: row['Path'],
|
||||||
duration: row.Duration
|
duration: row['Duration']
|
||||||
};
|
}))
|
||||||
})[0];
|
|
||||||
|
|
||||||
response
|
|
||||||
.status(200)
|
|
||||||
.send(JSON.stringify(converted));
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
Reference in New Issue
Block a user