- Added A TON of stuff, separated everything neatly and cleaned most models
This commit is contained in:
@@ -1,57 +1,28 @@
|
||||
'use strict'
|
||||
|
||||
// Bookshelf
|
||||
const bookshelf = require('../database/connection').bookshelf
|
||||
|
||||
const SchoolRepository = require('./school-repository')
|
||||
const Schools = new SchoolRepository()
|
||||
|
||||
const IngredientRepository = require('./ingredient-repository')
|
||||
const Ingredients = new IngredientRepository()
|
||||
|
||||
const VariableRepository = require('./variable-repository')
|
||||
const Variables = new VariableRepository()
|
||||
const bookshelf = require('../database/bookshelf').bookshelf
|
||||
const model = require('../models/spell-model')
|
||||
|
||||
// Model validation
|
||||
const Validator = require('jsonschema').Validator
|
||||
const v = new Validator()
|
||||
const SpellModel = require("../models/SpellValidation")
|
||||
v.addSchema(SpellModel, "/SpellModel")
|
||||
const SpellValidation = require("../validations/SpellValidation")
|
||||
v.addSchema(SpellValidation, "/SpellValidation")
|
||||
|
||||
// Validations
|
||||
const regexXSS = RegExp(/<[^>]*script/)
|
||||
|
||||
// Error handling
|
||||
const { HttpError } = require('../models/Errors')
|
||||
const { HttpError } = require('../validations/Errors')
|
||||
|
||||
class SpellRepository {
|
||||
|
||||
constructor() {
|
||||
this.model = bookshelf.Model.extend({
|
||||
tableName: 'spell',
|
||||
schools() {
|
||||
return this.belongsToMany( Schools._model, 'spell_school', 'spell_id', 'school_id' )
|
||||
},
|
||||
variables() {
|
||||
return this.belongsToMany( Variables._model, 'spell_variable', 'spell_id', 'variable_id' )
|
||||
},
|
||||
ingredients() {
|
||||
return this.belongsToMany( Ingredients._model, 'spell_ingredient', 'spell_id', 'ingredient_id' )
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
set model(model) {
|
||||
this._model = model
|
||||
}
|
||||
|
||||
get model() {
|
||||
return this._model
|
||||
}
|
||||
|
||||
getAll() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._model.forge()
|
||||
model.forge()
|
||||
.fetchAll({ withRelated: ['schools.meta_schools', 'variables', 'ingredients'] })
|
||||
.then(v => {
|
||||
resolve(v.toJSON({ omitPivot: true }))
|
||||
@@ -65,7 +36,7 @@ class SpellRepository {
|
||||
|
||||
getOne(id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._model.forge()
|
||||
model.forge()
|
||||
.where({ 'id' : id })
|
||||
.fetch({ withRelated: ['schools.meta_schools', 'variables', 'ingredients']})
|
||||
.then(v => {
|
||||
@@ -83,13 +54,13 @@ class SpellRepository {
|
||||
// Checks if body exists and if the model fits, and throws errors if it doesn't
|
||||
if (this.isEmptyObject(s)) {
|
||||
reject(new HttpError(403, "Error: Spell cannot be nothing !"))
|
||||
} else if (!v.validate(s, SpellModel).valid) {
|
||||
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SpellModel).errors))
|
||||
} else if (!v.validate(s, SpellValidation).valid) {
|
||||
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SpellValidation).errors))
|
||||
} else if (this.isXSSAttempt(s.name) || this.isXSSAttempt(s.description) || this.isXSSAttempt(s.cost)) {
|
||||
reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
|
||||
} else {
|
||||
bookshelf.transaction(t => {
|
||||
return this._model.forge({
|
||||
return model.forge({
|
||||
'name': s.name,
|
||||
'description': s.description,
|
||||
'level': s.level,
|
||||
@@ -143,12 +114,12 @@ class SpellRepository {
|
||||
// Checks if body exists and if the model fits, and throws errors if it doesn't
|
||||
if (this.isEmptyObject(s)) {
|
||||
reject(new HttpError(403, "Error: Spell cannot be nothing !"))
|
||||
} else if (!v.validate(s, SpellModel).valid) {
|
||||
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SpellModel).errors))
|
||||
} else if (!v.validate(s, SpellValidation).valid) {
|
||||
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SpellValidation).errors))
|
||||
} else if (this.isXSSAttempt(s.name) || this.isXSSAttempt(s.description) || this.isXSSAttempt(s.cost)) {
|
||||
reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
|
||||
} else {
|
||||
this._model.forge({id: id})
|
||||
model.forge({id: id})
|
||||
.fetch({require: true, withRelated: ['schools.meta_schools', 'variables', 'ingredients']})
|
||||
.then(v => {
|
||||
bookshelf.transaction(t => {
|
||||
@@ -231,14 +202,14 @@ class SpellRepository {
|
||||
|
||||
deleteOne(id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._model.forge()
|
||||
model.forge()
|
||||
.where({ 'id' : id })
|
||||
.fetch({require: true, withRelated: ['schools.meta_schools', 'variables', 'ingredients']})
|
||||
.then(v => {
|
||||
v.schools().detach();
|
||||
v.variables().detach();
|
||||
v.ingredients().detach();
|
||||
v.destroy();
|
||||
v.schools().detach()
|
||||
v.variables().detach()
|
||||
v.ingredients().detach()
|
||||
v.destroy()
|
||||
})
|
||||
.then(() => {
|
||||
resolve({
|
||||
|
||||
Reference in New Issue
Block a user