- Worked on spell things
This commit is contained in:
270
routes/spells.js
270
routes/spells.js
@@ -73,12 +73,18 @@ const { HttpError } = require('../models/Errors')
|
||||
const getSpells = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
Spell.forge().fetchAll({ withRelated: ['schools.meta_school', 'variables', 'ingredients'] })
|
||||
Spell.forge()
|
||||
.fetchAll({ withRelated: ['schools.meta_school', 'variables', 'ingredients'] })
|
||||
.then(v => {
|
||||
resolve(v.toJSON({ omitPivot: true }))
|
||||
})
|
||||
.catch(err => {
|
||||
reject(new HttpError(500, err.message))
|
||||
console.log(err)
|
||||
if (err.message == "EmptyResponse") {
|
||||
reject(new HttpError(404, "No spells matching this id were found"))
|
||||
} else {
|
||||
reject(new HttpError(500, err.message))
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
@@ -107,19 +113,16 @@ router.get('/', async (req, res) => {
|
||||
const getSpell = (id) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
let query = "SELECT * FROM spell WHERE id = " + db.escape(id)
|
||||
|
||||
db.query(query, async (err, result) => {
|
||||
if (err) {
|
||||
reject(new HttpError(500, 'Database error'))
|
||||
}
|
||||
try {
|
||||
result = buildSpell(result[0])
|
||||
resolve(result)
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
}
|
||||
Spell.forge()
|
||||
.where({ 'id' : id })
|
||||
.fetch({ withRelated: ['schools.meta_school', 'variables', 'ingredients'] })
|
||||
.then(v => {
|
||||
resolve(v.toJSON({ omitPivot: true }))
|
||||
})
|
||||
.catch(err => {
|
||||
reject(new HttpError(500, err.message))
|
||||
})
|
||||
|
||||
})
|
||||
.catch(err => {
|
||||
throw err
|
||||
@@ -154,124 +157,131 @@ const addSpell = (s) => {
|
||||
} else if (isXSSAttempt(s.name) || isXSSAttempt(s.description) || isXSSAttempt(s.cost)) {
|
||||
reject(new HttpError(403, 'Injection attempt detected, aborting the request.'))
|
||||
} else {
|
||||
let query =
|
||||
'INSERT INTO spell (name, description'
|
||||
|
||||
if (s.level !== null) { query += ', level' }
|
||||
if (s.charge !== null) { query += ', charge' }
|
||||
if (s.cost !== null) { query += ', cost' }
|
||||
if (s.is_ritual !== null) { query += ', is_ritual' }
|
||||
|
||||
query += `) VALUES (${db.escape(s.name)}, ${db.escape(s.description)}`
|
||||
|
||||
if (s.level !== null) { query += `, ${s.level}` }
|
||||
if (s.charge !== null) { query += `, ${s.charge}` }
|
||||
if (s.cost !== null) { query += `, ${db.escape(s.cost)}` }
|
||||
if (s.is_ritual !== null) { query += `, ${s.is_ritual}` }
|
||||
|
||||
query += ')'
|
||||
|
||||
db.query(query, async (err, result) => {
|
||||
if (err) {
|
||||
reject(new HttpError(500, 'Database error'))
|
||||
}
|
||||
console.log(`Inserted "${s.name}" with ID ${result.insertId}, affecting ${result.affectedRows} row(s)`)
|
||||
|
||||
const new_spell_id = result.insertId
|
||||
|
||||
let addSchoolsData = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (s.schools !== null) {
|
||||
if (s.schools.length > 0) {
|
||||
for (let i = 0; i < s.schools.length; i++) {
|
||||
if (!regexInt.test(s.schools[i].id)) {
|
||||
reject(new HttpError(403, 'Query error - School ID should be an integer !'))
|
||||
}
|
||||
|
||||
let insert_schools_query = `INSERT INTO spells_schools (id_spell, id_school) VALUES (${new_spell_id}, ${s.schools[i].id})`
|
||||
|
||||
db.query(insert_schools_query, async (err, result) => {
|
||||
if (err) {
|
||||
reject(new HttpError(404, 'Database error - No schools matching this ID'))
|
||||
} else {
|
||||
console.log(`Associated school ID ${s.schools[i].id} to spell ID ${new_spell_id}, affecting ${result.affectedRows} row(s)`)
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
let addVariablesData = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (s.variables !== null) {
|
||||
if (s.variables.length > 0) {
|
||||
for (let i = 0; i < s.variables.length; i++) {
|
||||
if (!regexInt.test(s.variables[i].id)) {
|
||||
reject(new HttpError(403, 'Query error - Variable ID should be an integer !'))
|
||||
}
|
||||
|
||||
let insert_variables_query = `INSERT INTO spells_variables (id_spell, id_variable) VALUES (${new_spell_id}, ${s.variables[i].id})`
|
||||
db.query(insert_variables_query, async (err, result) => {
|
||||
if (err) {
|
||||
reject(new HttpError(404, 'Database error - No variables matching this ID'))
|
||||
} else {
|
||||
console.log(`Associated variable ID ${s.variables[i].id} to spell ID ${new_spell_id}, affecting ${result.affectedRows} row(s)`)
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
let addIngredientsData = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (s.ingredients !== null) {
|
||||
if (s.ingredients.length > 0) {
|
||||
for (let i = 0; i < s.ingredients.length; i++) {
|
||||
if (!regexInt.test(s.ingredients[i].id)) {
|
||||
reject(new HttpError(403, 'Query error - Ingredient ID should be an integer !'))
|
||||
}
|
||||
|
||||
let insert_ingredients_query = `INSERT INTO spells_ingredients (id_spell, id_ingredient) VALUES (${new_spell_id}, ${s.ingredients[i].id})`
|
||||
db.query(insert_ingredients_query, async (err, result) => {
|
||||
if (err) {
|
||||
reject(new HttpError(404, 'Database error - No ingredients matching this ID'))
|
||||
} else {
|
||||
console.log(`Associated ingredient ID ${s.ingredients[i].id} to spell ID ${new_spell_id}, affecting ${result.affectedRows} row(s)`)
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Promise.all([addSchoolsData(), addVariablesData(), addIngredientsData()])
|
||||
.then(() => {
|
||||
resolve(getSpell(new_spell_id))
|
||||
})
|
||||
.catch(err => {
|
||||
reject(err)
|
||||
})
|
||||
Spell.forge({
|
||||
'name': s.name,
|
||||
'description': s.description,
|
||||
'level': s.level,
|
||||
})
|
||||
|
||||
// let query =
|
||||
// 'INSERT INTO spell (name, description'
|
||||
|
||||
// if (s.level !== null) { query += ', level' }
|
||||
// if (s.charge !== null) { query += ', charge' }
|
||||
// if (s.cost !== null) { query += ', cost' }
|
||||
// if (s.is_ritual !== null) { query += ', is_ritual' }
|
||||
|
||||
// query += `) VALUES (${db.escape(s.name)}, ${db.escape(s.description)}`
|
||||
|
||||
// if (s.level !== null) { query += `, ${s.level}` }
|
||||
// if (s.charge !== null) { query += `, ${s.charge}` }
|
||||
// if (s.cost !== null) { query += `, ${db.escape(s.cost)}` }
|
||||
// if (s.is_ritual !== null) { query += `, ${s.is_ritual}` }
|
||||
|
||||
// query += ')'
|
||||
|
||||
// db.query(query, async (err, result) => {
|
||||
// if (err) {
|
||||
// reject(new HttpError(500, 'Database error'))
|
||||
// }
|
||||
// console.log(`Inserted "${s.name}" with ID ${result.insertId}, affecting ${result.affectedRows} row(s)`)
|
||||
|
||||
// const new_spell_id = result.insertId
|
||||
|
||||
// let addSchoolsData = () => {
|
||||
// return new Promise((resolve, reject) => {
|
||||
// if (s.schools !== null) {
|
||||
// if (s.schools.length > 0) {
|
||||
// for (let i = 0; i < s.schools.length; i++) {
|
||||
// if (!regexInt.test(s.schools[i].id)) {
|
||||
// reject(new HttpError(403, 'Query error - School ID should be an integer !'))
|
||||
// }
|
||||
|
||||
// let insert_schools_query = `INSERT INTO spells_schools (id_spell, id_school) VALUES (${new_spell_id}, ${s.schools[i].id})`
|
||||
|
||||
// db.query(insert_schools_query, async (err, result) => {
|
||||
// if (err) {
|
||||
// reject(new HttpError(404, 'Database error - No schools matching this ID'))
|
||||
// } else {
|
||||
// console.log(`Associated school ID ${s.schools[i].id} to spell ID ${new_spell_id}, affecting ${result.affectedRows} row(s)`)
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
|
||||
// let addVariablesData = () => {
|
||||
// return new Promise((resolve, reject) => {
|
||||
// if (s.variables !== null) {
|
||||
// if (s.variables.length > 0) {
|
||||
// for (let i = 0; i < s.variables.length; i++) {
|
||||
// if (!regexInt.test(s.variables[i].id)) {
|
||||
// reject(new HttpError(403, 'Query error - Variable ID should be an integer !'))
|
||||
// }
|
||||
|
||||
// let insert_variables_query = `INSERT INTO spells_variables (id_spell, id_variable) VALUES (${new_spell_id}, ${s.variables[i].id})`
|
||||
// db.query(insert_variables_query, async (err, result) => {
|
||||
// if (err) {
|
||||
// reject(new HttpError(404, 'Database error - No variables matching this ID'))
|
||||
// } else {
|
||||
// console.log(`Associated variable ID ${s.variables[i].id} to spell ID ${new_spell_id}, affecting ${result.affectedRows} row(s)`)
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
|
||||
// let addIngredientsData = () => {
|
||||
// return new Promise((resolve, reject) => {
|
||||
// if (s.ingredients !== null) {
|
||||
// if (s.ingredients.length > 0) {
|
||||
// for (let i = 0; i < s.ingredients.length; i++) {
|
||||
// if (!regexInt.test(s.ingredients[i].id)) {
|
||||
// reject(new HttpError(403, 'Query error - Ingredient ID should be an integer !'))
|
||||
// }
|
||||
|
||||
// let insert_ingredients_query = `INSERT INTO spells_ingredients (id_spell, id_ingredient) VALUES (${new_spell_id}, ${s.ingredients[i].id})`
|
||||
// db.query(insert_ingredients_query, async (err, result) => {
|
||||
// if (err) {
|
||||
// reject(new HttpError(404, 'Database error - No ingredients matching this ID'))
|
||||
// } else {
|
||||
// console.log(`Associated ingredient ID ${s.ingredients[i].id} to spell ID ${new_spell_id}, affecting ${result.affectedRows} row(s)`)
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
|
||||
// Promise.all([addSchoolsData(), addVariablesData(), addIngredientsData()])
|
||||
// .then(() => {
|
||||
// resolve(getSpell(new_spell_id))
|
||||
// })
|
||||
// .catch(err => {
|
||||
// reject(err)
|
||||
// })
|
||||
// })
|
||||
}
|
||||
}).catch(err => {
|
||||
throw err
|
||||
|
||||
Reference in New Issue
Block a user