- [API] Finished the request to get all spells (associated tables for instance)

This commit is contained in:
Alexis
2020-03-28 20:45:44 +01:00
parent 481ed7507f
commit 27a1113bd2
5 changed files with 113 additions and 46 deletions

View File

@@ -13,4 +13,10 @@ const db = mysql.createConnection({
database: credentials.database, database: credentials.database,
}) })
db.on('error', err => {
if (err.errno == 'ECONNRESET') {
console.log("The connection was reset during your request. Please try again later.");
}
})
module.exports = { db } module.exports = { db }

7
database/querytests.sql Normal file
View File

@@ -0,0 +1,7 @@
USE spellsaurus;
/* Fetches a specific spell's school(s) */
SELECT school.name
FROM spells_schools AS sc
INNER JOIN school AS school ON sc.id_school = school.id
WHERE sc.id_spell = 1;

View File

@@ -1,14 +1,3 @@
DROP USER IF EXISTS 'archivist'@'%';
CREATE USER 'archivist'@'%'
IDENTIFIED BY 'root';
GRANT ALL
ON spellsaurus.*
TO 'archivist'@'%';
FLUSH PRIVILEGES;
DROP DATABASE IF EXISTS spellsaurus; DROP DATABASE IF EXISTS spellsaurus;
CREATE DATABASE spellsaurus; CREATE DATABASE spellsaurus;
USE spellsaurus; USE spellsaurus;

View File

@@ -1,23 +1,25 @@
'use strict';
// MODULES // MODULES
const express = require('express'); const express = require('express')
const connection = require('./database/connection'); const connection = require('./database/connection')
const db = connection.db; const db = connection.db
//
const routes = require('./routes');
// CONSTANTS // CONSTANTS
const port = 2814; const port = 2814
// APP // Import routes
app = express(); const routes = require('./routes')
const server = app.listen( port, () => {console.log(`App listening on port ${port}`)});
app.use('/spells', routes.spells); // Builds app
let app = express()
const server = app.listen( port, () => { console.log(`App listening on port ${port}`)} )
app.use('/spells', routes.spells)
// On process kill with SIGINT // On process kill with SIGINT
process.on('SIGINT', () => { process.on('SIGINT', () => {
db.end(); db.end()
server.close(); server.close()
}) })

View File

@@ -1,39 +1,102 @@
const express = require('express'); 'use strict'
let router = express.Router();
const connection = require('../database/connection'); const express = require('express')
const db = connection.db; let router = express.Router()
getSpells = () => { const connection = require('../database/connection')
const db = connection.db
const getSpells = (params) => {
let fetchSpellsData = new Promise((resolve, reject) => { let fetchSpellsData = new Promise((resolve, reject) => {
let query = let query =
"SELECT DISTINCT * " + "SELECT DISTINCT * " +
"FROM spell"; "FROM spell " +
"WHERE 1 = 1 "
db.query(query, (err, result) => { // Checks if the params object has items
if (err) { return reject } if (!(Object.keys(params).length === 0 && params.constructor === Object)) {
if (result.length == 0) { console.log("No results found") } if (params.id) {
query += "AND id = " + params.id + " "
for (let i = 0; i < result.length; i++) {
let currentSpell = result[i];
let schoolQuery;
} }
}) if (params.name) {
query += "AND name = " + params.name + " "
}
}
let fetchSpellsSchoolsData = new Promise((resolve, reject) => { db.query(query, async (err, result) => {
if (err) { return reject }
if (result.length == 0) { console.log("No spells found") }
// Loops over the results to fetch the associated tables
for (let i = 0; i < result.length; i++) {
let currentSpell_ID = i + 1
// Fetches the spell's schools
let fetchSpellSchoolData = new Promise((resolve, reject) => {
let query =
"SELECT school.name, school.description " +
"FROM spells_schools AS sc " +
"INNER JOIN school AS school ON sc.id_school = school.id " +
"WHERE sc.id_spell = " + currentSpell_ID
db.query(query, (err, result) => {
if (err) return reject
resolve(result)
})
})
// Fetches the spell's variables
let fetchSpellVariablesData = new Promise((resolve, reject) => {
let query =
"SELECT variable.description " +
"FROM spells_variables AS sv " +
"INNER JOIN variable AS variable ON sv.id_variable = variable.id " +
"WHERE sv.id_spell = " + currentSpell_ID
db.query(query, (err, result) => {
if (err) console.log(err)
resolve(result)
})
})
// Fetches the spell's ingredients
let fetchSpellIngredientsData = new Promise((resolve, reject) => {
let query =
"SELECT ingredient.name " +
"FROM spells_ingredients AS si " +
"INNER JOIN ingredient AS ingredient ON si.id_ingredient = ingredient.id " +
"WHERE si.id_spell = " + currentSpell_ID
db.query(query, (err, result) => {
if (err) console.log(err)
resolve(result)
})
})
result[i].schools = await fetchSpellSchoolData
result[i].variables = await fetchSpellVariablesData
result[i].ingredients = await fetchSpellIngredientsData
}
resolve(result)
}) })
}) })
return fetchSpellsData; return fetchSpellsData
} }
router router
.get('/', (req, res, next) => { .get('/', async (req, res, next) => {
getSpells().then(v => { getSpells(req.query)
res.setHeader('Content-Type', 'application/json'); .then(v => {
res.end(JSON.stringify({spells : v})); res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({spells : v}))
})
.catch(err => {
console.log(err)
next()
}) })
}) })