Update endpoints to make them more modular and submit to both sides at the same time.

master
Nic0Nic0Nii 3 years ago
parent 3f0e5f81e3
commit 67ee65f803
  1. 58
      server.js
  2. 76
      server_test.js

@ -877,7 +877,7 @@ app.post(PREFIX+"/test/saveskilltree",(req,res)=>{
}) })
}) })
app.post(PREFIX+"/submitBuild",(req,res)=>{ function submitBuild(req,res,db,send) {
if (req.body.id) { if (req.body.id) {
db.query('select users.username from builds join users on users_id=users.id where builds.id=$1',[req.body.id]) db.query('select users.username from builds join users on users_id=users.id where builds.id=$1',[req.body.id])
.then((data)=>{ .then((data)=>{
@ -885,73 +885,61 @@ app.post(PREFIX+"/submitBuild",(req,res)=>{
if (data.rows.length>0&&data.rows[0].username===req.body.username) { if (data.rows.length>0&&data.rows[0].username===req.body.username) {
return db.query('update builds set creator=$1,build_name=$2,class1=(SELECT id from class WHERE name=$3 limit 1),class2=(SELECT id from class WHERE name=$4 limit 1),last_modified=$5,data=$6 where id=$7 returning id',[req.body.creator,req.body.build_name,req.body.class1,req.body.class2,new Date(),req.body.data,req.body.id]) return db.query('update builds set creator=$1,build_name=$2,class1=(SELECT id from class WHERE name=$3 limit 1),class2=(SELECT id from class WHERE name=$4 limit 1),last_modified=$5,data=$6 where id=$7 returning id',[req.body.creator,req.body.build_name,req.body.class1,req.body.class2,new Date(),req.body.data,req.body.id])
.then((data)=>{ .then((data)=>{
if (send) {
res.status(200).send(data.rows[0]) res.status(200).send(data.rows[0])
}
}) })
.catch((err)=>{ .catch((err)=>{
console.log(err.message) console.log(err.message)
if (send) {
res.status(500).send(err.message) res.status(500).send(err.message)
}
}) })
} else { } else {
return db.query('insert into builds(users_id,creator,build_name,class1,class2,created_on,last_modified,likes,data,editors_choice) values((SELECT id from users WHERE username=$1 limit 1),$2,$3,(SELECT id from class WHERE name=$4 limit 1),(SELECT id from class WHERE name=$5 limit 1),$6,$7,$8,$9,$10) returning id',[req.body.username,req.body.creator,req.body.build_name,req.body.class1,req.body.class2,new Date(),new Date(),0,req.body.data,0]) return db.query('insert into builds(users_id,creator,build_name,class1,class2,created_on,last_modified,likes,data,editors_choice) values((SELECT id from users WHERE username=$1 limit 1),$2,$3,(SELECT id from class WHERE name=$4 limit 1),(SELECT id from class WHERE name=$5 limit 1),$6,$7,$8,$9,$10) returning id',[req.body.username,req.body.creator,req.body.build_name,req.body.class1,req.body.class2,new Date(),new Date(),0,req.body.data,0])
.then((data)=>{ .then((data)=>{
if (send) {
res.status(200).send(data.rows[0]) res.status(200).send(data.rows[0])
}
}) })
.catch((err)=>{ .catch((err)=>{
console.log(err.message) console.log(err.message)
if (send) {
res.status(500).send(err.message) res.status(500).send(err.message)
}
}) })
} }
}) })
.catch((err)=>{ .catch((err)=>{
console.log(err.message) console.log(err.message)
if (send) {
res.status(500).send(err.message) res.status(500).send(err.message)
}
}) })
} else { } else {
return db.query('insert into builds(users_id,creator,build_name,class1,class2,created_on,last_modified,likes,data,editors_choice) values((SELECT id from users WHERE username=$1 limit 1),$2,$3,(SELECT id from class WHERE name=$4 limit 1),(SELECT id from class WHERE name=$5 limit 1),$6,$7,$8,$9,$10) returning id',[req.body.username,req.body.creator,req.body.build_name,req.body.class1,req.body.class2,new Date(),new Date(),0,req.body.data,0]) db.query('insert into builds(users_id,creator,build_name,class1,class2,created_on,last_modified,likes,data,editors_choice) values((SELECT id from users WHERE username=$1 limit 1),$2,$3,(SELECT id from class WHERE name=$4 limit 1),(SELECT id from class WHERE name=$5 limit 1),$6,$7,$8,$9,$10) returning id',[req.body.username,req.body.creator,req.body.build_name,req.body.class1,req.body.class2,new Date(),new Date(),0,req.body.data,0])
.then((data)=>{ .then((data)=>{
if (send) {
res.status(200).send(data.rows[0]) res.status(200).send(data.rows[0])
}
}) })
.catch((err)=>{ .catch((err)=>{
console.log(err.message) console.log(err.message)
if (send) {
res.status(500).send(err.message) res.status(500).send(err.message)
}
}) })
} }
}
app.post(PREFIX+"/submitBuild",(req,res)=>{
submitBuild(req,res,db,true)
submitBuild(req,res,db2,false)
}) })
app.post(PREFIX+"/test/submitBuild",(req,res)=>{ app.post(PREFIX+"/test/submitBuild",(req,res)=>{
if (req.body.id) { submitBuild(req,res,db,true)
db2.query('select users.username from builds join users on users_id=users.id where builds.id=$1',[req.body.id]) submitBuild(req,res,db2,false)
.then((data)=>{
if (data.rows[0].username===req.body.username) {
return db2.query('update builds set creator=$1,build_name=$2,class1=(SELECT id from class WHERE name=$3 limit 1),class2=(SELECT id from class WHERE name=$4 limit 1),last_modified=$5,data=$6 returning id',[req.body.creator,req.body.build_name,req.body.class1,req.body.class2,new Date(),req.body.data])
.then((data)=>{
res.status(200).send(data.rows[0])
})
.catch((err)=>{
res.status(500).send(err.message)
})
} else {
return db2.query('insert into builds(users_id,creator,build_name,class1,class2,created_on,last_modified,likes,data,editors_choice) values((SELECT id from users WHERE username=$1 limit 1),$2,$3,(SELECT id from class WHERE name=$4 limit 1),(SELECT id from class WHERE name=$5 limit 1),$6,$7,$8,$9,$10) returning id',[req.body.username,req.body.creator,req.body.build_name,req.body.class1,req.body.class2,new Date(),new Date(),0,req.body.data,0])
.then((data)=>{
res.status(200).send(data.rows[0])
})
.catch((err)=>{
res.status(500).send(err.message)
})
}
})
.catch((err)=>{
res.status(500).send(err.message)
})
} else {
return db2.query('insert into builds(users_id,creator,build_name,class1,class2,created_on,last_modified,likes,data,editors_choice) values((SELECT id from users WHERE username=$1 limit 1),$2,$3,(SELECT id from class WHERE name=$4 limit 1),(SELECT id from class WHERE name=$5 limit 1),$6,$7,$8,$9,$10) returning id',[req.body.username,req.body.creator,req.body.build_name,req.body.class1,req.body.class2,new Date(),new Date(),0,req.body.data,0])
.then((data)=>{
res.status(200).send(data.rows[0])
})
.catch((err)=>{
res.status(500).send(err.message)
})
}
}) })
//Generates our table schema: //Generates our table schema:

@ -26,9 +26,6 @@ let allowCrossDomain = function(req, res, next) {
app.use(allowCrossDomain); app.use(allowCrossDomain);
app.listen(PORT, () => console.log(`Listening on ${ PORT }`)); app.listen(PORT, () => console.log(`Listening on ${ PORT }`));
var authenticated = true;
const PREFIX="" const PREFIX=""
const ENDPOINTDATA=[ const ENDPOINTDATA=[
@ -695,6 +692,7 @@ app.get(PREFIX+'/data',async(req,res)=>{
var finalresult = {} var finalresult = {}
var promises = [] var promises = []
for (var endpoint of ENDPOINTDATA) { for (var endpoint of ENDPOINTDATA) {
if (endpoint.endpoint!=="builds"&&endpoint.endpoint!=="users") {
if (endpoint.requiredfields.includes("name")) { if (endpoint.requiredfields.includes("name")) {
await db.query('select * from (select distinct on (name) name,* from '+endpoint.endpoint+' order by name,id desc)t order by id asc') await db.query('select * from (select distinct on (name) name,* from '+endpoint.endpoint+' order by name,id desc)t order by id asc')
.then((data)=>{ .then((data)=>{
@ -708,6 +706,7 @@ app.get(PREFIX+'/data',async(req,res)=>{
}) })
} }
} }
}
res.status(200).json(finalresult) res.status(200).json(finalresult)
}) })
@ -715,6 +714,7 @@ app.get(PREFIX+'/test/data',async(req,res)=>{
var finalresult = {} var finalresult = {}
var promises = [] var promises = []
for (var endpoint of ENDPOINTDATA) { for (var endpoint of ENDPOINTDATA) {
if (endpoint.endpoint!=="builds"&&endpoint.endpoint!=="users") {
if (endpoint.requiredfields.includes("name")) { if (endpoint.requiredfields.includes("name")) {
await db2.query('select distinct on (name) name,* from '+endpoint.endpoint+' order by name,id desc') await db2.query('select distinct on (name) name,* from '+endpoint.endpoint+' order by name,id desc')
.then((data)=>{ .then((data)=>{
@ -728,6 +728,7 @@ app.get(PREFIX+'/test/data',async(req,res)=>{
}) })
} }
} }
}
res.status(200).json(finalresult) res.status(200).json(finalresult)
}) })
@ -735,12 +736,14 @@ app.get(PREFIX+'/dataid',async(req,res)=>{
var finalresult = {} var finalresult = {}
var promises = [] var promises = []
for (var endpoint of ENDPOINTDATA) { for (var endpoint of ENDPOINTDATA) {
if (endpoint.endpoint!=="builds"&&endpoint.endpoint!=="users") {
await db.query('select * from '+endpoint.endpoint+' order by id asc') await db.query('select * from '+endpoint.endpoint+' order by id asc')
.then((data)=>{ .then((data)=>{
finalresult[endpoint.endpoint]={} finalresult[endpoint.endpoint]={}
data.rows.forEach((val)=>{finalresult[endpoint.endpoint][val.id]=val}) data.rows.forEach((val)=>{finalresult[endpoint.endpoint][val.id]=val})
}) })
} }
}
res.status(200).json(finalresult) res.status(200).json(finalresult)
}) })
@ -748,12 +751,14 @@ app.get(PREFIX+'/test/dataid',async(req,res)=>{
var finalresult = {} var finalresult = {}
var promises = [] var promises = []
for (var endpoint of ENDPOINTDATA) { for (var endpoint of ENDPOINTDATA) {
if (endpoint.endpoint!=="builds"&&endpoint.endpoint!=="users") {
await db2.query('select * from '+endpoint.endpoint+' order by id asc') await db2.query('select * from '+endpoint.endpoint+' order by id asc')
.then((data)=>{ .then((data)=>{
finalresult[endpoint.endpoint]={} finalresult[endpoint.endpoint]={}
data.rows.forEach((val)=>{finalresult[endpoint.endpoint][val.id]=val}) data.rows.forEach((val)=>{finalresult[endpoint.endpoint][val.id]=val})
}) })
} }
}
res.status(200).json(finalresult) res.status(200).json(finalresult)
}) })
@ -824,6 +829,71 @@ app.post(PREFIX+"/test/saveskilltree",(req,res)=>{
}) })
}) })
function submitBuild(req,res,db,send) {
if (req.body.id) {
db.query('select users.username from builds join users on users_id=users.id where builds.id=$1',[req.body.id])
.then((data)=>{
console.log(data.rows)
if (data.rows.length>0&&data.rows[0].username===req.body.username) {
return db.query('update builds set creator=$1,build_name=$2,class1=(SELECT id from class WHERE name=$3 limit 1),class2=(SELECT id from class WHERE name=$4 limit 1),last_modified=$5,data=$6 where id=$7 returning id',[req.body.creator,req.body.build_name,req.body.class1,req.body.class2,new Date(),req.body.data,req.body.id])
.then((data)=>{
if (send) {
res.status(200).send(data.rows[0])
}
})
.catch((err)=>{
console.log(err.message)
if (send) {
res.status(500).send(err.message)
}
})
} else {
return db.query('insert into builds(users_id,creator,build_name,class1,class2,created_on,last_modified,likes,data,editors_choice) values((SELECT id from users WHERE username=$1 limit 1),$2,$3,(SELECT id from class WHERE name=$4 limit 1),(SELECT id from class WHERE name=$5 limit 1),$6,$7,$8,$9,$10) returning id',[req.body.username,req.body.creator,req.body.build_name,req.body.class1,req.body.class2,new Date(),new Date(),0,req.body.data,0])
.then((data)=>{
if (send) {
res.status(200).send(data.rows[0])
}
})
.catch((err)=>{
console.log(err.message)
if (send) {
res.status(500).send(err.message)
}
})
}
})
.catch((err)=>{
console.log(err.message)
if (send) {
res.status(500).send(err.message)
}
})
} else {
db.query('insert into builds(users_id,creator,build_name,class1,class2,created_on,last_modified,likes,data,editors_choice) values((SELECT id from users WHERE username=$1 limit 1),$2,$3,(SELECT id from class WHERE name=$4 limit 1),(SELECT id from class WHERE name=$5 limit 1),$6,$7,$8,$9,$10) returning id',[req.body.username,req.body.creator,req.body.build_name,req.body.class1,req.body.class2,new Date(),new Date(),0,req.body.data,0])
.then((data)=>{
if (send) {
res.status(200).send(data.rows[0])
}
})
.catch((err)=>{
console.log(err.message)
if (send) {
res.status(500).send(err.message)
}
})
}
}
app.post(PREFIX+"/submitBuild",(req,res)=>{
submitBuild(req,res,db,true)
submitBuild(req,res,db2,false)
})
app.post(PREFIX+"/test/submitBuild",(req,res)=>{
submitBuild(req,res,db,true)
submitBuild(req,res,db2,false)
})
//Generates our table schema: //Generates our table schema:
ENDPOINTDATA.forEach((endpoint)=>{ ENDPOINTDATA.forEach((endpoint)=>{
console.log(endpoint.endpoint+":\n\t"+endpoint.requiredfields.join('\t')+(endpoint.optionalfields.length>0?"\t":"")+endpoint.optionalfields.join("\t")) console.log(endpoint.endpoint+":\n\t"+endpoint.requiredfields.join('\t')+(endpoint.optionalfields.length>0?"\t":"")+endpoint.optionalfields.join("\t"))

Loading…
Cancel
Save