From ce8bf448a1466e93c2cd4755777ff809c0548508 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Fri, 23 Jul 2021 08:44:59 -0500 Subject: [PATCH] Added POST dynamic endpoint --- ngsplanner_schema.sql | 5 +++ server.js | 94 ++++++++++++++++++++++++++++--------------- 2 files changed, 66 insertions(+), 33 deletions(-) diff --git a/ngsplanner_schema.sql b/ngsplanner_schema.sql index c4791a2..217edaa 100644 --- a/ngsplanner_schema.sql +++ b/ngsplanner_schema.sql @@ -31,11 +31,13 @@ CREATE TABLE "class" ( ); CREATE TABLE "class_weapon_type_data" ( + "id" SERIAL UNIQUE PRIMARY KEY, "class_id" int, "weapon_type_id" int ); CREATE TABLE "class_level_data" ( + "id" SERIAL UNIQUE PRIMARY KEY, "class_id" int, "level" int, "hp" int, @@ -70,6 +72,7 @@ CREATE TABLE "potential" ( ); CREATE TABLE "potential_data" ( + "id" SERIAL UNIQUE PRIMARY KEY, "potential_id" int, "level" int, "mel_dmg" float, @@ -157,6 +160,7 @@ CREATE TABLE "skill" ( ); CREATE TABLE "skill_data" ( + "id" SERIAL UNIQUE PRIMARY KEY, "skill_id" int, "level" int, "variance" float, @@ -205,6 +209,7 @@ CREATE TABLE "builds" ( ); CREATE TABLE "weapon_existence_data" ( + "id" SERIAL UNIQUE PRIMARY KEY, "weapon_type_id" int, "weapon_id" int ); diff --git a/server.js b/server.js index 497401d..93507a2 100644 --- a/server.js +++ b/server.js @@ -48,116 +48,116 @@ new Pool({ const ENDPOINTDATA=[ { endpoint:"class", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["name"], + optionalfields:["icon"], excludedfields:[] //Fields to not output in GET. }, { endpoint:"class_level_data", - requiredfields:["name","icon"], + requiredfields:["class_id","level","hp","atk","def"], optionalfields:[], excludedfields:[] //Fields to not output in GET. }, { endpoint:"class_weapon_type_data", - requiredfields:["name","icon"], + requiredfields:["class_id","weapon_type_id"], optionalfields:[], excludedfields:[] //Fields to not output in GET. }, { endpoint:"weapon", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["name","rarity","level_req","atk"], + optionalfields:["potential_id","variance","base_affix_slots","drop_info","pb_gauge_build","icon"], excludedfields:[] //Fields to not output in GET. }, { endpoint:"weapon_existence_data", - requiredfields:["name","icon"], + requiredfields:["weapon_type_id","weapon_id"], optionalfields:[], excludedfields:[] //Fields to not output in GET. }, { endpoint:"weapon_type", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["name"], + optionalfields:["icon"], excludedfields:[] //Fields to not output in GET. }, { endpoint:"armor", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["name","rarity","level_req","def"], + optionalfields:["hp","pp","mel_dmg","rng_dmg","tec_dmg","crit_rate","crit_dmg","pp_cost_reduction","active_pp_recovery","natural_pp_recovery","dmg_res","all_down_res","burn_res","freeze_res","blind_res","shock_res","panic_res","poison_res","battle_power_value","pb_gauge_build","icon"], excludedfields:[] //Fields to not output in GET. }, { endpoint:"potential", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["name"], + optionalfields:["icon"], excludedfields:[] //Fields to not output in GET. }, { endpoint:"potential_data", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["potential_id","level"], + optionalfields:["mel_dmg","rng_dmg","tec_dmg","crit_rate","crit_dmg","pp_cost_reduction","active_pp_recovery","natural_pp_recovery","dmg_res","all_down_res","burn_res","freeze_res","blind_res","shock_res","panic_res","poison_res","battle_power_value","pb_gauge_build"], excludedfields:[] //Fields to not output in GET. }, { endpoint:"builds", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["user_id","creator","build_name","class1","created_on","last_modified","data"], + optionalfields:["class2","likes"], excludedfields:[] //Fields to not output in GET. }, { endpoint:"skill", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["name","skill_type_id"], + optionalfields:["icon"], excludedfields:[] //Fields to not output in GET. }, { endpoint:"skill_type", - requiredfields:["name","icon"], + requiredfields:["name"], optionalfields:[], excludedfields:[] //Fields to not output in GET. }, { endpoint:"skill_data", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["skill_id","level"], + optionalfields:["variance","mel_dmg","rng_dmg","tec_dmg","crit_rate","crit_dmg","pp_cost_reduction","active_pp_recovery","natural_pp_recovery","dmg_res"], excludedfields:[] //Fields to not output in GET. }, { endpoint:"augment", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["augment_type_id","level"], + optionalfields:["variance","hp","pp","mel_dmg","rng_dmg","tec_dmg","crit_rate","crit_dmg","pp_cost_reduction","active_pp_recovery","natural_pp_recovery","dmg_res","affix_success_rate","all_down_res","burn_res","freeze_res","blind_res","shock_res","panic_res","poison_res","battle_power_value","pb_gauge_build"], excludedfields:[] //Fields to not output in GET. }, { endpoint:"augment_type", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["name"], + optionalfields:["icon"], excludedfields:[] //Fields to not output in GET. }, { endpoint:"food", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["material"], + optionalfields:["potency","pp","dmg_res","hp","pp_consumption","pp_recovery","weak_point_dmg","hp_recovery"], excludedfields:[] //Fields to not output in GET. }, { endpoint:"food_mult", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["amount"], + optionalfields:["potency","pp","dmg_res","hp","pp_consumption","pp_recovery","weak_point_dmg","hp_recovery"], excludedfields:[] //Fields to not output in GET. }, { endpoint:"roles", - requiredfields:["name","icon"], + requiredfields:["name"], optionalfields:[], excludedfields:[] //Fields to not output in GET. }, { endpoint:"users", - requiredfields:["name","icon"], - optionalfields:[], + requiredfields:["username","email","created_on","role_id"], + optionalfields:["avatar"], excludedfields:["password_hash"] //Fields to not output in GET. } ] @@ -173,6 +173,34 @@ function CreateDynamicEndpoints() { res.status(500).send(err.message) }) }) + + + app.post("/"+endpoint.endpoint,(req,res)=>{ + + var allExist=true + endpoint.requiredfields.forEach((field)=>{ + if (!(field in req.body)) { + allExist=false; + } + }) + if (!allExist) { + res.status(300).send("Invalid query!") + return + } + + var combinedfields = [...endpoint.requiredfields,...endpoint.optionalfields,...endpoint.excludedfields] + //console.log(combinedfields) + var all_filled_fields=combinedfields.filter((field)=>(field in req.body)) + + db.query('insert into '+endpoint.endpoint+"("+all_filled_fields.join(',')+") values("+all_filled_fields.map((field,i)=>"$"+(i+1)).join(",")+")",all_filled_fields.map((field)=>req.body[field])) + .then((data)=>{ + res.status(200).json(data.rows) + }) + .catch((err)=>{ + res.status(500).send(err.message) + }) + }) + }) }