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,73 @@
|
||||
const moment = require("../utils/moment");
|
||||
|
||||
const { Op } = require("sequelize")
|
||||
const Episode = require("../models/Episode");
|
||||
const Season = require("../models/Season");
|
||||
const Entry = require("../models/Entry");
|
||||
const EntryId = require("../models/EntryId");
|
||||
const Media = require("../models/Media");
|
||||
|
||||
const BadRequestError = require("../errors/BadRequestError");
|
||||
|
||||
class EpisodeService {
|
||||
|
||||
constructor() {
|
||||
if (!EpisodeService.instance) {
|
||||
EpisodeService.instance = this;
|
||||
}
|
||||
return EpisodeService.instance;
|
||||
}
|
||||
|
||||
async getEpisodesBetweenDates(startDate, endDate) {
|
||||
if (!startDate) {
|
||||
throw new BadRequestError("Start Date not provided");
|
||||
}
|
||||
|
||||
if (!endDate) {
|
||||
throw new BadRequestError("end Date not provided");
|
||||
}
|
||||
|
||||
const formatedStartDate = moment(startDate).format('YYYY-MM-DD');
|
||||
const formatedEndDate = moment(endDate).format('YYYY-MM-DD');
|
||||
|
||||
// fetch episodes that aired within the current month
|
||||
const episodes = await Episode.findAll({
|
||||
where: {
|
||||
air_date: {
|
||||
[Op.between]: [formatedStartDate, formatedEndDate]
|
||||
},
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: Season,
|
||||
as: 'season',
|
||||
include: {
|
||||
model: Entry,
|
||||
as: 'entry',
|
||||
include: [
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{
|
||||
model: Media,
|
||||
as: 'medias',
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
const modifiedEpisodes = episodes.map(episode => {
|
||||
const episodeJSON = episode.toJSON();
|
||||
if (episodeJSON.season && episodeJSON.season.entry) {
|
||||
episodeJSON.entry = episodeJSON.season.entry;
|
||||
episodeJSON.season.entry = null;
|
||||
}
|
||||
return episodeJSON;
|
||||
});
|
||||
|
||||
return modifiedEpisodes;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = new EpisodeService();
|
||||
Reference in new issue
Block a user