- Removed test data from db files, cleaned structures, and fix other things

This commit is contained in:
Alexis
2020-06-22 21:36:51 +02:00
parent 49bddf0939
commit 96d13ec6e9
15 changed files with 186 additions and 227 deletions

View File

@@ -126,6 +126,7 @@ export default {
@include colorschool(hydromancie,#3f68c7); @include colorschool(hydromancie,#3f68c7);
@include colorschool(electromancie,#cd9731); @include colorschool(electromancie,#cd9731);
@include colorschool(terramancie,#7e5540); @include colorschool(terramancie,#7e5540);
@include colorschool(sidéromancie,#58697a);
@include colorschool(caelomancie,#a8a8a8); @include colorschool(caelomancie,#a8a8a8);
@include colorschool(légimancie,#5dbabd); @include colorschool(légimancie,#5dbabd);
@include colorschool(illusiomancie,#9f63a1); @include colorschool(illusiomancie,#9f63a1);

View File

@@ -114,8 +114,6 @@ BEGIN
END$$ END$$
DELIMITER ; DELIMITER ;
/* =========== PRIMARY INSERTS =========== */ /* =========== PRIMARY INSERTS =========== */
SET NAMES utf8; SET NAMES utf8;
USE auracle; USE auracle;

View File

@@ -20,4 +20,16 @@ CALL insertIntoSchoolRange(116, 141, 7);
CALL insertIntoSchoolRange(142, 167, 8); CALL insertIntoSchoolRange(142, 167, 8);
CALL insertIntoSchoolRange(168, 193, 9); CALL insertIntoSchoolRange(168, 193, 9);
CALL insertIntoSchoolRange(194, 217, 10); CALL insertIntoSchoolRange(194, 217, 10);
CALL insertIntoSchoolRange(218, 220, 11);
CALL insertIntoSchoolRange(221, 242, 12);
CALL insertIntoSchoolRange(243, 257, 13);
CALL insertIntoSchoolRange(258, 272, 14);
CALL insertIntoSchoolRange(273, 283, 15);
CALL insertIntoSchoolRange(284, 301, 16);
CALL insertIntoSchoolRange(302, 322, 17);
CALL insertIntoSchoolRange(323, 339, 19);
CALL insertIntoSchoolRange(340, 341, 20);
CALL insertIntoSchoolRange(342, 356, 21);
CALL insertIntoSchoolRange(357, 387, 22);
CALL insertIntoSchoolRange(388, 396, 24);
CALL insertIntoSchoolRange(397, 403, 25);

48
functions.js Normal file
View File

@@ -0,0 +1,48 @@
// Error handling
const { HttpError } = require('./validations/Errors')
const regexInt = RegExp(/^[1-9]\d*$/)
const regexXSS = RegExp(/<[^>]*script/)
// Check if int for param validation
const paramIntCheck = (req, res, next, input) => {
try {
if (regexInt.test(input)) {
next()
} else {
throw new Error
}
} catch (err) {
err = new HttpError(403, 'Provided ID must be an integer and not zero')
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
)
}
}
// Check if script injection attempt
const isXSSAttempt = (string) => {
if (regexXSS.test(string)) {
return true
} else {
return false
}
}
// Check if object is null
const isEmptyObject = (obj) => {
if (Object.keys(obj).length === 0 && obj.constructor === Object) {
return true
} else {
return false
}
}
module.exports = {
paramIntCheck,
isXSSAttempt,
isEmptyObject
}

View File

@@ -10,7 +10,8 @@ const IngredientValidation = require("../validations/IngredientValidation")
v.addSchema(IngredientValidation, "/IngredientValidation") v.addSchema(IngredientValidation, "/IngredientValidation")
// Validations // Validations
const regexXSS = RegExp(/<[^>]*script/) const isXSSAttempt = require('../functions').isXSSAttempt
const isEmptyObject = require('../functions').isEmptyObject
// Error handling // Error handling
const { HttpError } = require('../validations/Errors') const { HttpError } = require('../validations/Errors')
@@ -68,11 +69,11 @@ class IngredientRepository {
addOne(igr) { addOne(igr) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't // Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(igr)) { if (isEmptyObject(igr)) {
reject(new HttpError(403, "Error: Ingredient cannot be nothing !")) reject(new HttpError(403, "Error: Ingredient cannot be nothing !"))
} else if (!v.validate(igr, IngredientValidation).valid) { } else if (!v.validate(igr, IngredientValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(igr, IngredientValidation).errors)) reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(igr, IngredientValidation).errors))
} else if (this.isXSSAttempt(igr.description)) { } else if (isXSSAttempt(igr.description)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else { } else {
bookshelf.transaction(t => { bookshelf.transaction(t => {
@@ -103,11 +104,11 @@ class IngredientRepository {
updateOne(id, igr) { updateOne(id, igr) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't // Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(igr)) { if (isEmptyObject(igr)) {
reject(new HttpError(403, "Error: Ingredient cannot be nothing !")) reject(new HttpError(403, "Error: Ingredient cannot be nothing !"))
} else if (!v.validate(igr, IngredientValidation).valid) { } else if (!v.validate(igr, IngredientValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(igr, IngredientValidation).errors)) reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(igr, IngredientValidation).errors))
} else if (this.isXSSAttempt(igr.description)) { } else if (isXSSAttempt(igr.description)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else { } else {
model.forge({id: id}) model.forge({id: id})
@@ -165,24 +166,6 @@ class IngredientRepository {
}) })
}) })
} }
// 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 = IngredientRepository module.exports = IngredientRepository

View File

@@ -10,7 +10,8 @@ const SchoolValidation = require("../validations/SchoolValidation")
v.addSchema(SchoolValidation, "/SchoolValidation") v.addSchema(SchoolValidation, "/SchoolValidation")
// Validations // Validations
const regexXSS = RegExp(/<[^>]*script/) const isXSSAttempt = require('../functions').isXSSAttempt
const isEmptyObject = require('../functions').isEmptyObject
// Error handling // Error handling
const { HttpError } = require('../validations/Errors') const { HttpError } = require('../validations/Errors')
@@ -67,11 +68,11 @@ class SchoolRepository {
addOne(s) { addOne(s) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't // Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(s)) { if (isEmptyObject(s)) {
reject(new HttpError(403, "Error: School cannot be nothing !")) reject(new HttpError(403, "Error: School cannot be nothing !"))
} else if (!v.validate(s, SchoolValidation).valid) { } else if (!v.validate(s, SchoolValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SchoolValidation).errors)) reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SchoolValidation).errors))
} else if (this.isXSSAttempt(s.name) || this.isXSSAttempt(s.description)) { } else if (isXSSAttempt(s.name) || isXSSAttempt(s.description)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else { } else {
bookshelf.transaction(t => { bookshelf.transaction(t => {
@@ -103,11 +104,11 @@ class SchoolRepository {
updateOne(id, s) { updateOne(id, s) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't // Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(s)) { if (isEmptyObject(s)) {
reject(new HttpError(403, "Error: School cannot be nothing !")) reject(new HttpError(403, "Error: School cannot be nothing !"))
} else if (!v.validate(s, SchoolValidation).valid) { } else if (!v.validate(s, SchoolValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SchoolValidation).errors)) reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SchoolValidation).errors))
} else if (this.isXSSAttempt(s.name) || this.isXSSAttempt(s.description)) { } else if (isXSSAttempt(s.name) || isXSSAttempt(s.description)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else { } else {
model.forge({id: id}) model.forge({id: id})
@@ -166,24 +167,6 @@ class SchoolRepository {
}) })
}) })
} }
// 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 = SchoolRepository module.exports = SchoolRepository

View File

@@ -10,7 +10,8 @@ const SpellValidation = require("../validations/SpellValidation")
v.addSchema(SpellValidation, "/SpellValidation") v.addSchema(SpellValidation, "/SpellValidation")
// Validations // Validations
const regexXSS = RegExp(/<[^>]*script/) const isXSSAttempt = require('../functions').isXSSAttempt
const isEmptyObject = require('../functions').isEmptyObject
// Error handling // Error handling
const { HttpError } = require('../validations/Errors') const { HttpError } = require('../validations/Errors')
@@ -34,6 +35,40 @@ class SpellRepository {
}) })
} }
getAllPublic() {
return new Promise((resolve, reject) => {
model.forge()
.where({ 'public' : 1 })
.fetchAll({ withRelated: ['schools.meta_schools', 'variables', 'ingredients'] })
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject(new HttpError(500, "Couldn't get public spells"))
})
})
}
getPage(page) {
return new Promise((resolve, reject) => {
model.forge()
.where({ 'public' : 1 })
.fetchPage({
pageSize: 20,
page: page,
withRelated: ['schools.meta_schools', 'variables', 'ingredients'],
})
.then(v => {
resolve(v.toJSON({ omitPivot: true }))
})
.catch(err => {
console.log(err)
reject(new HttpError(500, "Couldn't get public spells"))
})
})
}
getOne(id) { getOne(id) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
model.forge() model.forge()
@@ -52,11 +87,11 @@ class SpellRepository {
addOne(s) { addOne(s) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't // Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(s)) { if (isEmptyObject(s)) {
reject(new HttpError(403, "Error: Spell cannot be nothing !")) reject(new HttpError(403, "Error: Spell cannot be nothing !"))
} else if (!v.validate(s, SpellValidation).valid) { } else if (!v.validate(s, SpellValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SpellValidation).errors)) 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)) { } else if (isXSSAttempt(s.name) || isXSSAttempt(s.description) || isXSSAttempt(s.cost)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else { } else {
bookshelf.transaction(t => { bookshelf.transaction(t => {
@@ -112,11 +147,11 @@ class SpellRepository {
updateOne(id, s) { updateOne(id, s) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't // Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(s)) { if (isEmptyObject(s)) {
reject(new HttpError(403, "Error: Spell cannot be nothing !")) reject(new HttpError(403, "Error: Spell cannot be nothing !"))
} else if (!v.validate(s, SpellValidation).valid) { } else if (!v.validate(s, SpellValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SpellValidation).errors)) 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)) { } else if (isXSSAttempt(s.name) || isXSSAttempt(s.description) || isXSSAttempt(s.cost)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else { } else {
model.forge({id: id}) model.forge({id: id})
@@ -222,24 +257,6 @@ class SpellRepository {
}) })
}) })
} }
// 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 = SpellRepository module.exports = SpellRepository

View File

@@ -13,7 +13,8 @@ const UserValidation = require("../validations/UserValidation")
v.addSchema(UserValidation, "/UserValidation") v.addSchema(UserValidation, "/UserValidation")
// Validations // Validations
const regexXSS = RegExp(/<[^>]*script/) const isXSSAttempt = require('../functions').isXSSAttempt
const isEmptyObject = require('../functions').isEmptyObject
// Error handling // Error handling
const { HttpError } = require('../validations/Errors') const { HttpError } = require('../validations/Errors')

View File

@@ -10,7 +10,8 @@ const VariableValidation = require("../validations/VariableValidation")
v.addSchema(VariableValidation, "/VariableValidation") v.addSchema(VariableValidation, "/VariableValidation")
// Validations // Validations
const regexXSS = RegExp(/<[^>]*script/) const isXSSAttempt = require('../functions').isXSSAttempt
const isEmptyObject = require('../functions').isEmptyObject
// Error handling // Error handling
const { HttpError } = require('../validations/Errors') const { HttpError } = require('../validations/Errors')
@@ -67,11 +68,11 @@ class VariableRepository {
addOne(vr) { addOne(vr) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't // Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(vr)) { if (isEmptyObject(vr)) {
reject(new HttpError(403, "Error: Variable cannot be nothing !")) reject(new HttpError(403, "Error: Variable cannot be nothing !"))
} else if (!v.validate(vr, VariableValidation).valid) { } else if (!v.validate(vr, VariableValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(vr, VariableValidation).errors)) reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(vr, VariableValidation).errors))
} else if (this.isXSSAttempt(vr.description)) { } else if (isXSSAttempt(vr.description)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else { } else {
bookshelf.transaction(t => { bookshelf.transaction(t => {
@@ -101,11 +102,11 @@ class VariableRepository {
updateOne(id, vr) { updateOne(id, vr) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Checks if body exists and if the model fits, and throws errors if it doesn't // Checks if body exists and if the model fits, and throws errors if it doesn't
if (this.isEmptyObject(vr)) { if (isEmptyObject(vr)) {
reject(new HttpError(403, "Error: Variable cannot be nothing !")) reject(new HttpError(403, "Error: Variable cannot be nothing !"))
} else if (!v.validate(vr, VariableValidation).valid) { } else if (!v.validate(vr, VariableValidation).valid) {
reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(vr, VariableValidation).errors)) reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(vr, VariableValidation).errors))
} else if (this.isXSSAttempt(vr.description)) { } else if (isXSSAttempt(vr.description)) {
reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
} else { } else {
model.forge({id: id}) model.forge({id: id})
@@ -162,24 +163,6 @@ class VariableRepository {
}) })
}) })
} }
// 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 = VariableRepository module.exports = VariableRepository

View File

@@ -6,17 +6,12 @@ let router = express.Router()
// Connection // Connection
const connection = require('../database/bookshelf') const connection = require('../database/bookshelf')
const db = connection.db const functions = require('../functions')
// Repository // Repository
const IngredientRepository = require('../repositories/ingredient-repository'); const IngredientRepository = require('../repositories/ingredient-repository');
const Ingredients = new IngredientRepository(); const Ingredients = new IngredientRepository();
const regexInt = RegExp(/^[1-9]\d*$/)
// Error handling
const { HttpError } = require('../validations/Errors')
// ROUTES // ROUTES
// GET ALL ------------------ // GET ALL ------------------
const getIngredients = () => { const getIngredients = () => {
@@ -167,24 +162,7 @@ router.delete('/:id/', async (req, res) => {
}) })
}) })
// Param validation for ID // Param validations
// (check if id is int) (could be refactored) router.param('id', functions.paramIntCheck)
router.param('id', (req, res, next, id) => {
try {
if (regexInt.test(id)) {
next()
} else {
throw new Error;
}
} catch (err) {
err = new HttpError(403, 'Provided ID must be an integer and not zero')
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
)
}
})
module.exports = router module.exports = router

View File

@@ -6,17 +6,12 @@ let router = express.Router()
// Connection // Connection
const connection = require('../database/bookshelf') const connection = require('../database/bookshelf')
const db = connection.db const functions = require('../functions')
// Repository // Repository
const MetaSchoolRepository = require('../repositories/meta-school-repository'); const MetaSchoolRepository = require('../repositories/meta-school-repository');
const MetaSchools = new MetaSchoolRepository(); const MetaSchools = new MetaSchoolRepository();
const regexInt = RegExp(/^[1-9]\d*$/)
// Error handling
const { HttpError } = require('../validations/Errors')
// ROUTES // ROUTES
// GET ALL ------------------ // GET ALL ------------------
const getMetaSchools = () => { const getMetaSchools = () => {
@@ -67,25 +62,7 @@ router.get('/:id/', async (req, res) => {
}) })
}) })
// Param validations
// Param validation for ID router.param('id', functions.paramIntCheck)
// (check if id is int) (could be refactored)
router.param('id', (req, res, next, id) => {
try {
if (regexInt.test(id)) {
next()
} else {
throw new Error;
}
} catch (err) {
err = new HttpError(403, 'Provided ID must be an integer and not zero')
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
)
}
})
module.exports = router module.exports = router

View File

@@ -6,17 +6,12 @@ let router = express.Router()
// Connection // Connection
const connection = require('../database/bookshelf') const connection = require('../database/bookshelf')
const db = connection.db const functions = require('../functions')
// Repository // Repository
const SchoolRepository = require('../repositories/school-repository'); const SchoolRepository = require('../repositories/school-repository');
const Schools = new SchoolRepository(); const Schools = new SchoolRepository();
const regexInt = RegExp(/^[1-9]\d*$/)
// Error handling
const { HttpError } = require('../validations/Errors')
// ROUTES // ROUTES
// GET ALL ------------------ // GET ALL ------------------
const getSchools = () => { const getSchools = () => {
@@ -166,24 +161,7 @@ router.delete('/:id/', async (req, res) => {
}) })
}) })
// Param validation for ID // Param validations
// (check if id is int) (could be refactored) router.param('id', functions.paramIntCheck)
router.param('id', (req, res, next, id) => {
try {
if (regexInt.test(id)) {
next()
} else {
throw new Error;
}
} catch (err) {
err = new HttpError(403, 'Provided ID must be an integer and not zero')
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
)
}
})
module.exports = router module.exports = router

View File

@@ -6,18 +6,37 @@ let router = express.Router()
// Connection // Connection
const connection = require('../database/bookshelf') const connection = require('../database/bookshelf')
const db = connection.db const functions = require('../functions')
// Repository // Repository
const SpellReposity = require('../repositories/spell-repository'); const SpellReposity = require('../repositories/spell-repository');
const Spells = new SpellReposity(); const Spells = new SpellReposity();
const regexInt = RegExp(/^[1-9]\d*$/)
// Error handling
const { HttpError } = require('../validations/Errors')
// ROUTES // ROUTES
// GET ALL PUBLIC ------------------
const getPublicSpells = () => {
return Spells.getAllPublic()
.catch(err => {
console.log(err)
throw err
})
}
router.get('/', async (req, res) => {
getPublicSpells()
.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
})
)
})
})
// GET ALL ------------------ // GET ALL ------------------
const getSpells = () => { const getSpells = () => {
return Spells.getAll() return Spells.getAll()
@@ -26,7 +45,7 @@ const getSpells = () => {
throw err throw err
}) })
} }
router.get('/', async (req, res) => { router.get('/private', async (req, res) => {
getSpells() getSpells()
.then(v => { .then(v => {
res.setHeader('Content-Type', 'application/json;charset=utf-8') res.setHeader('Content-Type', 'application/json;charset=utf-8')
@@ -42,6 +61,29 @@ router.get('/', async (req, res) => {
}) })
}) })
// GET SOME ------------------
const getSomeSpells = (page) => {
return Spells.getPage(page)
.catch(err => {
console.log(err)
throw err
})
}
router.get('/page/:page', async (req, res) => {
getSomeSpells(req.params.page)
.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
})
)
})
})
// GET ONE ------------------ // GET ONE ------------------
const getSpell = (id) => { const getSpell = (id) => {
@@ -142,25 +184,8 @@ router.delete('/:id/', async (req, res) => {
}) })
}) })
// Param validations
// Param validation for ID router.param('id', functions.paramIntCheck)
// (check if id is int) (could be refactored) router.param('page', functions.paramIntCheck)
router.param('id', (req, res, next, id) => {
try {
if (regexInt.test(id)) {
next()
} else {
throw new Error;
}
} catch (err) {
err = new HttpError(403, 'Provided ID must be an integer and not zero')
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
)
}
})
module.exports = router module.exports = router

View File

@@ -14,9 +14,6 @@ const Users = new UserRepository();
const regexInt = RegExp(/^[1-9]\d*$/) const regexInt = RegExp(/^[1-9]\d*$/)
// Error handling
const { HttpError } = require('../validations/Errors')
// ROUTES // ROUTES
// GET ALL ------------------ // GET ALL ------------------
const getUsers = () => { const getUsers = () => {

View File

@@ -6,17 +6,12 @@ let router = express.Router()
// Connection // Connection
const connection = require('../database/bookshelf') const connection = require('../database/bookshelf')
const db = connection.db const functions = require('../functions')
// Repository // Repository
const VariableRepository = require('../repositories/variable-repository'); const VariableRepository = require('../repositories/variable-repository');
const Variables = new VariableRepository(); const Variables = new VariableRepository();
const regexInt = RegExp(/^[1-9]\d*$/)
// Error handling
const { HttpError } = require('../validations/Errors')
// ROUTES // ROUTES
// GET ALL ------------------ // GET ALL ------------------
const getvariables = () => { const getvariables = () => {
@@ -167,24 +162,7 @@ router.delete('/:id/', async (req, res) => {
}) })
}) })
// Param validation for ID // Param validations
// (check if id is int) (could be refactored) router.param('id', functions.paramIntCheck)
router.param('id', (req, res, next, id) => {
try {
if (regexInt.test(id)) {
next()
} else {
throw new Error;
}
} catch (err) {
err = new HttpError(403, 'Provided ID must be an integer and not zero')
res.status(err.code).send(JSON.stringify(
{
"error": err.message,
"code": err.code
})
)
}
})
module.exports = router module.exports = router