Backend saving implemented

master
sigonasr2 3 years ago
parent 2afbfaf58f
commit 61ea568fda
  1. 85
      server.js

@ -740,6 +740,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)=>{
@ -753,6 +754,7 @@ app.get(PREFIX+'/data',async(req,res)=>{
}) })
} }
} }
}
res.status(200).json(finalresult) res.status(200).json(finalresult)
}) })
@ -760,6 +762,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)=>{
@ -773,6 +776,7 @@ app.get(PREFIX+'/test/data',async(req,res)=>{
}) })
} }
} }
}
res.status(200).json(finalresult) res.status(200).json(finalresult)
}) })
@ -780,12 +784,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)
}) })
@ -793,12 +799,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)
}) })
@ -869,6 +877,83 @@ app.post(PREFIX+"/test/saveskilltree",(req,res)=>{
}) })
}) })
app.post(PREFIX+"/submitBuild",(req,res)=>{
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)=>{
res.status(200).send(data.rows[0])
})
.catch((err)=>{
console.log(err.message)
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)=>{
res.status(200).send(data.rows[0])
})
.catch((err)=>{
console.log(err.message)
res.status(500).send(err.message)
})
}
})
.catch((err)=>{
console.log(err.message)
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)=>{
res.status(200).send(data.rows[0])
})
.catch((err)=>{
console.log(err.message)
res.status(500).send(err.message)
})
}
})
app.post(PREFIX+"/test/submitBuild",(req,res)=>{
if (req.body.id) {
db2.query('select users.username from builds join users on users_id=users.id where builds.id=$1',[req.body.id])
.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:
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