irc-radio-bot/libs/Queries.js

129 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-12-25 13:47:42 +01:00
const moment = require('moment')
2019-12-25 13:10:13 +01:00
const formatString = require('../helpers/strings').formatString
class Queries {
constructor () {
this.filter = null
this.value = null
2019-12-25 13:47:42 +01:00
this.period = null
2019-12-25 13:10:13 +01:00
}
/**
* Fonction permettant de générer la query sur artist et title
* @param {Array} line
* @param {Integer} start
2019-12-25 13:47:42 +01:00
* @param {Boolean} artistAndTitle
2019-12-25 13:10:13 +01:00
*/
2019-12-25 13:47:42 +01:00
setBaseFilter (line, start, artistAndTitle) {
2019-12-25 13:10:13 +01:00
let value = ''
for (let i = start; i < line.length; i += 1) {
value += ' ' + line[i]
}
2019-12-25 13:47:42 +01:00
this.value = value.replace(' ', '')
value = formatString(this.value)
if (artistAndTitle) {
this.filter = {
$or: [
{
artist: value
},
{
title: value
}
]
}
} else {
this.filter = {
artist: value
}
2019-12-25 13:10:13 +01:00
}
}
2019-12-25 13:47:42 +01:00
/**
* Fonction permettant de générer le filtre de date sur les commandes !stats et !when
* @param {String} period
*/
setPeriod (period) {
switch (period) {
case 'day':
this.period = {
$gte: moment().startOf('day'),
$lte: moment().endOf('day')
}
break
case 'week':
this.period = {
$gte: moment().startOf('week'),
$lte: moment().endOf('day')
}
break
case 'month':
this.period = {
$gte: moment().startOf('month'),
$lte: moment().endOf('day')
}
break
case 'year':
this.period = {
$gte: moment().startOf('year'),
$lte: moment().endOf('day')
}
break
case 'lastday':
2020-01-13 14:26:18 +01:00
case 'yesterday':
2019-12-25 13:47:42 +01:00
this.period = {
2020-01-13 11:58:05 +01:00
$gte: moment().subtract(1, 'day').startOf('day'),
$lte: moment().subtract(1, 'day').endOf('day')
2019-12-25 13:47:42 +01:00
}
break
case 'lastweek':
this.period = {
$gte: moment().subtract(1, 'week').startOf('week'),
$lte: moment().subtract(1, 'week').endOf('week')
}
break
case 'lastmonth':
this.period = {
$gte: moment().subtract(1, 'month').startOf('month'),
$lte: moment().subtract(1, 'month').endOf('month')
}
break
case 'lastyear':
this.period = {
$gte: moment().subtract(1, 'year').startOf('year'),
$lte: moment().subtract(1, 'year').endOf('year')
}
break
default:
this.period = {}
}
}
2019-12-25 13:10:13 +01:00
/**
* Getter pour le filter
* @return {Object}
*/
getFilter () {
return this.filter
}
/**
* Getter pour le texte filtré
* @return {String}
*/
getValue () {
return this.value
}
2019-12-25 13:47:42 +01:00
/**
* Getter pour la période
* @return {String}
*/
getPeriod () {
return this.period
}
2019-12-25 13:10:13 +01:00
}
module.exports = Queries