- [API] Fixed the updateSpell to wait for queries

This commit is contained in:
Alexis
2020-05-24 12:12:15 +02:00
parent 08b0b94bd8
commit df7ee9add4

View File

@@ -121,17 +121,17 @@ const addSpell = (s) => {
let query = let query =
'INSERT INTO spell (name, description' 'INSERT INTO spell (name, description'
if (typeof s.level !== undefined) { query += ', level' } if (s.level !== null) { query += ', level' }
if (typeof s.charge !== undefined) { query += ', charge' } if (s.charge !== null) { query += ', charge' }
if (typeof s.cost !== undefined) { query += ', cost' } if (s.cost !== null) { query += ', cost' }
if (typeof s.is_ritual !== undefined) { query += ', is_ritual' } if (s.is_ritual !== null) { query += ', is_ritual' }
query += `) VALUES (${db.escape(s.name)}, ${db.escape(s.description)}` query += `) VALUES (${db.escape(s.name)}, ${db.escape(s.description)}`
if (typeof s.level !== undefined) { query += `, ${s.level}` } if (s.level !== null) { query += `, ${s.level}` }
if (typeof s.charge !== undefined) { query += `, ${s.charge}` } if (s.charge !== null) { query += `, ${s.charge}` }
if (typeof s.cost !== undefined) { query += `, ${db.escape(s.cost)}` } if (s.cost !== null) { query += `, ${db.escape(s.cost)}` }
if (typeof s.is_ritual !== undefined) { query += `, ${s.is_ritual}` } if (s.is_ritual !== null) { query += `, ${s.is_ritual}` }
query += ')' query += ')'
@@ -145,7 +145,7 @@ const addSpell = (s) => {
let addSchoolsData = () => { let addSchoolsData = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof s.schools !== undefined) { if (s.schools !== null) {
if (s.schools.length > 0) { if (s.schools.length > 0) {
for (let i = 0; i < s.schools.length; i++) { for (let i = 0; i < s.schools.length; i++) {
if (!regexInt.test(s.schools[i].id)) { if (!regexInt.test(s.schools[i].id)) {
@@ -156,7 +156,7 @@ const addSpell = (s) => {
db.query(insert_schools_query, async (err, result) => { db.query(insert_schools_query, async (err, result) => {
if (err) { if (err) {
reject(new HttpError(500, 'Database error - No schools matching this ID')) reject(new HttpError(404, 'Database error - No schools matching this ID'))
} else { } else {
console.log(`Associated school ID ${s.schools[i].id} to spell ID ${new_spell_id}, affecting ${result.affectedRows} row(s)`) console.log(`Associated school ID ${s.schools[i].id} to spell ID ${new_spell_id}, affecting ${result.affectedRows} row(s)`)
resolve() resolve()
@@ -174,7 +174,7 @@ const addSpell = (s) => {
let addVariablesData = () => { let addVariablesData = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof s.variables !== undefined) { if (s.variables !== null) {
if (s.variables.length > 0) { if (s.variables.length > 0) {
for (let i = 0; i < s.variables.length; i++) { for (let i = 0; i < s.variables.length; i++) {
if (!regexInt.test(s.variables[i].id)) { if (!regexInt.test(s.variables[i].id)) {
@@ -184,7 +184,7 @@ const addSpell = (s) => {
let insert_variables_query = `INSERT INTO spells_variables (id_spell, id_variable) VALUES (${new_spell_id}, ${s.variables[i].id})` 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) => { db.query(insert_variables_query, async (err, result) => {
if (err) { if (err) {
reject(new HttpError(500, 'Database error - No variables matching this ID')) reject(new HttpError(404, 'Database error - No variables matching this ID'))
} else { } else {
console.log(`Associated variable ID ${s.variables[i].id} to spell ID ${new_spell_id}, affecting ${result.affectedRows} row(s)`) console.log(`Associated variable ID ${s.variables[i].id} to spell ID ${new_spell_id}, affecting ${result.affectedRows} row(s)`)
resolve() resolve()
@@ -202,7 +202,7 @@ const addSpell = (s) => {
let addIngredientsData = () => { let addIngredientsData = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof s.ingredients !== undefined) { if (s.ingredients !== null) {
if (s.ingredients.length > 0) { if (s.ingredients.length > 0) {
for (let i = 0; i < s.ingredients.length; i++) { for (let i = 0; i < s.ingredients.length; i++) {
if (!regexInt.test(s.ingredients[i].id)) { if (!regexInt.test(s.ingredients[i].id)) {
@@ -212,7 +212,7 @@ const addSpell = (s) => {
let insert_ingredients_query = `INSERT INTO spells_ingredients (id_spell, id_ingredient) VALUES (${new_spell_id}, ${s.ingredients[i].id})` 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) => { db.query(insert_ingredients_query, async (err, result) => {
if (err) { if (err) {
reject(new HttpError(500, 'Database error - No ingredients matching this ID')) reject(new HttpError(404, 'Database error - No ingredients matching this ID'))
} else { } else {
console.log(`Associated ingredient ID ${s.ingredients[i].id} to spell ID ${new_spell_id}, affecting ${result.affectedRows} row(s)`) console.log(`Associated ingredient ID ${s.ingredients[i].id} to spell ID ${new_spell_id}, affecting ${result.affectedRows} row(s)`)
resolve() resolve()
@@ -276,27 +276,10 @@ const updateSpell = (s, id) => {
} else if (isXSSAttempt(s.name) || isXSSAttempt(s.description) || 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 {
let query =
'UPDATE spell SET '
if (typeof s.name !== undefined) { query += `name = "${s.name}" ` }
if (typeof s.description !== undefined) { query += `, description = "${s.description}" ` }
if (typeof s.level !== undefined) { query += `, level = ${s.level} ` }
if (typeof s.charge !== undefined) { query += `, charge = ${s.charge} ` }
if (typeof s.cost !== undefined) { query += `, cost = "${s.cost}" ` }
if (typeof s.is_ritual !== undefined) { query += `, is_ritual = ${s.is_ritual} ` }
query += ` WHERE id = ${db.escape(id)}`
db.query(query, async (err, result) => {
if (err) {
reject(new HttpError(500, 'Database error - Spell update failed'))
}
console.log(`Updated "${s.name}" on ID ${old_spell.id}, affecting ${result.affectedRows} row(s)`)
let updateSchoolsData = () => { let updateSchoolsData = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof s.schools !== undefined) { if (s.schools !== null) {
if (s.schools.length > 0) { if (s.schools.length > 0) {
let delete_schools_query = let delete_schools_query =
`DELETE FROM spells_schools WHERE id_spell = ${old_spell.id}` `DELETE FROM spells_schools WHERE id_spell = ${old_spell.id}`
@@ -315,7 +298,7 @@ const updateSpell = (s, id) => {
let update_schools_query = `INSERT INTO spells_schools (id_spell, id_school) VALUES (${old_spell.id}, ${s.schools[i].id})` let update_schools_query = `INSERT INTO spells_schools (id_spell, id_school) VALUES (${old_spell.id}, ${s.schools[i].id})`
db.query(update_schools_query, async (err, result) => { db.query(update_schools_query, async (err, result) => {
if (err) { if (err) {
reject(new HttpError(500, 'Database error - No schools matching this ID')) reject(new HttpError(404, 'Database error - No schools matching this ID'))
} else { } else {
console.log(`Updated association school ID ${s.schools[i].id} to spell ID ${old_spell.id}`) console.log(`Updated association school ID ${s.schools[i].id} to spell ID ${old_spell.id}`)
resolve() resolve()
@@ -333,7 +316,7 @@ const updateSpell = (s, id) => {
let updateVariablesData = () => { let updateVariablesData = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof s.variables !== undefined) { if (s.variables !== null) {
if (s.variables.length > 0) { if (s.variables.length > 0) {
let delete_variables_query = let delete_variables_query =
`DELETE FROM spells_variables WHERE id_spell = ${old_spell.id}` `DELETE FROM spells_variables WHERE id_spell = ${old_spell.id}`
@@ -352,7 +335,7 @@ const updateSpell = (s, id) => {
let update_variables_query = `INSERT INTO spells_variables (id_spell, id_variable) VALUES (${old_spell.id}, ${s.variables[i].id})` let update_variables_query = `INSERT INTO spells_variables (id_spell, id_variable) VALUES (${old_spell.id}, ${s.variables[i].id})`
db.query(update_variables_query, async (err, result) => { db.query(update_variables_query, async (err, result) => {
if (err) { if (err) {
reject(new HttpError(500, 'Database error - No variables matching this ID')) reject(new HttpError(404, 'Database error - No variables matching this ID'))
} else { } else {
console.log(`Updated variable ID "${s.variables[i].id}" to spell ID ${old_spell.id}`) console.log(`Updated variable ID "${s.variables[i].id}" to spell ID ${old_spell.id}`)
resolve() resolve()
@@ -370,7 +353,7 @@ const updateSpell = (s, id) => {
let updateIngredientsData = () => { let updateIngredientsData = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof s.ingredients !== undefined) { if (s.ingredients !== null) {
if (s.ingredients.length > 0) { if (s.ingredients.length > 0) {
let delete_ingredients_query = let delete_ingredients_query =
`DELETE FROM spells_ingredients WHERE id_spell = ${old_spell.id}` `DELETE FROM spells_ingredients WHERE id_spell = ${old_spell.id}`
@@ -391,7 +374,7 @@ const updateSpell = (s, id) => {
let update_ingredients_query = `INSERT INTO spells_ingredients (id_spell, id_ingredient) VALUES (${old_spell.id}, ${s.ingredients[i].id})` let update_ingredients_query = `INSERT INTO spells_ingredients (id_spell, id_ingredient) VALUES (${old_spell.id}, ${s.ingredients[i].id})`
db.query(update_ingredients_query, async (err, result) => { db.query(update_ingredients_query, async (err, result) => {
if (err) { if (err) {
reject(new HttpError(500, 'Database error - No ingredients matching this ID')) reject(new HttpError(404, 'Database error - No ingredients matching this ID'))
} else { } else {
console.log(`Updated ingredient ID "${s.ingredients[i].id}" to spell ID ${old_spell.id}`) console.log(`Updated ingredient ID "${s.ingredients[i].id}" to spell ID ${old_spell.id}`)
resolve() resolve()
@@ -407,20 +390,47 @@ const updateSpell = (s, id) => {
}) })
} }
const promises = [ let updateSpellData = () => {
return new Promise((resolve, reject) => {
let update_spell_query =
'UPDATE spell SET '
if (s.name !== null) { update_spell_query += `name = "${s.name}" ` }
if (s.description !== null) { update_spell_query += `, description = "${s.description}" ` }
if (s.level !== null) { update_spell_query += `, level = ${s.level} ` }
if (s.charge !== null) { update_spell_query += `, charge = ${s.charge} ` }
if (s.cost !== null) { update_spell_query += `, cost = "${s.cost}" ` }
if (s.is_ritual !== null) { update_spell_query += `, is_ritual = ${s.is_ritual} ` }
update_spell_query += ` WHERE id = ${db.escape(id)}`
db.query(update_spell_query, async (err, result) => {
if (err) {
reject(new HttpError(500, 'Database error - Spell update failed'))
} else {
console.log(`Updated "${s.name}" on ID ${old_spell.id}, affecting ${result.affectedRows} row(s)`)
resolve()
}
})
})
}
const sub_promises = [
updateSchoolsData(), updateSchoolsData(),
updateVariablesData(), updateVariablesData(),
updateIngredientsData() updateIngredientsData()
] ]
Promise.all(promises) Promise.all(sub_promises)
.then(() => {
updateSpellData()
})
.then(() => { .then(() => {
resolve(getSpell(old_spell.id)) resolve(getSpell(old_spell.id))
}) })
.catch(err => { .catch(err => {
reject(err) reject(err)
}) })
})
} }
}) })
.catch(err => { .catch(err => {
@@ -580,7 +590,7 @@ const buildSpell = async (spell) => {
let fetchSpellSchoolData = (s) => { let fetchSpellSchoolData = (s) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof s == undefined) { reject(new HttpError(404, "Error: No spell matching this ID"))} if (s == null) { reject(new HttpError(404, "Error: No spell matching this ID"))}
let query = let query =
"SELECT school.id, school.name " + "SELECT school.id, school.name " +
@@ -603,7 +613,7 @@ const buildSpell = async (spell) => {
let fetchSpellVariablesData = (s) => { let fetchSpellVariablesData = (s) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof s == undefined) { reject(new HttpError(404, "Error: No spell matching this ID"))} if (s == null) { reject(new HttpError(404, "Error: No spell matching this ID"))}
let query = let query =
"SELECT variable.id, variable.description " + "SELECT variable.id, variable.description " +
@@ -626,7 +636,7 @@ const buildSpell = async (spell) => {
let fetchSpellIngredientsData = (s) => { let fetchSpellIngredientsData = (s) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (typeof s == undefined) { reject(new HttpError(404, "Error: No spell matching this ID"))} if (s == null) { reject(new HttpError(404, "Error: No spell matching this ID"))}
let query = let query =
"SELECT ingredient.id, ingredient.name " + "SELECT ingredient.id, ingredient.name " +