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