Created
May 15, 2015 04:46
-
-
Save Piefayth/fc444a41e3d93aa8b232 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//OLD: | |
pg.connect(constring, function(err, client, done){ | |
if(err){ | |
console.log('Unable to collect client from pool.'); | |
} | |
client.query('BEGIN', function(err){ | |
if (err) return rollback(client, done); | |
process.nextTick(function(){ | |
var query = | |
"INSERT INTO users.users(id, email, name, profilepicture, accesstoken, refreshtoken, expirationtime)" + | |
" SELECT $1, $2, $3, $4, $5, $6, $7" + | |
" WHERE NOT EXISTS ( SELECT id FROM users.users WHERE id = $1);" | |
client.query(query, | |
[body.id, | |
body.email, | |
body.display_name, | |
profilePicture, | |
req.session.spotify_access_token, | |
req.session.spotify_refresh_token, | |
req.session.spotify_expiration_time, | |
100/1373], | |
function(err){ | |
if(err) return rollback(client, done, err); | |
var query = | |
"INSERT INTO users.genreweights(userid, genre, weight) " + | |
"SELECT $1, genres.name, $8 FROM music.genres " + | |
" WHERE NOT EXISTS (SELECT id FROM users.users WHERE id = $1);"; | |
client.query(query, [body.id, 100/1373]. function(err){ | |
if(err) return rollback(client, done, err); | |
client.query('COMMIT', done); | |
}) | |
}) | |
}) | |
}) | |
}) | |
//NEW: | |
var queryable = new Queryable(constring); | |
queryable.query('BEGIN', function(){ | |
process.nextTick(function(){ | |
var query1 = | |
"INSERT INTO users.genreweights(userid, genre, weight) " + | |
"SELECT $1, genres.name, $2 FROM music.genres " + | |
"WHERE NOT EXISTS (SELECT id FROM users.users WHERE id = $1)"; | |
var query2 = | |
"INSERT INTO users.users(id, email, name, profilepicture, accesstoken, refreshtoken, expirationtime)" + | |
" SELECT $1, $2, $3, $4, $5, $6, $7" + | |
" WHERE NOT EXISTS ( SELECT id FROM users.users WHERE id = $1)"; | |
queryable.query(query1, | |
[body.id, | |
STARTING_POINTS/NUMBER_GENRES]) | |
.query(query2, | |
[body.id, | |
body.email, | |
body.display_name, | |
profilePicture, | |
req.session.spotify_access_token, | |
req.session.spotify_refresh_token, | |
req.session.spotify_expiration_time]) | |
.query('COMMIT').end(); | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment