From 9757f7b91f4169a212a00cdfe3f6cee920808360 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Fri, 30 Aug 2013 09:57:58 -0300 Subject: [PATCH] less indentation --- lyricsnode/routes/round.js | 89 +++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 31 deletions(-) diff --git a/lyricsnode/routes/round.js b/lyricsnode/routes/round.js index 4140033..a8ffe14 100644 --- a/lyricsnode/routes/round.js +++ b/lyricsnode/routes/round.js @@ -34,43 +34,70 @@ function acronymize(phrase) { */ function getSong(res) { var name = dbName(); - console.log('Opening the database ' + name + '...'); - db = new sqlite3.Database(name, sqlite3.OPEN_READWRITE, function (error) { - if (error) { - throw error; - } + var db = null; + var song = null; + var artist = null; + var lyrics = null; + + function pickSong() { + console.log('Picking song...'); + db.get('SELECT * FROM songs WHERE done_in IS NULL ORDER BY random() LIMIT 1', function (err, selectSong) { + if (err) { + throw err; + } + + song = selectSong; + console.log(song); + pickArtist(); + }); + } - console.log('Pickling a song...'); - db.get('SELECT * FROM songs WHERE done_in IS NULL ORDER BY random() LIMIT 1', function (err, song) { + function pickArtist() { + console.log('Retrieving artist for song...'); + db.get('SELECT * FROM artists WHERE id = ' + song.artist, function (err, selectArtist) { if (err) { throw err; } - console.log('Retrieving artist for song...'); - db.get('SELECT * FROM artists WHERE id = ' + song.artist, function (err, artist) { - if (err) { - throw err; - } - - console.log('Retrieving lyrics for song... '); - db.get('SELECT * FROM lyrics WHERE song = ' + song.id, function (err, lyrics) { - if (err) { - throw err; - } - - var params = { - title: 'Round', - lyrics: lyrics.lyrics, - song_title: song.title, - song_artist: artist.name, - acronym_title: acronymize(song.title), - acronym_artist: acronymize(artist.name), - id: song.id - }; - res.render('index', params); - }); - }); + artist = selectArtist; + console.log(artist); + pickLyrics(); }); + } + + function pickLyrics() { + console.log('Retrieving lyrics for song... '); + db.get('SELECT * FROM lyrics WHERE song = ' + song.id, function (err, selectLyrics) { + if (err) { + throw err; + } + + lyrics = selectLyrics; + console.log(lyrics); + answer(); + }); + } + + function answer() { + var params = { + title: 'Round', + lyrics: lyrics.lyrics, + song_title: song.title, + song_artist: artist.name, + acronym_title: acronymize(song.title), + acronym_artist: acronymize(artist.name), + id: song.id + }; + res.render('index', params); + } + + console.log('Opening the database ' + name + '...'); + db = new sqlite3.Database(name, sqlite3.OPEN_READWRITE, function (error) { + if (error) { + throw error; + } + + pickSong(); }); }