|
|
|
@ -4,18 +4,18 @@
|
|
|
|
|
|
|
|
|
|
var sqlite3 = require('sqlite3'); |
|
|
|
|
var path = require('path'); |
|
|
|
|
var db; // make it global
|
|
|
|
|
var db = null; // make it global
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* db name |
|
|
|
|
*/ |
|
|
|
|
/********************************************************************** |
|
|
|
|
* Helper functions |
|
|
|
|
*********************************************************************/ |
|
|
|
|
|
|
|
|
|
/** db name */ |
|
|
|
|
function dbName() { |
|
|
|
|
return path.join(__dirname, '../../db/lyrics.sq3'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* turn a phrase into a list of acronyms |
|
|
|
|
*/ |
|
|
|
|
/** turn a phrase into a list of acronyms */ |
|
|
|
|
function acronymize(phrase) { |
|
|
|
|
var letters = []; |
|
|
|
|
phrase.split(' ').forEach(function (elem, idx, array) { |
|
|
|
@ -25,31 +25,44 @@ function acronymize(phrase) {
|
|
|
|
|
return letters.join(''); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*********************************************************************** |
|
|
|
|
* Main page |
|
|
|
|
**********************************************************************/ |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Pick a song |
|
|
|
|
/********************************************************************** |
|
|
|
|
* Open the database |
|
|
|
|
*********************************************************************/ |
|
|
|
|
console.log('Opening the database ' + dbName() + '...'); |
|
|
|
|
db = new sqlite3.Database(dbName(), sqlite3.OPEN_READWRITE, function (error) { |
|
|
|
|
if (error) { |
|
|
|
|
db = null; // keep it null, to point that it is broken
|
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
/** small note: the db structure: |
|
|
|
|
* artists: |
|
|
|
|
* id int primary key |
|
|
|
|
* name text unique |
|
|
|
|
* |
|
|
|
|
* songs: |
|
|
|
|
* id int primary key |
|
|
|
|
* title text |
|
|
|
|
* artist int |
|
|
|
|
* done_in text |
|
|
|
|
* |
|
|
|
|
* lyrics: |
|
|
|
|
* id int primary key |
|
|
|
|
* song int |
|
|
|
|
* lyrics text |
|
|
|
|
*/ |
|
|
|
|
function getSong(res) { |
|
|
|
|
var name = dbName(); |
|
|
|
|
var db = null; |
|
|
|
|
|
|
|
|
|
/********************************************************************** |
|
|
|
|
* Select a song and display it |
|
|
|
|
*********************************************************************/ |
|
|
|
|
exports.index = function (req, res) { |
|
|
|
|
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(); |
|
|
|
|
}); |
|
|
|
|
if (!db) { |
|
|
|
|
res.render('db-error'); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function pickArtist() { |
|
|
|
@ -91,25 +104,78 @@ function getSong(res) {
|
|
|
|
|
res.render('index', params); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
console.log('Opening the database ' + name + '...'); |
|
|
|
|
db = new sqlite3.Database(name, sqlite3.OPEN_READWRITE, function (error) { |
|
|
|
|
if (error) { |
|
|
|
|
throw error; |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pickSong(); |
|
|
|
|
song = selectSong; |
|
|
|
|
console.log(song); |
|
|
|
|
pickArtist(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
exports.index = function (req, res) { |
|
|
|
|
getSong(res); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************** |
|
|
|
|
* Support for the small box with the "others" picks |
|
|
|
|
**********************************************************************/ |
|
|
|
|
|
|
|
|
|
exports.others = function (req, res) { |
|
|
|
|
console.log('Artist: ' + req.body.artist); |
|
|
|
|
console.log('Song: ' + req.body.song); |
|
|
|
|
var posted_artist = req.body.artist.toLowerCase(); |
|
|
|
|
var posted_song = req.body.song.toLowerCase(); |
|
|
|
|
|
|
|
|
|
console.log('Artist: ' + posted_artist); |
|
|
|
|
console.log('Song: ' + posted_song); |
|
|
|
|
|
|
|
|
|
var result = {status: '', message: ''} |
|
|
|
|
|
|
|
|
|
function insertSong(artist) { |
|
|
|
|
var now = new Date(); |
|
|
|
|
console.log('Adding song as played in ' + now.toJSON() + '...'); |
|
|
|
|
|
|
|
|
|
var stmt = db.run('INSERT INTO songs (id, title, artist, done_in) VALUES (NULL, ?, ?, ?)', [ |
|
|
|
|
posted_song, |
|
|
|
|
artist, |
|
|
|
|
now.toJSON() |
|
|
|
|
], function (error) { |
|
|
|
|
if (error) { |
|
|
|
|
// expected: artist AND song already picked
|
|
|
|
|
console.log('Insert failed, possible duplicate'); |
|
|
|
|
result.status = 'ERROR'; |
|
|
|
|
result.message = 'Song already picked'; |
|
|
|
|
} else { |
|
|
|
|
console.log('Insert succeded'); |
|
|
|
|
// things went alright
|
|
|
|
|
result.status = 'OK' |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
console.log(JSON.stringify(result)); |
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'}); |
|
|
|
|
res.write(JSON.stringify(result)); |
|
|
|
|
res.end(); |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var stmt = db.run('INSERT INTO artists (id, name) VALUES (NULL, ?)', [ |
|
|
|
|
posted_artist |
|
|
|
|
], |
|
|
|
|
function (error) { |
|
|
|
|
if (error) { |
|
|
|
|
// expected error: artist already exists
|
|
|
|
|
console.log('Artist exists, trying to find id...'); |
|
|
|
|
db.get('SELECT id FROM artists WHERE name = ?', [posted_artist], function (err, artist) { |
|
|
|
|
if (err) { |
|
|
|
|
throw err; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
console.log('Artist id = ' + artist.id); |
|
|
|
|
insertSong(artist.id); |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
console.log('New artist id = ' + this.lastID); |
|
|
|
|
insertSong(this.lastID); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
|