Added getSpellsFromUser() method
This commit is contained in:
@@ -7,14 +7,18 @@ require('./ingredient-model')
|
||||
|
||||
let Spell = bookshelf.Model.extend({
|
||||
tableName: 'spell',
|
||||
hidden: [ 'author_id' ],
|
||||
author() {
|
||||
return this.belongsTo( 'User', 'author_id' );
|
||||
},
|
||||
schools() {
|
||||
return this.belongsToMany( 'School', 'spell_school' )
|
||||
return this.belongsToMany( 'School', 'spell_school' );
|
||||
},
|
||||
variables() {
|
||||
return this.belongsToMany( 'Variable', 'spell_variable' )
|
||||
return this.belongsToMany( 'Variable', 'spell_variable' );
|
||||
},
|
||||
ingredients() {
|
||||
return this.belongsToMany( 'Ingredient', 'spell_ingredient' )
|
||||
return this.belongsToMany( 'Ingredient', 'spell_ingredient' );
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
const bookshelf = require('../database/bookshelf').bookshelf;
|
||||
|
||||
require('./role-model');
|
||||
require('./spell-model');
|
||||
|
||||
let User = bookshelf.Model.extend({
|
||||
tableName: 'user',
|
||||
@@ -9,6 +10,9 @@ let User = bookshelf.Model.extend({
|
||||
role() {
|
||||
return this.belongsTo( 'Role' );
|
||||
},
|
||||
spells() {
|
||||
return this.hasMany( 'Spell', 'author_id' );
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = bookshelf.model('User', User)
|
||||
@@ -30,7 +30,7 @@ class SpellRepository {
|
||||
if (cost) { query.where({ 'cost' : cost }) }
|
||||
if (ritual) { query.where({ 'is_ritual' : ritual }) }
|
||||
|
||||
query.fetchAll({ withRelated: ['schools.meta_schools', 'variables', 'ingredients'] })
|
||||
query.fetchAll({ withRelated: [ 'schools.meta_schools', 'variables', 'ingredients', 'author' ] })
|
||||
.then(v => {
|
||||
resolve(v.toJSON({ omitPivot: true }))
|
||||
})
|
||||
@@ -56,7 +56,7 @@ class SpellRepository {
|
||||
if (cost) { query.where({ 'cost' : cost }) }
|
||||
if (ritual) { query.where({ 'is_ritual' : ritual }) }
|
||||
|
||||
query.fetchAll({ withRelated: ['schools.meta_schools', 'variables', 'ingredients'] })
|
||||
query.fetchAll({ withRelated: [ 'schools.meta_schools', 'variables', 'ingredients', 'author' ] })
|
||||
.then(v => {
|
||||
resolve(v.toJSON({ omitPivot: true }));
|
||||
})
|
||||
@@ -77,7 +77,7 @@ class SpellRepository {
|
||||
.fetchPage({
|
||||
pageSize: 20,
|
||||
page: page,
|
||||
withRelated: ['schools.meta_schools', 'variables', 'ingredients'],
|
||||
withRelated: [ 'schools.meta_schools', 'variables', 'ingredients', 'author' ],
|
||||
})
|
||||
.then(v => {
|
||||
resolve(v.toJSON({ omitPivot: true }))
|
||||
@@ -96,7 +96,7 @@ class SpellRepository {
|
||||
return new Promise((resolve, reject) => {
|
||||
new model()
|
||||
.where({ 'id' : id })
|
||||
.fetch({ withRelated: ['schools.meta_schools', 'variables', 'ingredients']})
|
||||
.fetch({ withRelated: [ 'schools.meta_schools', 'variables', 'ingredients', 'author' ]})
|
||||
.then(v => {
|
||||
resolve(v.toJSON({ omitPivot: true }))
|
||||
})
|
||||
@@ -170,7 +170,7 @@ class SpellRepository {
|
||||
})
|
||||
})
|
||||
.then(v => {
|
||||
return v.load(['schools.meta_schools', 'variables', 'ingredients'])
|
||||
return v.load([ 'schools.meta_schools', 'variables', 'ingredients', 'author' ])
|
||||
})
|
||||
.then(v => {
|
||||
resolve(this.getOne(v.id))
|
||||
@@ -206,7 +206,7 @@ class SpellRepository {
|
||||
});
|
||||
} else {
|
||||
new model({id: id})
|
||||
.fetch({require: true, withRelated: ['schools.meta_schools', 'variables', 'ingredients']})
|
||||
.fetch({require: true, withRelated: [ 'schools.meta_schools', 'variables', 'ingredients', 'author' ]})
|
||||
.then(v => {
|
||||
bookshelf.transaction(t => {
|
||||
return v.save({
|
||||
@@ -269,7 +269,7 @@ class SpellRepository {
|
||||
})
|
||||
})
|
||||
.then(v => {
|
||||
return v.load(['schools.meta_schools', 'variables', 'ingredients']);
|
||||
return v.load([ 'schools.meta_schools', 'variables', 'ingredients', 'author' ]);
|
||||
})
|
||||
.then(v => {
|
||||
resolve(this.getOne(v.id));
|
||||
@@ -297,7 +297,7 @@ class SpellRepository {
|
||||
return new Promise((resolve, reject) => {
|
||||
new model()
|
||||
.where({ 'id' : id })
|
||||
.fetch({require: true, withRelated: ['schools.meta_schools', 'variables', 'ingredients']})
|
||||
.fetch({require: true, withRelated: [ 'schools.meta_schools', 'variables', 'ingredients', 'author' ]})
|
||||
.then(v => {
|
||||
v.schools().detach();
|
||||
v.variables().detach();
|
||||
|
||||
@@ -63,7 +63,7 @@ class UserRepository {
|
||||
return new Promise((resolve, reject) => {
|
||||
new model()
|
||||
.where({ 'mail': mail })
|
||||
.fetch()
|
||||
.fetch({ withRelated: [ 'role' ] })
|
||||
.then(v => {
|
||||
resolve(v.toJSON({ omitPivot: true, visibility: !full }));
|
||||
})
|
||||
@@ -72,7 +72,24 @@ class UserRepository {
|
||||
"message": "L'utilisateur avec cet email n'a pas été trouvé.",
|
||||
"code": 404,
|
||||
});
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
getSpellsFromOne(uuid) {
|
||||
return new Promise((resolve, reject) => {
|
||||
new model()
|
||||
.where({ 'uuid': uuid })
|
||||
.fetch({ withRelated: [ 'role', 'spells.schools.meta_schools', 'spells.variables', 'spells.ingredients' ] })
|
||||
.then(v => {
|
||||
resolve(v.toJSON({ omitPivot: true }));
|
||||
})
|
||||
.catch(() => {
|
||||
reject({
|
||||
"message": "Les sorts liés à cet utilisateur n'ont pas été trouvés.",
|
||||
"code": 404,
|
||||
});
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,29 @@ router.get('/:uuid/', async (req, res) => {
|
||||
})
|
||||
})
|
||||
|
||||
// GET SPELLS FROM ONE ------------------
|
||||
const getSpellsFromUser = (uuid) => {
|
||||
return Users.getSpellsFromOne(uuid)
|
||||
.catch(err => {
|
||||
throw err
|
||||
})
|
||||
}
|
||||
router.get('/:uuid/spells', async (req, res) => {
|
||||
getSpellsFromUser(req.params.uuid)
|
||||
.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
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
// CHECK IF MAIL IS AVAILABLE ------------------
|
||||
const checkIfEmailAvailable = (mail) => {
|
||||
|
||||
Reference in New Issue
Block a user