sync backend
This commit is contained in:
parent
c8137c3cca
commit
f1b75549f1
@ -458,7 +458,10 @@ function CreateDynamicEndpoints() {
|
|||||||
var all_filled_fields=combinedfields.filter((field)=>(field in req.body))
|
var all_filled_fields=combinedfields.filter((field)=>(field in req.body))
|
||||||
var requiresInsert=true
|
var requiresInsert=true
|
||||||
if (endpoint.requiredfields.includes("name")) {
|
if (endpoint.requiredfields.includes("name")) {
|
||||||
await db.query('update '+endpoint.endpoint+' set '+all_filled_fields.map((field,i)=>field+"=$"+(i+1)).join(",")+' where name=$'+(all_filled_fields.length+1)+' returning *',[...all_filled_fields.map((field)=>req.body[field]),req.body["name"]])
|
await db.query('update '+endpoint.endpoint+' set '+all_filled_fields.map((field,i)=>{
|
||||||
|
if (!field.includes("_id")) {return field+"=$"+(i+1)}else{
|
||||||
|
if (Number.isNaN(Number(req.body[field]))) {return field+"=(select id from "+field.replace("_id","")+" where name=$"+(i+1)+")"} else {return field+"=$"+(i+1)}
|
||||||
|
}}).join(",")+' where name=$'+(all_filled_fields.length+1)+' returning *',[...all_filled_fields.map((field)=>req.body[field]),req.body["name"]])
|
||||||
.then((data)=>{
|
.then((data)=>{
|
||||||
if (data.rows.length===0) {
|
if (data.rows.length===0) {
|
||||||
requiresInsert=true
|
requiresInsert=true
|
||||||
@ -472,7 +475,57 @@ function CreateDynamicEndpoints() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (requiresInsert) {
|
if (requiresInsert) {
|
||||||
db.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]))
|
db.query('insert into '+endpoint.endpoint+"("+all_filled_fields.join(',')+") values("+all_filled_fields.map((field,i)=>{
|
||||||
|
if (!field.includes("_id")) {return "$"+(i+1)}else{
|
||||||
|
if (Number.isNaN(Number(req.body[field]))) {return "(select id from "+field.replace("_id","")+" where name=$"+(i+1)+")"} else {return "$"+(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.post(PREFIX+"/"+endpoint.endpoint,async(req,res)=>{
|
||||||
|
db4.query('select * from password where password=$1',[req.body.pass])
|
||||||
|
.then(async(data)=>{
|
||||||
|
if (data.rows.length>0) {
|
||||||
|
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))
|
||||||
|
var requiresInsert=true
|
||||||
|
if (endpoint.requiredfields.includes("name")) {
|
||||||
|
await db.query('update '+endpoint.endpoint+' set '+all_filled_fields.map((field,i)=>{
|
||||||
|
if (!field.includes("_id")) {return field+"=$"+(i+1)}else{
|
||||||
|
if (Number.isNaN(Number(req.body[field]))) {return field+"=(select id from "+field.replace("_id","")+" where name=$"+(i+1)+")"} else {return field+"=$"+(i+1)}
|
||||||
|
}}).join(",")+' where name=$'+(all_filled_fields.length+1)+' returning *',[...all_filled_fields.map((field)=>req.body[field]),req.body["name"]])
|
||||||
|
.then((data)=>{
|
||||||
|
if (data.rows.length===0) {
|
||||||
|
requiresInsert=true
|
||||||
|
} else {
|
||||||
|
requiresInsert=false
|
||||||
|
res.status(200).json(data.rows)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((err)=>{
|
||||||
|
res.status(500).send(err.message)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if (requiresInsert) {
|
||||||
|
db.query('insert into '+endpoint.endpoint+"("+all_filled_fields.join(',')+") values("+all_filled_fields.map((field,i)=>{
|
||||||
|
if (!field.includes("_id")) {return "$"+(i+1)}else{
|
||||||
|
if (Number.isNaN(Number(req.body[field]))) {return "(select id from "+field.replace("_id","")+" where name=$"+(i+1)+")"} else {return "$"+(i+1)}
|
||||||
|
}}).join(",")+") returning *",all_filled_fields.map((field)=>req.body[field]))
|
||||||
.then((data)=>{
|
.then((data)=>{
|
||||||
res.status(200).json(data.rows)
|
res.status(200).json(data.rows)
|
||||||
})
|
})
|
||||||
@ -485,6 +538,11 @@ function CreateDynamicEndpoints() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
res.status(500).send("Could not authenticate!")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
app.patch(PREFIX+"/"+endpoint.endpoint,(req,res)=>{
|
app.patch(PREFIX+"/"+endpoint.endpoint,(req,res)=>{
|
||||||
if (req.body.id) {
|
if (req.body.id) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user