Add in endpoints for a test http server version of ngsplanner_backend

pull/10/head
sigonasr2, Sig, Sigo 3 years ago
parent b38a54a3c3
commit dc4805eb9b
  1. 201
      server/app.js

@ -45,6 +45,205 @@ new Pool({
port: 5432,
})
const db2 =
new Pool({
user: 'postgres',
password: '',
host: 'postgres',
database: 'ngsplanner2',
port: 5432,
})
const ENDPOINTDATA=[
{
endpoint:"class",
requiredfields:["name"],
optionalfields:["icon"],
excludedfields:[] //Fields to not output in GET.
},
{
endpoint:"class_level_data",
requiredfields:["class_id","level","hp","atk","def"],
optionalfields:[],
excludedfields:[] //Fields to not output in GET.
},
{
endpoint:"class_weapon_type_data",
requiredfields:["class_id","weapon_type_id"],
optionalfields:[],
excludedfields:[] //Fields to not output in GET.
},
{
endpoint:"weapon",
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:["weapon_type_id","weapon_id"],
optionalfields:[],
excludedfields:[] //Fields to not output in GET.
},
{
endpoint:"weapon_type",
requiredfields:["name"],
optionalfields:["icon"],
excludedfields:[] //Fields to not output in GET.
},
{
endpoint:"armor",
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"],
optionalfields:["icon"],
excludedfields:[] //Fields to not output in GET.
},
{
endpoint:"potential_data",
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:["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","skill_type_id"],
optionalfields:["icon"],
excludedfields:[] //Fields to not output in GET.
},
{
endpoint:"skill_type",
requiredfields:["name"],
optionalfields:[],
excludedfields:[] //Fields to not output in GET.
},
{
endpoint:"skill_data",
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:["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"],
optionalfields:["icon"],
excludedfields:[] //Fields to not output in GET.
},
{
endpoint:"food",
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:["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"],
optionalfields:[],
excludedfields:[] //Fields to not output in GET.
},
{
endpoint:"users",
requiredfields:["username","email","created_on","role_id"],
optionalfields:["avatar"],
excludedfields:["password_hash"] //Fields to not output in GET.
}
]
function CreateDynamicEndpoints() {
ENDPOINTDATA.map((endpoint)=>{
app.get("/"+endpoint.endpoint,(req,res)=>{
db2.query('select * from '+endpoint.endpoint+" order by id desc")
.then((data)=>{
res.status(200).json({fields:data.fields,rows:data.rows})
})
.catch((err)=>{
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("Required fields are: "+endpoint.requiredfields.filter((field)=>!(field in req.body)).join(','))
return
}
var combinedfields = [...endpoint.requiredfields,...endpoint.optionalfields,...endpoint.excludedfields]
//console.log(combinedfields)
var all_filled_fields=combinedfields.filter((field)=>(field in req.body))
db2.query('insert into '+endpoint.endpoint+"("+all_filled_fields.join(',')+") values("+all_filled_fields.map((field,i)=>"$"+(i+1)).join(",")+") returning *",all_filled_fields.map((field)=>req.body[field]))
.then((data)=>{
res.status(200).json(data.rows)
})
.catch((err)=>{
res.status(500).send(err.message)
})
})
app.patch("/"+endpoint.endpoint,(req,res)=>{
if (req.body.id) {
var combinedfields = [...endpoint.requiredfields,...endpoint.optionalfields,...endpoint.excludedfields]
//console.log(combinedfields)
var all_filled_fields=combinedfields.filter((field)=>(field in req.body))
db2.query('update '+endpoint.endpoint+' set '+all_filled_fields.map((field,i)=>field+"=$"+(i+1)).join(",")+" where id=$"+(all_filled_fields.length+1)+" returning *",[...all_filled_fields.map((field)=>req.body[field]),req.body.id])
.then((data)=>{
res.status(200).json(data.rows)
})
.catch((err)=>{
res.status(500).send(err.message)
})
} else {
res.status(300).send("Invalid query!")
}
})
app.delete("/"+endpoint.endpoint,(req,res)=>{
if (req.body.id) {
db2.query('delete from '+endpoint.endpoint+' where id=$1 returning *',[req.body.id])
.then((data)=>{
res.status(200).json(data.rows)
})
.catch((err)=>{
res.status(500).send(err.message)
})
} else {
res.status(300).send("Invalid query!")
}
})
})
}
/*const db2 =
new Pool({
user: 'read_only_user',
@ -2370,3 +2569,5 @@ setInterval(
setInterval(()=>{
axios.get("http://www.projectdivar.com/eventdata/t20?chart=true&force=true")
},20000)
CreateDynamicEndpoints()
Loading…
Cancel
Save