Updated saveSong function

This commit is contained in:
dbroqua 2019-12-30 13:36:15 +01:00
parent a5a5d82969
commit 92825ab201
3 changed files with 29 additions and 39 deletions

View file

@ -1,7 +1,6 @@
const radio = require('radio-stream') const radio = require('radio-stream')
const request = require('request') const request = require('request')
const irc = require('irc-upd') const irc = require('irc-upd')
const Db = require('./libs/Db')
const models = require('./models').models const models = require('./models').models
const Help = require('./libs/Help') const Help = require('./libs/Help')
const ArtistSong = require('./libs/ArtistSong') const ArtistSong = require('./libs/ArtistSong')
@ -11,7 +10,6 @@ const List = require('./libs/List')
const allowedCommands = ['!artist', '!song', '!stats', '!when', '!help', '!notifications', '!list'] const allowedCommands = ['!artist', '!song', '!stats', '!when', '!help', '!notifications', '!list']
// Init des librairies // Init des librairies
const db = new Db(models)
const artistSong = new ArtistSong(models) const artistSong = new ArtistSong(models)
const when = new When(models) const when = new When(models)
const stats = new Statistics(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}`) bot.say(process.env.IRC_CHANNEL, `${process.env.RADIO_ALIAS} : ${currentSong.artist} - ${currentSong.title}`)
// Save song for history // Save song for history
db.saveSong({ artistSong.saveCurrent({
radio: process.env.RADIO_ALIAS, radio: process.env.RADIO_ALIAS,
artist: currentSong.artist, artist: currentSong.artist,
title: currentSong.title title: currentSong.title

View file

@ -5,6 +5,34 @@ class ArtistSong {
this.models = models 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 * Point d'entrée pour l'ajout suppression de favoris
* @param {String} type * @param {String} type

View file

@ -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