diff --git a/models/school-model.js b/models/school-model.js index 2d3ef63..b8bf5ea 100644 --- a/models/school-model.js +++ b/models/school-model.js @@ -7,10 +7,10 @@ require('./meta-school-model') let School = bookshelf.Model.extend({ tableName: 'school', spells() { - return this.belongsToMany( 'Spell', 'spell_school') + return this.belongsToMany( 'Spell', 'spell_school' ) }, meta_schools() { - return this.belongsTo( 'MetaSchool', 'meta_school_id') + return this.belongsTo( 'MetaSchool', 'meta_school_id' ) } }) diff --git a/repositories/ingredient-repository.js b/repositories/ingredient-repository.js index d6cc682..f510d7e 100644 --- a/repositories/ingredient-repository.js +++ b/repositories/ingredient-repository.js @@ -50,6 +50,21 @@ class IngredientRepository { }) } + getSpellsFromOne(id) { + return new Promise((resolve, reject) => { + model.forge() + .where({ 'id' : id }) + .fetch({ withRelated: ['spells']}) + .then(v => { + resolve(v.toJSON({ omitPivot: true })) + }) + .catch(err => { + console.log(err) + reject(new HttpError(500, "Couldn't get ingredient")) + }) + }) + } + addOne(igr) { return new Promise((resolve, reject) => { // Checks if body exists and if the model fits, and throws errors if it doesn't diff --git a/repositories/school-repository.js b/repositories/school-repository.js index 2ffa311..3abb3c6 100644 --- a/repositories/school-repository.js +++ b/repositories/school-repository.js @@ -49,6 +49,21 @@ class SchoolRepository { }) } + getSpellsFromOne(id) { + return new Promise((resolve, reject) => { + model.forge() + .where({ 'id' : id }) + .fetch({ withRelated: ['spells', 'meta_schools']}) + .then(v => { + resolve(v.toJSON({ omitPivot: true })) + }) + .catch(err => { + console.log(err) + reject(new HttpError(500, "Couldn't get school")) + }) + }) + } + addOne(s) { return new Promise((resolve, reject) => { // Checks if body exists and if the model fits, and throws errors if it doesn't diff --git a/repositories/variable-repository.js b/repositories/variable-repository.js index 517a672..bc77297 100644 --- a/repositories/variable-repository.js +++ b/repositories/variable-repository.js @@ -49,6 +49,21 @@ class VariableRepository { }) } + getSpellsFromOne(id) { + return new Promise((resolve, reject) => { + model.forge() + .where({ 'id' : id }) + .fetch({ withRelated: ['spells']}) + .then(v => { + resolve(v.toJSON({ omitPivot: true })) + }) + .catch(err => { + console.log(err) + reject(new HttpError(500, "Couldn't get variable")) + }) + }) + } + addOne(vr) { return new Promise((resolve, reject) => { // Checks if body exists and if the model fits, and throws errors if it doesn't diff --git a/routes/ingredients.js b/routes/ingredients.js index 057b2f3..7748a8e 100644 --- a/routes/ingredients.js +++ b/routes/ingredients.js @@ -68,6 +68,31 @@ router.get('/:id/', async (req, res) => { }) +// GET SPELLS FROM ONE ------------------ +const getSpellsFromOne = (id) => { + return Ingredients.getSpellsFromOne(id) + .catch(err => { + console.log(err) + throw err + }) +} +router.get('/:id/spells', async (req, res) => { + getSpellsFromOne(req.params.id) + .then(v => { + res.setHeader('Content-Type', 'application/json;charset=utf-8') + res.end(JSON.stringify(v)) + }) + .catch(err => { + res.status(err.code).send(JSON.stringify( + { + "error": err.message, + "code": err.code + }) + ) + }) +}) + + // CREATE ONE ------------------ const addIngredient = (igr) => { return Ingredients.addOne(igr) diff --git a/routes/schools.js b/routes/schools.js index 4b228e0..1be0200 100644 --- a/routes/schools.js +++ b/routes/schools.js @@ -68,6 +68,31 @@ router.get('/:id/', async (req, res) => { }) +// GET SPELLS FROM ONE ------------------ +const getSpellsFromOne = (id) => { + return Schools.getSpellsFromOne(id) + .catch(err => { + console.log(err) + throw err + }) +} +router.get('/:id/spells', async (req, res) => { + getSpellsFromOne(req.params.id) + .then(v => { + res.setHeader('Content-Type', 'application/json;charset=utf-8') + res.end(JSON.stringify(v)) + }) + .catch(err => { + res.status(err.code).send(JSON.stringify( + { + "error": err.message, + "code": err.code + }) + ) + }) +}) + + // CREATE ONE ------------------ const addSchool = (s) => { return Schools.addOne(s) diff --git a/routes/variables.js b/routes/variables.js index a4e649a..01ee961 100644 --- a/routes/variables.js +++ b/routes/variables.js @@ -68,6 +68,31 @@ router.get('/:id/', async (req, res) => { }) +// GET SPELLS FROM ONE ------------------ +const getSpellsFromOne = (id) => { + return Variables.getSpellsFromOne(id) + .catch(err => { + console.log(err) + throw err + }) +} +router.get('/:id/spells', async (req, res) => { + getSpellsFromOne(req.params.id) + .then(v => { + res.setHeader('Content-Type', 'application/json;charset=utf-8') + res.end(JSON.stringify(v)) + }) + .catch(err => { + res.status(err.code).send(JSON.stringify( + { + "error": err.message, + "code": err.code + }) + ) + }) +}) + + // CREATE ONE ------------------ const addVariable = (vr) => { return Variables.addOne(vr)