diff --git a/database/connection.js b/database/connection.js index 8000d2a..3084136 100644 --- a/database/connection.js +++ b/database/connection.js @@ -13,4 +13,10 @@ const db = mysql.createConnection({ 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 } \ No newline at end of file diff --git a/database/querytests.sql b/database/querytests.sql new file mode 100644 index 0000000..6c5f3df --- /dev/null +++ b/database/querytests.sql @@ -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; \ No newline at end of file diff --git a/database/spellsaurus_db_create.sql b/database/spellsaurus_db_create.sql index 82ae670..6a16d53 100644 --- a/database/spellsaurus_db_create.sql +++ b/database/spellsaurus_db_create.sql @@ -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; CREATE DATABASE spellsaurus; USE spellsaurus; diff --git a/index.js b/index.js index 44b81f8..0e5b0c9 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,25 @@ +'use strict'; + // MODULES -const express = require('express'); +const express = require('express') -const connection = require('./database/connection'); -const db = connection.db; - -// -const routes = require('./routes'); +const connection = require('./database/connection') +const db = connection.db // CONSTANTS -const port = 2814; +const port = 2814 -// APP -app = express(); -const server = app.listen( port, () => {console.log(`App listening on port ${port}`)}); +// Import routes +const routes = require('./routes') -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 process.on('SIGINT', () => { - db.end(); - server.close(); + db.end() + server.close() }) diff --git a/routes/spells.js b/routes/spells.js index 1f0e823..8a6e7ec 100644 --- a/routes/spells.js +++ b/routes/spells.js @@ -1,39 +1,102 @@ -const express = require('express'); -let router = express.Router(); +'use strict' -const connection = require('../database/connection'); -const db = connection.db; +const express = require('express') +let router = express.Router() -getSpells = () => { +const connection = require('../database/connection') +const db = connection.db + +const getSpells = (params) => { let fetchSpellsData = new Promise((resolve, reject) => { + let query = "SELECT DISTINCT * " + - "FROM spell"; + "FROM spell " + + "WHERE 1 = 1 " - db.query(query, (err, result) => { - if (err) { return reject } - if (result.length == 0) { console.log("No results found") } - - for (let i = 0; i < result.length; i++) { - let currentSpell = result[i]; - - let schoolQuery; + // Checks if the params object has items + if (!(Object.keys(params).length === 0 && params.constructor === Object)) { + if (params.id) { + query += "AND id = " + params.id + " " } - }) + 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 -.get('/', (req, res, next) => { - getSpells().then(v => { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({spells : v})); +.get('/', async (req, res, next) => { + getSpells(req.query) + .then(v => { + res.setHeader('Content-Type', 'application/json') + res.end(JSON.stringify({spells : v})) + }) + .catch(err => { + console.log(err) + next() }) })