db.query("select id,authentication_token from users where username=$1",[req.body.username])
db.query("select id,authentication_token from users where username=$1",[req.body.username])
.then((data)=>{if(data.rows.length>0){if(data.rows[0].authentication_token===req.body.authentication_token){userId=data.rows[0].id;returndb.query("select id from songs where name=$1 or romanized_name=$1 or english_name=$1",[req.body.song])}else{thrownewError("Could not authenticate!")}}else{thrownewError("Could not find user.")}
.then((data)=>{if(data.rows.length>0){if(data.rows[0].authentication_token===req.body.authentication_token){userId=data.rows[0].id;returndb.query("select id from songs where name=$1 or romanized_name=$1 or english_name=$1",[req.body.song])}else{thrownewError("Could not authenticate!")}}else{thrownewError("Could not find user.")}
})
})
.then((data)=>{if(data.rows.length>0){songId=data.rows[0].id;varscore=CalculateSongScore({cool:req.body.cool,fine:req.body.fine,safe:req.body.safe,sad:req.body.sad,worst:req.body.worst,percent:req.body.percent,difficulty:req.body.difficulty});returndb.query("insert into plays(songId,userId,difficulty,cool,fine,safe,sad,worst,percent,date,score) values($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11) returning *",[songId,userId,req.body.difficulty,req.body.cool,req.body.fine,req.body.safe,req.body.sad,req.body.worst,req.body.percent,newDate(),score])}else{thrownewError("Could not find song.")}})
.then((data)=>{if(data.rows.length>0){songId=data.rows[0].id;varscore=CalculateSongScore({cool:req.body.cool,fine:req.body.fine,safe:req.body.safe,sad:req.body.sad,worst:req.body.worst,percent:req.body.percent,difficulty:req.body.difficulty,fail:fail});returndb.query("insert into plays(songId,userId,difficulty,cool,fine,safe,sad,worst,percent,date,score,fail) values($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) returning *",[songId,userId,req.body.difficulty,req.body.cool,req.body.fine,req.body.safe,req.body.sad,req.body.worst,req.body.percent,newDate(),score,fail])}else{thrownewError("Could not find song.")}})
.then((data)=>{if(data.rows.length>0){res.status(200).json(data.rows[0])}else{thrownewError("Could not submit song.")}})
.then((data)=>{if(data.rows.length>0){res.status(200).json(data.rows[0])}else{thrownewError("Could not submit song.")}})