Major Backend SI Update part 2: added controllers

This commit is contained in:
X4Lo committed 2025-02-15 22:42:57 +01:00
1 parent a51e207707
commit dbb00fdb61
38 files changed
+2129 -1231

No files matched your search

+7 -112
View File
@@ -1,121 +1,16 @@
const express = require('express');
const statisticsRouter = express.Router();
const sequelize = require('../config/database');
const { Op } = require('sequelize');
const EpisodeSeen = require('../models/EpisodeSeen');
const EntrySeen = require('../models/EntrySeen');
const Episode = require('../models/Episode');
const Entry = require('../models/Entry');
const statisticsController = require('../controllers/statisticsController');
// ! /statistics
// * GET - Total Watch Time
statisticsRouter.get('/watch-time', async (req, res) => {
const totalWatchTimeSeries = await EpisodeSeen.findOne({
include: [
{ model: Episode, as: 'episode', attributes: [] }
],
attributes: [
[sequelize.fn('SUM', sequelize.col('runtime')), 'watchTime']
],
});
// * GET - All Time, Watch Time
statisticsRouter.get('/watch-time', statisticsController.getAllTimeWatchTime);
const totalWatchTimeMovies = await EntrySeen.findOne({
include: [
{ model: Entry, as: 'entry', attributes: [] }
],
attributes: [
[sequelize.fn('SUM', sequelize.col('runtime')), 'watchTime']
],
});
// * GET - Yearly Watch Time
statisticsRouter.get('/watch-time/year/:year', statisticsController.getYearlyWatchTime);
const totalWatchTime = {
series: totalWatchTimeSeries.dataValues.watchTime,
movies: totalWatchTimeMovies.dataValues.watchTime
}
return res.status(200).json(totalWatchTime);
});
// * GET - Total Watch Time
statisticsRouter.get('/watch-time/year/:year', async (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);
});
// * GET - YEAR SEEN HISTORY GROUPED BY MONTH
statisticsRouter.get('/watch-count/year/:year', async (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 });
}
});
// * GET - Yearly Watch Count Grouped By Month
statisticsRouter.get('/watch-count/year/:year', statisticsController.getYearlyWatchCountGroupByMonth);
module.exports = statisticsRouter;