diff --git a/routes/schools.js b/routes/schools.js new file mode 100644 index 0000000..28a46f0 --- /dev/null +++ b/routes/schools.js @@ -0,0 +1,116 @@ +'use strict' + +const express = require('express') +let router = express.Router() + +const connection = require('../database/connection') +const db = connection.db + +// Model validation +const Validator = require('jsonschema').Validator +const v = new Validator() +const School = require("../models/School") +v.addSchema(School, "/SchoolModel") + +const getSchools = () => { + let getSchoolsPromise = new Promise((resolve, reject) => { + + let query = "SELECT DISTINCT * FROM school" + + db.query(query, async (err, result) => { + if (err) return reject + if (result.length == 0) { console.log("No school found in database") } + + // Loops over the results to fetch the associated tables + for (let i = 0; i < result.length; i++) { + result[i] = await buildSchool(result[i]) + } + resolve(result) + }) + }) + return getSchoolsPromise +} + +const getSchool = (id) => { + let getSchoolPromise = new Promise((resolve, reject) => { + + let query = "SELECT * FROM school WHERE id = " + id + + db.query(query, async (err, result) => { + if (err) return reject + result = await buildSchool(result[0]) + resolve(result); + }) + }) + return getSchoolPromise +} + +// ROUTES +// Get All +router.get('/', async (req, res, next) => { + getSchools() + .then(v => { + res.setHeader('Content-Type', 'application/json;charset=utf-8') + res.end(JSON.stringify(v)) + }) + .catch(err => { + console.log(err) + next() + }) +}) + +// Get One +router.get('/:id/', async (req, res, next) => { + getSchool(req.params.id) + .then(v => { + res.setHeader('Content-Type', 'application/json;charset=utf-8') + res.end(JSON.stringify(v)) + }) + .catch(err => { + console.log(err) + next() + }) +}) + +// Param validation for single school +// (check if id is int) (could be refactored) +router.param('id', (req, res, next, id) => { + const regex = RegExp(/^[1-9]\d*$/); + try { + if (regex.test(id)) { + next() + } else { + let err = { + name: "InvalidParameterException", + description: "The parameter is not valid. It should be an integer." + } + throw err + } + } catch (err) { + res.status(403).send(err) + } +}) + +// SHARED FUNCTIONS +// Builds the associated infos for a given school object +const buildSchool = async (school) => { + + // Fetches the school's parent school + let fetchMetaSchoolData = new Promise((resolve, reject) => { + let query = + "SELECT ms.id, ms.name " + + "FROM meta_school AS ms " + + "WHERE ms.id = " + school.id_meta_school + + db.query(query, (err, result) => { + if (err) return reject + resolve(result) + }) + }) + + // Builds the school and returns it + school.meta_school = await fetchMetaSchoolData + return school +} + +module.exports = router \ No newline at end of file diff --git a/routes/spells.js b/routes/spells.js index cc27c52..9e4a0a5 100644 --- a/routes/spells.js +++ b/routes/spells.js @@ -6,15 +6,20 @@ let router = express.Router() const connection = require('../database/connection') const db = connection.db +// Model validation +const Validator = require('jsonschema').Validator +const v = new Validator() +const Spell = require("../models/Spell") +v.addSchema(Spell, "/SpellModel") const getSpells = () => { - let fetchSpellsData = new Promise((resolve, reject) => { + let getSpellsPromise = new Promise((resolve, reject) => { let query = "SELECT DISTINCT * FROM spell" db.query(query, async (err, result) => { - if (err) { return reject } - if (result.length == 0) { console.log("No spells found") } + if (err) return reject + if (result.length == 0) { console.log("No spell found in database") } // Loops over the results to fetch the associated tables for (let i = 0; i < result.length; i++) { @@ -23,11 +28,11 @@ const getSpells = () => { resolve(result) }) }) - return fetchSpellsData + return getSpellsPromise } const getSpell = (id) => { - let fetchSpellData = new Promise((resolve, reject) => { + let getSpellPromise = new Promise((resolve, reject) => { let query = "SELECT * FROM spell WHERE id = " + id @@ -37,10 +42,38 @@ const getSpell = (id) => { resolve(result); }) }) - return fetchSpellData + return getSpellPromise +} + +const addSpell = (spell) => { + let addSpellPromise = new Promise((resolve, reject) => { + + // Checks if body is empty + if(Object.keys(spell).length === 0 && spell.constructor === Object) { + reject("You can't add an empty spell !") + } + + // Check if model validation goes through + if (!v.validate(spell, Spell).valid) { + reject("Schema is not valid") + } else { + resolve(spell) + } + + }) + return addSpellPromise +} + +const updateSpell = (spell) => { + return null; +} + +const deleteSpell = (id) => { + return null; } // ROUTES +// Get All router.get('/', async (req, res, next) => { getSpells() .then(v => { @@ -52,6 +85,8 @@ router.get('/', async (req, res, next) => { next() }) }) + +// Get One router.get('/:id/', async (req, res, next) => { getSpell(req.params.id) .then(v => { @@ -64,6 +99,30 @@ router.get('/:id/', async (req, res, next) => { }) }) +// Add One +router.post('/', async (req, res, next) => { + console.log(req.body) + addSpell(req.body) + .then(v => { + res.setHeader('Content-Type', 'application/json;charset=utf-8') + res.send(JSON.stringify(v)) + }) + .catch(err => { + console.log(err) + next() + }) +}) + +// Update One +router.put('/:id/', async (req, res, next) => { + +}) + +// Delete One +router.delete('/:id/', async (req, res, next) => { + +}) + // Param validation for single spell // (check if id is int) (could be refactored) router.param('id', (req, res, next, id) => {