Backend - Route all TMDB requests through a rate limiter (20 req/s, 8 concurrent) with 429/5xx retries; stop logging the API key - Fetch external ids, certification, keywords, videos, watch providers and alternative titles in the entry details request - Shared entry mapper used by create, refresh, season refresh and importer - New columns on entries, ids, ratings, seasons, episodes and people, with an idempotent startup migration (adds missing columns, widens types) - Fix TV IMDb ids, network country/logo, episode tmdb_id on refresh, and genre/network removal on refresh - Add stale-entries maintenance job (count, start, cancel) and live recommendations endpoint Frontend - Entry page: certification, tagline, creators, in-app trailer, vote count, TV/movie details, network logos, external links, keywords, where to watch, and recommendations - Episode page: directors, writers and guest stars - Settings: TMDB refresh card with live progress
165 lines
4.0 KiB
JavaScript
165 lines
4.0 KiB
JavaScript
const Episode = require("../models/Episode");
|
|
const EpisodeSeen = require("../models/EpisodeSeen");
|
|
const Season = require("../models/Season");
|
|
const Entry = require("../models/Entry");
|
|
const EntryId = require("../models/EntryId");
|
|
const { fetchSeasonData } = require("../services/tmdbService");
|
|
const {
|
|
mapSeasonFields,
|
|
mapEpisodeFields,
|
|
} = require("../mappers/entryMapper.js");
|
|
|
|
class SeasonController {
|
|
constructor() {
|
|
if (!SeasonController.instance) {
|
|
SeasonController.instance = this;
|
|
}
|
|
return SeasonController.instance;
|
|
}
|
|
|
|
async createSeason(req, res) {
|
|
try {
|
|
const season = await Season.create(req.body);
|
|
res.status(201).send(season);
|
|
} catch (error) {
|
|
res.status(400).send(error.message);
|
|
}
|
|
}
|
|
|
|
async getSeasons(req, res) {
|
|
try {
|
|
const seasons = await Season.findAll();
|
|
res.send(seasons);
|
|
} catch (error) {
|
|
res.status(500).send(error.message);
|
|
}
|
|
}
|
|
|
|
async getSeaonById(req, res) {
|
|
try {
|
|
const season = await Season.findByPk(req.params.id, {
|
|
include: [
|
|
{
|
|
model: Episode,
|
|
as: "episodes",
|
|
},
|
|
],
|
|
});
|
|
|
|
if (season) {
|
|
res.send(season);
|
|
} else {
|
|
res.status(404).send("Season not found");
|
|
}
|
|
} catch (error) {
|
|
res.status(500).send(error.message);
|
|
}
|
|
}
|
|
|
|
async getSeasonEpisodes(req, res) {
|
|
try {
|
|
const episodes = await Episode.findAll({
|
|
where: {
|
|
season_id: req.params.id,
|
|
},
|
|
include: [{ model: EpisodeSeen }],
|
|
order: [
|
|
["episode_number", "ASC"],
|
|
[EpisodeSeen, "date_seen", "ASC"],
|
|
],
|
|
});
|
|
|
|
if (episodes) {
|
|
res.send(episodes);
|
|
} else {
|
|
res.status(404).send("Episodes not found");
|
|
}
|
|
} catch (error) {
|
|
res.status(500).send(error.message);
|
|
}
|
|
}
|
|
|
|
async updateSeason(req, res) {
|
|
try {
|
|
const [updated] = await Season.update(req.body, {
|
|
where: { id: req.params.id },
|
|
});
|
|
if (updated) {
|
|
const updatedSeason = await Season.findByPk(req.params.id);
|
|
res.send(updatedSeason);
|
|
} else {
|
|
res.status(404).send("Season not found");
|
|
}
|
|
} catch (error) {
|
|
res.status(500).send(error.message);
|
|
}
|
|
}
|
|
|
|
async refreshSeason(req, res) {
|
|
try {
|
|
const season = await Season.findByPk(req.params.id, {
|
|
include: [
|
|
{
|
|
model: Entry,
|
|
as: "entry",
|
|
include: [{ model: EntryId, as: "entryId" }],
|
|
},
|
|
],
|
|
});
|
|
|
|
if (!season) {
|
|
return res.status(404).send("Season not found");
|
|
}
|
|
|
|
const tmdbTvId = season.entry?.entryId?.tmdb_id;
|
|
if (!tmdbTvId) {
|
|
return res.status(400).send("Entry has no TMDB ID");
|
|
}
|
|
|
|
const seasonData = await fetchSeasonData(tmdbTvId, season.season_number);
|
|
if (!seasonData) {
|
|
return res.status(500).send("Couldn't fetch data from TMDB");
|
|
}
|
|
|
|
await season.update(mapSeasonFields(seasonData));
|
|
|
|
// Keep the season's episodes in sync too (guest stars, crew, votes…)
|
|
for (const episode of seasonData.episodes ?? []) {
|
|
const existingEpisode = await Episode.findOne({
|
|
where: { tmdb_id: episode.id },
|
|
});
|
|
if (existingEpisode) {
|
|
await existingEpisode.update(mapEpisodeFields(episode));
|
|
} else {
|
|
await Episode.create({
|
|
...mapEpisodeFields(episode),
|
|
season_id: season.id,
|
|
});
|
|
}
|
|
}
|
|
|
|
const refreshedSeason = await Season.findByPk(req.params.id);
|
|
res.send(refreshedSeason);
|
|
} catch (error) {
|
|
res.status(500).send(error.message);
|
|
}
|
|
}
|
|
|
|
async deleteSeason(req, res) {
|
|
try {
|
|
const deleted = await Season.destroy({
|
|
where: { id: req.params.id },
|
|
});
|
|
if (deleted) {
|
|
res.status(204).send("Season deleted");
|
|
} else {
|
|
res.status(404).send("Season not found");
|
|
}
|
|
} catch (error) {
|
|
res.status(500).send(error.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = new SeasonController();
|