From 92825ab20105cdf8c8a484cb899bdc196b303c9b Mon Sep 17 00:00:00 2001 From: dbroqua Date: Mon, 30 Dec 2019 13:36:15 +0100 Subject: [PATCH] Updated saveSong function --- index.js | 4 +--- libs/ArtistSong.js | 28 ++++++++++++++++++++++++++++ libs/Db.js | 36 ------------------------------------ 3 files changed, 29 insertions(+), 39 deletions(-) delete mode 100644 libs/Db.js diff --git a/index.js b/index.js index dce9d7f..35c08f9 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,6 @@ const radio = require('radio-stream') const request = require('request') const irc = require('irc-upd') -const Db = require('./libs/Db') const models = require('./models').models const Help = require('./libs/Help') const ArtistSong = require('./libs/ArtistSong') @@ -11,7 +10,6 @@ const List = require('./libs/List') const allowedCommands = ['!artist', '!song', '!stats', '!when', '!help', '!notifications', '!list'] // Init des librairies -const db = new Db(models) const artistSong = new ArtistSong(models) const when = new When(models) const stats = new Statistics(models) @@ -44,7 +42,7 @@ const checkCurrentSong = () => { bot.say(process.env.IRC_CHANNEL, `${process.env.RADIO_ALIAS} : ${currentSong.artist} - ${currentSong.title}`) // Save song for history - db.saveSong({ + artistSong.saveCurrent({ radio: process.env.RADIO_ALIAS, artist: currentSong.artist, title: currentSong.title diff --git a/libs/ArtistSong.js b/libs/ArtistSong.js index e49fa5c..aec5703 100644 --- a/libs/ArtistSong.js +++ b/libs/ArtistSong.js @@ -5,6 +5,34 @@ class ArtistSong { this.models = models } + /** + * Fonction sauvegardant en historique le morceau en cours de lecture + * @param {Object} value + */ + saveCurrent (value) { + // Find if previous song was the same (on bot reload) + this.models.Histories + .find({}) + .sort({ + createdAt: 'desc' + }) + .limit(1) + .exec((err, last) => { + if (err || + last.length === 0 || + (last[0] !== undefined && + last[0].artist !== value.artist && + last[0].title !== value.title + ) + ) { + console.log('Save song!', value) + // Previous song was different => save song! + const history = new this.models.Histories(value) + history.save() + } + }) + } + /** * Point d'entrée pour l'ajout suppression de favoris * @param {String} type diff --git a/libs/Db.js b/libs/Db.js deleted file mode 100644 index e117cab..0000000 --- a/libs/Db.js +++ /dev/null @@ -1,36 +0,0 @@ - -class Db { - constructor (models) { - this.models = models - } - - /** - * Fonction sauvegardant en historique le morceau en cours de lecture - * @param {Object} value - */ - saveSong (value) { - // Find if previous song was the same (on bot reload) - this.models.Histories - .find({}) - .sort({ - createdAt: 'desc' - }) - .limit(1) - .exec((err, last) => { - if (err || - last.length === 0 || - (last[0] !== undefined && - last[0].artist !== value.artist && - last[0].title !== value.title - ) - ) { - console.log('Save song!', value) - // Previous song was different => save song! - const history = new this.models.Histories(value) - history.save() - } - }) - } -} - -module.exports = Db