|
|
|
@ -25,6 +25,31 @@ function acronymize(phrase) {
|
|
|
|
|
return letters.join(''); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** return a json response */ |
|
|
|
|
function asJson(res, obj) { |
|
|
|
|
console.log(JSON.stringify(obj)); |
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'}); |
|
|
|
|
res.write(JSON.stringify(obj)); |
|
|
|
|
res.end(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** mark a song as played */ |
|
|
|
|
function markAsDone(song_id, after) { |
|
|
|
|
var now = new Date(); |
|
|
|
|
db.run('UPDATE songs SET done_in = ? WHERE id = ?', [ |
|
|
|
|
now.toJSON(), song_id |
|
|
|
|
], function (error) { |
|
|
|
|
if (error) { |
|
|
|
|
throw error; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (after) { |
|
|
|
|
after(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/********************************************************************** |
|
|
|
|
* Open the database |
|
|
|
|
*********************************************************************/ |
|
|
|
@ -55,7 +80,7 @@ db = new sqlite3.Database(dbName(), sqlite3.OPEN_READWRITE, function (error) {
|
|
|
|
|
/********************************************************************** |
|
|
|
|
* Select a song and display it |
|
|
|
|
*********************************************************************/ |
|
|
|
|
exports.index = function (req, res) { |
|
|
|
|
function index(req, res) { |
|
|
|
|
var song = null; |
|
|
|
|
var artist = null; |
|
|
|
|
var lyrics = null; |
|
|
|
@ -116,6 +141,8 @@ exports.index = function (req, res) {
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
exports.index = index; |
|
|
|
|
|
|
|
|
|
/*********************************************************************** |
|
|
|
|
* Support for the small box with the "others" picks |
|
|
|
|
**********************************************************************/ |
|
|
|
@ -127,7 +154,32 @@ exports.others = function (req, res) {
|
|
|
|
|
console.log('Artist: ' + posted_artist); |
|
|
|
|
console.log('Song: ' + posted_song); |
|
|
|
|
|
|
|
|
|
var result = {status: '', message: ''} |
|
|
|
|
var result = {status: '', message: ''}; |
|
|
|
|
|
|
|
|
|
function duplicateOrNotDone(err, song) { |
|
|
|
|
if (err) { |
|
|
|
|
throw err; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// the record will always exist, it's just a matter of "does it have a done_in"?
|
|
|
|
|
console.log('Song done in ' + song.done_in); |
|
|
|
|
|
|
|
|
|
if (song.done_in) { |
|
|
|
|
// yup, already picked that song
|
|
|
|
|
result.status = 'ERROR'; |
|
|
|
|
result.message = 'Song already picked'; |
|
|
|
|
|
|
|
|
|
asJson(res, result); |
|
|
|
|
} else { |
|
|
|
|
// it was one of your songs that someone else picked.
|
|
|
|
|
// just tell the user that and update it in the background
|
|
|
|
|
result.status = 'OK'; |
|
|
|
|
result.message = 'Someone picked one of your songs'; |
|
|
|
|
asJson(res, result); |
|
|
|
|
|
|
|
|
|
markAsDone(song.id); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function insertSong(artist) { |
|
|
|
|
var now = new Date(); |
|
|
|
@ -141,19 +193,18 @@ exports.others = function (req, res) {
|
|
|
|
|
if (error) { |
|
|
|
|
// expected: artist AND song already picked
|
|
|
|
|
console.log('Insert failed, possible duplicate'); |
|
|
|
|
result.status = 'ERROR'; |
|
|
|
|
result.message = 'Song already picked'; |
|
|
|
|
db.get('SELECT * FROM songs WHERE artist = ? AND title = ?', [ |
|
|
|
|
artist, |
|
|
|
|
posted_song |
|
|
|
|
], duplicateOrNotDone |
|
|
|
|
); |
|
|
|
|
} else { |
|
|
|
|
console.log('Insert succeded'); |
|
|
|
|
// things went alright
|
|
|
|
|
result.status = 'OK'; |
|
|
|
|
result.message = 'Added'; |
|
|
|
|
asJson(res, result); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
console.log(JSON.stringify(result)); |
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'}); |
|
|
|
|
res.write(JSON.stringify(result)); |
|
|
|
|
res.end(); |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
@ -186,13 +237,6 @@ exports.others = function (req, res) {
|
|
|
|
|
*********************************************************************/ |
|
|
|
|
|
|
|
|
|
exports.played = function (req, res) { |
|
|
|
|
var now = new Date(); |
|
|
|
|
db.run('UPDATE songs SET done_in = ? WHERE id = ?', [ |
|
|
|
|
now.toJSON(), req.body.id |
|
|
|
|
], function (error) { |
|
|
|
|
if (error) { |
|
|
|
|
throw error; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
markAsDone(req.body.id); |
|
|
|
|
index(req, res); |
|
|
|
|
}; |
|
|
|
|