From 091a9597ce835b2e405f40084aae2ff42923d864 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 15 Aug 2013 10:55:11 -0300 Subject: [PATCH] finally displaying something --- lyricsnode/package.json | 5 +-- lyricsnode/routes/round.js | 72 +++++++++++++++++++++++++++++++++++-- lyricsnode/views/index.html | 10 +++--- 3 files changed, 78 insertions(+), 9 deletions(-) diff --git a/lyricsnode/package.json b/lyricsnode/package.json index 285f223..eac1d7f 100644 --- a/lyricsnode/package.json +++ b/lyricsnode/package.json @@ -8,6 +8,7 @@ "dependencies": { "express": "3.3.4", "consolidate": "*", - "swig": "*" + "swig": "*", + "sqlite3": ">=2.1.15" } -} \ No newline at end of file +} diff --git a/lyricsnode/routes/round.js b/lyricsnode/routes/round.js index 64af49d..970a85a 100644 --- a/lyricsnode/routes/round.js +++ b/lyricsnode/routes/round.js @@ -1,6 +1,74 @@ /* * main page */ + +var sqlite3 = require('sqlite3'); +var path = require('path'); +var db; // make it global + +/** + * db name + */ +function dbName() +{ + return path.join(__dirname, '../../db/lyrics.sq3'); +} + +/** + * turn a phrase into a list of acronyms + */ +function acronymize(phrase) +{ + var letters = []; + phrase.split(' ').forEach(function(elem, idx, array) { + letters.push(elem[0].toUpperCase()); + }) + + return letters.join(''); +} + +/** + * Pick a song + */ +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; + } + + console.log('Pickling a song...'); + db.get('SELECT * FROM songs WHERE done_in IS NULL ORDER BY random() LIMIT 1', function(err, song) { + if (err) { + throw err; + } + + db.get('SELECT * FROM artists WHERE id = ' + song.artist, function(err, artist) { + if (err) { + throw err; + } + + db.get('SELECT * FROM lyrics WHERE song = ' + song.id, function(err, lyrics) { + if (err) { + throw err; + } + + params = {title: 'Round', + lyrics: lyrics.lyrics, + song_title: song.title, + song_artist: artist.name, + acronym_title: acronymize(song.title), + acronym_artist: acronymize(artist.name) + }; + res.render('index', params); + }); + }); + }); + }); +} + exports.index = function(req, res) { - res.render('index', { title: 'Round'} ); -} \ No newline at end of file + // res.render('index', { title: 'Round'} ); + getSong(res); +} diff --git a/lyricsnode/views/index.html b/lyricsnode/views/index.html index e3a767b..fe9e581 100644 --- a/lyricsnode/views/index.html +++ b/lyricsnode/views/index.html @@ -39,7 +39,7 @@
- +
{{ lyrics }}
@@ -61,13 +61,13 @@
- Artist:
- Song: + Artist: {{ acronym_artist }}
+ Song: {{ acronym_title }}
- Artist:
- Song: + Artist: {{ song_artist }}
+ Song: {{ song_title }}