Merge pull request #38 from AlexisNP/issues/user_spells

Added getSpellsFromUser() method
This commit is contained in:
Joururi
2021-01-01 22:13:06 +01:00
committed by GitHub
5 changed files with 60 additions and 12 deletions

View File

@@ -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' );
}
})

View File

@@ -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)

View File

@@ -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();

View File

@@ -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,
});
});
})
}

View File

@@ -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) => {