Major Backend SI Update part 2: added controllers
This commit is contained in:
1 parent
a51e207707
commit
dbb00fdb61
38 files changed
+2129
-1231
No files matched your search
@@ -0,0 +1,125 @@
|
||||
const sequelize = require('../config/database');
|
||||
const { Op } = require('sequelize');
|
||||
const Entry = require('../models/Entry');
|
||||
const EntrySeen = require('../models/EntrySeen');
|
||||
const Episode = require('../models/Episode');
|
||||
const EpisodeSeen = require('../models/EpisodeSeen');
|
||||
|
||||
class StatisticsController {
|
||||
|
||||
constructor() {
|
||||
if (!StatisticsController.instance) {
|
||||
StatisticsController.instance = this;
|
||||
}
|
||||
return StatisticsController.instance;
|
||||
}
|
||||
|
||||
async getAllTimeWatchTime(req, res) {
|
||||
const totalWatchTimeSeries = await EpisodeSeen.findOne({
|
||||
include: [
|
||||
{ model: Episode, as: 'episode', attributes: [] }
|
||||
],
|
||||
attributes: [
|
||||
[sequelize.fn('SUM', sequelize.col('runtime')), 'watchTime']
|
||||
],
|
||||
});
|
||||
|
||||
const totalWatchTimeMovies = await EntrySeen.findOne({
|
||||
include: [
|
||||
{ model: Entry, as: 'entry', attributes: [] }
|
||||
],
|
||||
attributes: [
|
||||
[sequelize.fn('SUM', sequelize.col('runtime')), 'watchTime']
|
||||
],
|
||||
});
|
||||
|
||||
const totalWatchTime = {
|
||||
series: totalWatchTimeSeries.dataValues.watchTime,
|
||||
movies: totalWatchTimeMovies.dataValues.watchTime
|
||||
}
|
||||
|
||||
return res.status(200).json(totalWatchTime);
|
||||
}
|
||||
|
||||
async getYearlyWatchTime(req, res) {
|
||||
const { year } = req.params;
|
||||
|
||||
const totalWatchTimeSeries = await EpisodeSeen.findAll({
|
||||
include: [
|
||||
{ model: Episode, as: 'episode', attributes: [] }
|
||||
],
|
||||
attributes: [
|
||||
[sequelize.fn('MONTH', sequelize.col('date_seen')), 'month'],
|
||||
[sequelize.fn('SUM', sequelize.col('runtime')), 'watchTime']
|
||||
],
|
||||
where: {
|
||||
date_seen: {
|
||||
[Op.between]: [new Date(year, 0, 1), new Date(year, 11, 31)]
|
||||
}
|
||||
},
|
||||
group: [sequelize.fn('MONTH', sequelize.col('date_seen'))]
|
||||
});
|
||||
|
||||
const totalWatchTimeMovies = await EntrySeen.findAll({
|
||||
include: [
|
||||
{ model: Entry, as: 'entry', attributes: [] }
|
||||
],
|
||||
attributes: [
|
||||
[sequelize.fn('MONTH', sequelize.col('date_seen')), 'month'],
|
||||
[sequelize.fn('SUM', sequelize.col('runtime')), 'watchTime']
|
||||
],
|
||||
where: {
|
||||
date_seen: {
|
||||
[Op.between]: [new Date(year, 0, 1), new Date(year, 11, 31)]
|
||||
}
|
||||
},
|
||||
group: [sequelize.fn('MONTH', sequelize.col('date_seen'))]
|
||||
});
|
||||
|
||||
const totalWatchTime = {
|
||||
series: totalWatchTimeSeries,
|
||||
movies: totalWatchTimeMovies
|
||||
}
|
||||
|
||||
return res.status(200).json(totalWatchTime);
|
||||
}
|
||||
|
||||
async getYearlyWatchCountGroupByMonth(req, res) {
|
||||
try {
|
||||
const { year } = req.params;
|
||||
const seriesCount = await EpisodeSeen.findAll({
|
||||
attributes: [
|
||||
[sequelize.fn('MONTH', sequelize.col('date_seen')), 'month'],
|
||||
[sequelize.fn('COUNT', sequelize.col('*')), 'count']
|
||||
],
|
||||
where: {
|
||||
date_seen: {
|
||||
[Op.between]: [new Date(year, 0, 1), new Date(year, 11, 31)]
|
||||
}
|
||||
},
|
||||
group: [sequelize.fn('MONTH', sequelize.col('date_seen'))]
|
||||
});
|
||||
|
||||
const moviesCount = await EntrySeen.findAll({
|
||||
attributes: [
|
||||
[sequelize.fn('MONTH', sequelize.col('date_seen')), 'month'],
|
||||
[sequelize.fn('COUNT', sequelize.col('*')), 'count']
|
||||
],
|
||||
where: {
|
||||
date_seen: {
|
||||
[Op.between]: [new Date(year, 0, 1), new Date(year, 11, 31)]
|
||||
}
|
||||
},
|
||||
group: [sequelize.fn('MONTH', sequelize.col('date_seen'))]
|
||||
});
|
||||
|
||||
res.status(200).json({ series: seriesCount, movies: moviesCount });
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = new StatisticsController();
|
||||
Reference in new issue
Block a user