From 96d13ec6e9de8dd410033638b53629e5fed96c5f Mon Sep 17 00:00:00 2001 From: Alexis <35.alexis.pele@gmail.com> Date: Mon, 22 Jun 2020 21:36:51 +0200 Subject: [PATCH] - Removed test data from db files, cleaned structures, and fix other things --- client/src/components/spells/spell-card.vue | 1 + database/auracle_db_create.sql | 2 - database/auracle_db_schools.sql | 14 +++- functions.js | 48 +++++++++++++ repositories/ingredient-repository.js | 29 ++------ repositories/school-repository.js | 29 ++------ repositories/spell-repository.js | 63 ++++++++++------ repositories/user-repository.js | 3 +- repositories/variable-repository.js | 29 ++------ routes/ingredients.js | 28 +------- routes/meta_schools.js | 29 +------- routes/schools.js | 28 +------- routes/spells.js | 79 ++++++++++++++------- routes/users.js | 3 - routes/variables.js | 28 +------- 15 files changed, 186 insertions(+), 227 deletions(-) create mode 100644 functions.js diff --git a/client/src/components/spells/spell-card.vue b/client/src/components/spells/spell-card.vue index 1bef45b..78cb59d 100644 --- a/client/src/components/spells/spell-card.vue +++ b/client/src/components/spells/spell-card.vue @@ -126,6 +126,7 @@ export default { @include colorschool(hydromancie,#3f68c7); @include colorschool(electromancie,#cd9731); @include colorschool(terramancie,#7e5540); + @include colorschool(sidéromancie,#58697a); @include colorschool(caelomancie,#a8a8a8); @include colorschool(légimancie,#5dbabd); @include colorschool(illusiomancie,#9f63a1); diff --git a/database/auracle_db_create.sql b/database/auracle_db_create.sql index fdbdb4b..79f45bd 100644 --- a/database/auracle_db_create.sql +++ b/database/auracle_db_create.sql @@ -114,8 +114,6 @@ BEGIN END$$ DELIMITER ; - - /* =========== PRIMARY INSERTS =========== */ SET NAMES utf8; USE auracle; diff --git a/database/auracle_db_schools.sql b/database/auracle_db_schools.sql index c13e2c2..07f9126 100644 --- a/database/auracle_db_schools.sql +++ b/database/auracle_db_schools.sql @@ -20,4 +20,16 @@ CALL insertIntoSchoolRange(116, 141, 7); CALL insertIntoSchoolRange(142, 167, 8); CALL insertIntoSchoolRange(168, 193, 9); 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); diff --git a/functions.js b/functions.js new file mode 100644 index 0000000..2fbdb92 --- /dev/null +++ b/functions.js @@ -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 +} \ No newline at end of file diff --git a/repositories/ingredient-repository.js b/repositories/ingredient-repository.js index ea226b4..e91aeb9 100644 --- a/repositories/ingredient-repository.js +++ b/repositories/ingredient-repository.js @@ -10,7 +10,8 @@ const IngredientValidation = require("../validations/IngredientValidation") v.addSchema(IngredientValidation, "/IngredientValidation") // Validations -const regexXSS = RegExp(/<[^>]*script/) +const isXSSAttempt = require('../functions').isXSSAttempt +const isEmptyObject = require('../functions').isEmptyObject // Error handling const { HttpError } = require('../validations/Errors') @@ -68,11 +69,11 @@ class IngredientRepository { addOne(igr) { return new Promise((resolve, reject) => { // 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 !")) } else if (!v.validate(igr, IngredientValidation).valid) { 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.')) } else { bookshelf.transaction(t => { @@ -103,11 +104,11 @@ class IngredientRepository { updateOne(id, igr) { return new Promise((resolve, reject) => { // 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 !")) } else if (!v.validate(igr, IngredientValidation).valid) { 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.')) } else { 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 \ No newline at end of file diff --git a/repositories/school-repository.js b/repositories/school-repository.js index f15d597..f9eae87 100644 --- a/repositories/school-repository.js +++ b/repositories/school-repository.js @@ -10,7 +10,8 @@ const SchoolValidation = require("../validations/SchoolValidation") v.addSchema(SchoolValidation, "/SchoolValidation") // Validations -const regexXSS = RegExp(/<[^>]*script/) +const isXSSAttempt = require('../functions').isXSSAttempt +const isEmptyObject = require('../functions').isEmptyObject // Error handling const { HttpError } = require('../validations/Errors') @@ -67,11 +68,11 @@ class SchoolRepository { addOne(s) { return new Promise((resolve, reject) => { // 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 !")) } 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)) { + } else if (isXSSAttempt(s.name) || isXSSAttempt(s.description)) { reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) } else { bookshelf.transaction(t => { @@ -103,11 +104,11 @@ class SchoolRepository { updateOne(id, s) { return new Promise((resolve, reject) => { // 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 !")) } 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)) { + } else if (isXSSAttempt(s.name) || isXSSAttempt(s.description)) { reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) } else { 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 \ No newline at end of file diff --git a/repositories/spell-repository.js b/repositories/spell-repository.js index b376e96..f694fde 100644 --- a/repositories/spell-repository.js +++ b/repositories/spell-repository.js @@ -10,7 +10,8 @@ const SpellValidation = require("../validations/SpellValidation") v.addSchema(SpellValidation, "/SpellValidation") // Validations -const regexXSS = RegExp(/<[^>]*script/) +const isXSSAttempt = require('../functions').isXSSAttempt +const isEmptyObject = require('../functions').isEmptyObject // Error handling 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) { return new Promise((resolve, reject) => { model.forge() @@ -52,11 +87,11 @@ class SpellRepository { addOne(s) { return new Promise((resolve, reject) => { // 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 !")) } 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)) { + } else if (isXSSAttempt(s.name) || isXSSAttempt(s.description) || isXSSAttempt(s.cost)) { reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) } else { bookshelf.transaction(t => { @@ -112,11 +147,11 @@ class SpellRepository { updateOne(id, s) { return new Promise((resolve, reject) => { // 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 !")) } 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)) { + } else if (isXSSAttempt(s.name) || isXSSAttempt(s.description) || isXSSAttempt(s.cost)) { reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) } else { 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 \ No newline at end of file diff --git a/repositories/user-repository.js b/repositories/user-repository.js index edf866a..3b77786 100644 --- a/repositories/user-repository.js +++ b/repositories/user-repository.js @@ -13,7 +13,8 @@ const UserValidation = require("../validations/UserValidation") v.addSchema(UserValidation, "/UserValidation") // Validations -const regexXSS = RegExp(/<[^>]*script/) +const isXSSAttempt = require('../functions').isXSSAttempt +const isEmptyObject = require('../functions').isEmptyObject // Error handling const { HttpError } = require('../validations/Errors') diff --git a/repositories/variable-repository.js b/repositories/variable-repository.js index 57bb530..2fc2a31 100644 --- a/repositories/variable-repository.js +++ b/repositories/variable-repository.js @@ -10,7 +10,8 @@ const VariableValidation = require("../validations/VariableValidation") v.addSchema(VariableValidation, "/VariableValidation") // Validations -const regexXSS = RegExp(/<[^>]*script/) +const isXSSAttempt = require('../functions').isXSSAttempt +const isEmptyObject = require('../functions').isEmptyObject // Error handling const { HttpError } = require('../validations/Errors') @@ -67,11 +68,11 @@ class VariableRepository { addOne(vr) { return new Promise((resolve, reject) => { // 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 !")) } else if (!v.validate(vr, VariableValidation).valid) { 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.')) } else { bookshelf.transaction(t => { @@ -101,11 +102,11 @@ class VariableRepository { updateOne(id, vr) { return new Promise((resolve, reject) => { // 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 !")) } else if (!v.validate(vr, VariableValidation).valid) { 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.')) } else { 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 \ No newline at end of file diff --git a/routes/ingredients.js b/routes/ingredients.js index 7748a8e..e51c348 100644 --- a/routes/ingredients.js +++ b/routes/ingredients.js @@ -6,17 +6,12 @@ let router = express.Router() // Connection const connection = require('../database/bookshelf') -const db = connection.db +const functions = require('../functions') // Repository const IngredientRepository = require('../repositories/ingredient-repository'); const Ingredients = new IngredientRepository(); -const regexInt = RegExp(/^[1-9]\d*$/) - -// Error handling -const { HttpError } = require('../validations/Errors') - // ROUTES // GET ALL ------------------ const getIngredients = () => { @@ -167,24 +162,7 @@ router.delete('/:id/', async (req, res) => { }) }) -// Param validation for ID -// (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 - }) - ) - } -}) +// Param validations +router.param('id', functions.paramIntCheck) module.exports = router \ No newline at end of file diff --git a/routes/meta_schools.js b/routes/meta_schools.js index 4639c36..8812891 100644 --- a/routes/meta_schools.js +++ b/routes/meta_schools.js @@ -6,17 +6,12 @@ let router = express.Router() // Connection const connection = require('../database/bookshelf') -const db = connection.db +const functions = require('../functions') // Repository const MetaSchoolRepository = require('../repositories/meta-school-repository'); const MetaSchools = new MetaSchoolRepository(); -const regexInt = RegExp(/^[1-9]\d*$/) - -// Error handling -const { HttpError } = require('../validations/Errors') - // ROUTES // GET ALL ------------------ const getMetaSchools = () => { @@ -67,25 +62,7 @@ router.get('/:id/', async (req, res) => { }) }) - -// Param validation for ID -// (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 - }) - ) - } -}) +// Param validations +router.param('id', functions.paramIntCheck) module.exports = router \ No newline at end of file diff --git a/routes/schools.js b/routes/schools.js index 1be0200..173af9c 100644 --- a/routes/schools.js +++ b/routes/schools.js @@ -6,17 +6,12 @@ let router = express.Router() // Connection const connection = require('../database/bookshelf') -const db = connection.db +const functions = require('../functions') // Repository const SchoolRepository = require('../repositories/school-repository'); const Schools = new SchoolRepository(); -const regexInt = RegExp(/^[1-9]\d*$/) - -// Error handling -const { HttpError } = require('../validations/Errors') - // ROUTES // GET ALL ------------------ const getSchools = () => { @@ -166,24 +161,7 @@ router.delete('/:id/', async (req, res) => { }) }) -// Param validation for ID -// (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 - }) - ) - } -}) +// Param validations +router.param('id', functions.paramIntCheck) module.exports = router \ No newline at end of file diff --git a/routes/spells.js b/routes/spells.js index 2d3ea4f..f0acaf0 100644 --- a/routes/spells.js +++ b/routes/spells.js @@ -6,18 +6,37 @@ let router = express.Router() // Connection const connection = require('../database/bookshelf') -const db = connection.db +const functions = require('../functions') // Repository const SpellReposity = require('../repositories/spell-repository'); const Spells = new SpellReposity(); -const regexInt = RegExp(/^[1-9]\d*$/) - -// Error handling -const { HttpError } = require('../validations/Errors') - // 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 ------------------ const getSpells = () => { return Spells.getAll() @@ -26,7 +45,7 @@ const getSpells = () => { throw err }) } -router.get('/', async (req, res) => { +router.get('/private', async (req, res) => { getSpells() .then(v => { 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 ------------------ const getSpell = (id) => { @@ -142,25 +184,8 @@ router.delete('/:id/', async (req, res) => { }) }) - -// Param validation for ID - // (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 - }) - ) - } -}) +// Param validations +router.param('id', functions.paramIntCheck) +router.param('page', functions.paramIntCheck) module.exports = router diff --git a/routes/users.js b/routes/users.js index 951aab9..2060af6 100644 --- a/routes/users.js +++ b/routes/users.js @@ -14,9 +14,6 @@ const Users = new UserRepository(); const regexInt = RegExp(/^[1-9]\d*$/) -// Error handling -const { HttpError } = require('../validations/Errors') - // ROUTES // GET ALL ------------------ const getUsers = () => { diff --git a/routes/variables.js b/routes/variables.js index 01ee961..15bfa39 100644 --- a/routes/variables.js +++ b/routes/variables.js @@ -6,17 +6,12 @@ let router = express.Router() // Connection const connection = require('../database/bookshelf') -const db = connection.db +const functions = require('../functions') // Repository const VariableRepository = require('../repositories/variable-repository'); const Variables = new VariableRepository(); -const regexInt = RegExp(/^[1-9]\d*$/) - -// Error handling -const { HttpError } = require('../validations/Errors') - // ROUTES // GET ALL ------------------ const getvariables = () => { @@ -167,24 +162,7 @@ router.delete('/:id/', async (req, res) => { }) }) -// Param validation for ID -// (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 - }) - ) - } -}) +// Param validations +router.param('id', functions.paramIntCheck) module.exports = router \ No newline at end of file