- 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,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