- Added A TON of stuff, separated everything neatly and cleaned most models

This commit is contained in:
Alexis
2020-06-08 19:55:36 +02:00
parent ce9018f65e
commit 48987b8349
28 changed files with 602 additions and 171 deletions

View File

@@ -1,27 +1,55 @@
'use strict'
// Bookshelf
const bookshelf = require('../database/connection').bookshelf
const bookshelf = require('../database/bookshelf').bookshelf
const model = require('../models/ingredient-model')
const Spells = require('./spell-repository')
// Model validation
const Validator = require('jsonschema').Validator
const v = new Validator()
const IngredientValidation = require("../validations/IngredientValidation")
v.addSchema(IngredientValidation, "/IngredientValidation")
// Validations
const regexXSS = RegExp(/<[^>]*script/)
// Error handling
const { HttpError } = require('../validations/Errors')
class IngredientRepository {
constructor() {
this.model = bookshelf.Model.extend({
tableName: 'ingredient',
spells() {
return this.belongsToMany( Spells._model, 'spell_ingredient', 'ingredient_id', 'spell_id')
}
}
getAll() {
return new Promise((resolve, reject) => {
model.forge()
.fetchAll({ withRelated: ['spells'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject(new HttpError(500, "Couldn't get ingredients"))
})
})
}
set model(model) {
this._model = model
getOne(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"))
})
})
}
get model() {
return this._model
}
}
module.exports = IngredientRepository

View File

@@ -1,27 +1,72 @@
'use strict'
// Bookshelf
const bookshelf = require('../database/connection').bookshelf
const bookshelf = require('../database/bookshelf').bookshelf
const model = require('../models/meta-school-model')
const Schools = require('./school-repository')
// Model validation
const Validator = require('jsonschema').Validator
const v = new Validator()
const MetaSchoolValidation = require("../validations/MetaSchoolValidation")
v.addSchema(MetaSchoolValidation, "/MetaSchoolValidation")
// Validations
const regexXSS = RegExp(/<[^>]*script/)
// Error handling
const { HttpError } = require('../validations/Errors')
class MetaSchoolRepository {
constructor() {
this.model = bookshelf.Model.extend({
tableName: 'meta_school',
schools() {
return this.hasMany( Schools._model )
}
}
getAll() {
return new Promise((resolve, reject) => {
model.forge()
.fetchAll({ withRelated: ['schools'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject(new HttpError(500, "Couldn't get meta schools"))
})
})
}
set model(model) {
this._model = model
getOne(id) {
return new Promise((resolve, reject) => {
model.forge()
.where({ 'id' : id })
.fetch({ withRelated: ['schools']})
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject(new HttpError(500, "Couldn't get meta school"))
})
})
}
get model() {
return this._model
// Check if object is null
isEmptyObject = (obj) => {
if (Object.keys(obj).length === 0 && obj.constructor === Object) {
return true
} else {
return false
}
}
// Check if script injection attempt
isXSSAttempt = (string) => {
if (regexXSS.test(string)) {
return true
} else {
return false
}
}
}
module.exports = MetaSchoolRepository

View File

@@ -1,49 +1,28 @@
'use strict'
// Bookshelf
const bookshelf = require('../database/connection').bookshelf
const Spells = require('./spell-repository')
const MetaSchoolRepository = require('./meta-school-repository')
const MetaSchools = new MetaSchoolRepository()
const bookshelf = require('../database/bookshelf').bookshelf
const model = require('../models/school-model')
// Model validation
const Validator = require('jsonschema').Validator
const v = new Validator()
const SchoolModel = require("../models/SchoolValidation")
v.addSchema(SchoolModel, "/SchoolModel")
const SchoolValidation = require("../validations/SchoolValidation")
v.addSchema(SchoolValidation, "/SchoolValidation")
// Validations
const regexXSS = RegExp(/<[^>]*script/)
// Error handling
const { HttpError } = require('../models/Errors')
const { HttpError } = require('../validations/Errors')
class SchoolRepository {
constructor() {
this.model = bookshelf.Model.extend({
tableName: 'school',
spells() {
return this.belongsToMany( Spells._model, 'spell_school', 'school_id', 'spell_id')
},
meta_schools() {
return this.belongsTo( MetaSchools._model, 'meta_school_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: ['meta_schools'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
@@ -57,7 +36,7 @@ class SchoolRepository {
getOne(id) {
return new Promise((resolve, reject) => {
this._model.forge()
model.forge()
.where({ 'id' : id })
.fetch({ withRelated: ['meta_schools']})
.then(v => {
@@ -75,13 +54,13 @@ class SchoolRepository {
// 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: School cannot be nothing !"))
} else if (!v.validate(s, SchoolModel).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SchoolModel).errors))
} else if (!v.validate(s, SchoolValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SchoolValidation).errors))
} else if (this.isXSSAttempt(s.name) || this.isXSSAttempt(s.description)) {
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,
'meta_school_id': s.meta_school_id,
@@ -111,12 +90,12 @@ class SchoolRepository {
// 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: School cannot be nothing !"))
} else if (!v.validate(s, SchoolModel).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SchoolModel).errors))
} else if (!v.validate(s, SchoolValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SchoolValidation).errors))
} else if (this.isXSSAttempt(s.name) || this.isXSSAttempt(s.description)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else {
this._model.forge({id: id})
model.forge({id: id})
.fetch({require: true, withRelated: ['meta_schools']})
.then(v => {
bookshelf.transaction(t => {
@@ -154,11 +133,12 @@ class SchoolRepository {
deleteOne(id) {
return new Promise((resolve, reject) => {
this._model.forge()
model.forge()
.where({ 'id' : id })
.fetch({require: true, withRelated: ['spells', 'meta_schools']})
.then(v => {
v.spells().detach()
v.destroy()
})
.then(() => {
resolve({

View File

@@ -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({

View File

@@ -1,7 +1,7 @@
'use strict'
// Bookshelf
const bookshelf = require('../database/connection').bookshelf
const bookshelf = require('../database/bookshelf').bookshelf
const model = require('../models/user-model')
// Hashing and passwords
const bcrypt = require('bcrypt')
@@ -9,29 +9,18 @@ const bcrypt = require('bcrypt')
// Model validation
const Validator = require('jsonschema').Validator
const v = new Validator()
const UserModel = require("../models/UserValidation")
v.addSchema(UserModel, "/UserModel")
const UserValidation = require("../validations/UserValidation")
v.addSchema(UserValidation, "/UserValidation")
// Validations
const regexInt = RegExp(/^[1-9]\d*$/)
const regexXSS = RegExp(/<[^>]*script/)
// Error handling
const { HttpError } = require('../models/Errors')
const { HttpError } = require('../validations/Errors')
class UserRepository {
constructor() {
this.model = bookshelf.Model.extend({
tableName: 'user',
})
}
set model(model) {
this._model = model
}
get model() {
return this._model
constructor() {
}
getAll() {

View File

@@ -1,26 +1,47 @@
'use strict'
// Bookshelf
const bookshelf = require('../database/connection').bookshelf
const bookshelf = require('../database/bookshelf').bookshelf
const model = require('../models/variable-model')
const Spells = require('./spell-repository')
// Model validation
const Validator = require('jsonschema').Validator
const v = new Validator()
const VariableValidation = require("../validations/VariableValidation")
v.addSchema(VariableValidation, "/VariableValidation")
class VariableRepository {
constructor() {
this.model = bookshelf.Model.extend({
tableName: 'variable',
spells() {
return this.belongsToMany( Spells._model, 'spell_variable', 'variable_id', 'spell_id')
}
}
getAll() {
return new Promise((resolve, reject) => {
model.forge()
.fetchAll({ withRelated: ['spells'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject(new HttpError(500, "Couldn't get variables"))
})
})
}
set model(model) {
this._model = model
}
get model() {
return this._model
getOne(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"))
})
})
}
}