Browse Source

finally displaying something

master
Julio Biason 11 years ago
parent
commit
091a9597ce
  1. 5
      lyricsnode/package.json
  2. 72
      lyricsnode/routes/round.js
  3. 10
      lyricsnode/views/index.html

5
lyricsnode/package.json

@ -8,6 +8,7 @@
"dependencies": {
"express": "3.3.4",
"consolidate": "*",
"swig": "*"
"swig": "*",
"sqlite3": ">=2.1.15"
}
}
}

72
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'} );
}
// res.render('index', { title: 'Round'} );
getSong(res);
}

10
lyricsnode/views/index.html

@ -39,7 +39,7 @@
<div class='row'>
<div class='large-6 columns' id='song'>
<textarea readonly>hello!</textarea>
<pre>{{ lyrics }}</pre>
</div>
<div class='large-6 columns' id='quick-add'>
@ -61,13 +61,13 @@
<div class='row'>
<div class='large-6 columns' id='pick'>
Artist: <span class='artist'></span><br/>
Song: <span class='song'></span>
Artist: <span class='artist'>{{ acronym_artist }}</span><br/>
Song: <span class='song'>{{ acronym_title }}</span>
</div>
<div class='large-6 columns' id='solution'>
Artist: <span class='artist'></span><br/>
Song: <span class='song'></span>
Artist: <span class='artist'>{{ song_artist }}</span><br/>
Song: <span class='song'>{{ song_title }}</span>
</div>
</div>

Loading…
Cancel
Save