Fixed database request handling
This commit is contained in:
22
.idea/workspace.xml
generated
22
.idea/workspace.xml
generated
@@ -14,9 +14,9 @@
|
||||
</configurations>
|
||||
</component>
|
||||
<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$/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>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@@ -210,7 +210,10 @@
|
||||
<workItem from="1716889548355" duration="3475000" />
|
||||
<workItem from="1716904079045" duration="2259000" />
|
||||
<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 id="LOCAL-00001" summary="Changes">
|
||||
<created>1713528225837</created>
|
||||
@@ -392,7 +395,15 @@
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1716977853269</updated>
|
||||
</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 />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
@@ -433,6 +444,7 @@
|
||||
<MESSAGE value="Commit war crimes in formal Yugoslavia" />
|
||||
<MESSAGE value="Crack butt" />
|
||||
<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>
|
||||
</project>
|
@@ -1,3 +1,6 @@
|
||||
|
||||
const databaseQuery = 'SELECT * FROM `Exercise` ORDER BY RAND() LIMIT 1';
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Request} request The incoming request
|
||||
@@ -8,45 +11,33 @@
|
||||
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
|
||||
pool.getConnection()
|
||||
.then(conn => {
|
||||
conn.query(query, parameters)
|
||||
conn.query(
|
||||
databaseQuery)
|
||||
.then(rows => {
|
||||
if (rows.length === 0)
|
||||
{
|
||||
response
|
||||
.status(404)
|
||||
.send(JSON.stringify({error: 'Exercise not found'}));
|
||||
.send(JSON.stringify({error: 'No exercises found in the database.'}));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Send back the data in the right format
|
||||
let converted = rows.map(row => {
|
||||
return {
|
||||
exerciseId: row.ExerciseID,
|
||||
name: row.Name,
|
||||
muscleGroup: row.MuscleGroup,
|
||||
shortDescription: row.ShortDescription,
|
||||
description: row.Description,
|
||||
imageUrl: row.ImageURL,
|
||||
videoUrl: row.VideoURL,
|
||||
path: row.Path,
|
||||
duration: row.Duration
|
||||
};
|
||||
})[0];
|
||||
|
||||
response
|
||||
.status(200)
|
||||
.send(JSON.stringify(converted));
|
||||
let row = rows[0];
|
||||
response.status(200)
|
||||
.send(JSON.stringify({
|
||||
exerciseId: row['ExerciseID'],
|
||||
name: row['Name'],
|
||||
muscleGroup: row['MuscleGroup'],
|
||||
shortDescription: row['ShortDescription'],
|
||||
description: row['Description'],
|
||||
imageUrl: row['ImageURL'],
|
||||
videoUrl: row['VideoURL'],
|
||||
path: row['Path'],
|
||||
duration: row['Duration']
|
||||
}))
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
|
Reference in New Issue
Block a user