Files
Showy/backend/controllers/entryController.js
T
X4Lo 28eee34548 Unify entry page for TMDB previews and add artwork section
- Entry details page now also renders entries not in the library, using
  the TMDB preview mapped to the same entry shape; remove TmdbPreviewPage
- Preview endpoint returns an entry-shaped object, TMDB seasons and
  directors
- Artwork section: TMDB posters, backdrops and logos merged with the media
  table, with in-library badges, language filter and add-to-library
- Record tmdb_path on downloaded media so TMDB images can be matched
- Recommendations by TMDB id for entries not in the library
2026-11-05 14:06:46 +00:00

372 lines
11 KiB
JavaScript

const EntryRating = require("../models/EntryRating");
const collectionManager = require("../services/collectionManager");
const entryManager = require("../services/entryManager");
const entryService = require("../services/entryService");
const tmdbService = require("../services/tmdbService");
const artworkService = require("../services/artworkService");
const {
mapEntryFields,
mapEntryIds,
mapEntryRatings,
} = require("../mappers/entryMapper");
const asyncWrapper = require("../utils/asyncWrapper");
const _collectionManager = collectionManager;
const _entryManager = entryManager;
const _entryService = entryService;
class EntryController {
constructor() {
if (!EntryController.instance) {
EntryController.instance = this;
}
return EntryController.instance;
}
// * GET ENTRY BY ID
getAllEntries = asyncWrapper(async (req, res) => {
const entries = await _entryService.getAllEntries();
res.status(200).json(entries);
});
// * GET ENTRY BY ID
getEntryById = asyncWrapper(async (req, res) => {
const { id } = req.params;
let entry = await _entryManager.getUpdatedEntry(id);
res.status(200).json(entry);
});
// * POST - FETCH AND CREATE ENTRY USING TMDB ID
fetchAndSaveNewEntry = asyncWrapper(async (req, res) => {
const { tmdb_id, is_tvserie } = req.body;
const createdEntry = await _entryManager.fetchAndSaveNewEntry(
tmdb_id,
is_tvserie,
);
res.status(201).json(createdEntry);
});
// * PUT - ENTRY UPDATE
updateEntry = asyncWrapper(async (req, res) => {
const { id } = req.params;
const { entryData } = req.body;
const updatedEntry = await _entryService.updateEntryById(
id,
entryData,
true,
);
res.status(201).json(updatedEntry);
});
// * PUT - ENTRY Ratings UPDATE
updateEntryRatings = asyncWrapper(async (req, res) => {
const { id } = req.params;
const { entryRatings } = req.body;
const updatedEntryRatings = await _entryService.updateEntryRatings(
id,
entryRatings,
);
res.status(200).json(updatedEntryRatings);
});
// * PUT - ENTRY Ratings UPDATE
updateEntryIds = asyncWrapper(async (req, res) => {
const { id } = req.params;
const { entryIds } = req.body;
const updatedEntryIds = await _entryService.updateEntryRatings(
id,
entryIds,
);
res.status(200).json(updatedEntryIds);
});
// * DELETE - ENTRY BY ID
deleteEntry = asyncWrapper(async (req, res) => {
const { id } = req.params;
await _entryService.deleteEntryById(id);
res.status(200).json({ message: `Entry with ID ${id} deleted.` });
});
// * GET - SYNC ALL ENTIRES DATA
syncSeries = asyncWrapper(async (req, res, next) => {
const entries = await _entryManager.SyncSeriesData();
res.status(201).json(entries);
});
// * GET - Get seasons of an entry
getEntrySeasons = asyncWrapper(async (req, res) => {
const { id } = req.params;
const seasons = await _entryService.getEntrySeasons(id);
if (seasons.length == 0) {
return res.status(204).json({ message: "Provided entry has no seasons" });
}
res.status(200).json(seasons);
});
// * GET - Get ALL credits (cast + crew) for an entry
getEntryCredits = asyncWrapper(async (req, res) => {
const { id } = req.params;
const credits = await _entryService.getEntryCredits(id);
res.status(200).json(credits);
});
// * PUT - REQUEST ENTRY REFRECH
refrechEntry = asyncWrapper(async (req, res, next) => {
const { id } = req.params;
const entry = await _entryManager.refrechEntryById(id);
res.status(201).json(entry);
});
// * GET - TMDB RECOMMENDATIONS FOR A LIBRARY ENTRY (live, not stored)
getEntryRecommendations = asyncWrapper(async (req, res) => {
const entry = await _entryService.getEntryById(req.params.id, false);
const tmdbId = entry.entryId?.tmdb_id;
if (!tmdbId)
return res.status(400).json({ message: "Entry has no TMDB id" });
const data = await tmdbService.fetchEntryRecommendations(
tmdbId,
entry.is_tvseries,
req.query.page || 1,
);
if (!data)
return res
.status(500)
.json({ message: "Failed to fetch recommendations" });
res.status(200).json(data);
});
// * GET - TMDB RECOMMENDATIONS BY TMDB ID (entries not in the library)
getTmdbRecommendations = asyncWrapper(async (req, res) => {
const { type, tmdbId } = req.params;
const data = await tmdbService.fetchEntryRecommendations(
tmdbId,
type === "tv",
req.query.page || 1,
);
if (!data)
return res
.status(500)
.json({ message: "Failed to fetch recommendations" });
res.status(200).json(data);
});
// * GET - ARTWORK: TMDB images merged with the entry's media table
getEntryArtwork = asyncWrapper(async (req, res) => {
const artwork = await artworkService.getEntryArtwork(req.params.id);
res.status(200).json(artwork);
});
// * POST - ARTWORK: download a TMDB image into the entry's media table
addEntryArtwork = asyncWrapper(async (req, res) => {
const media = await artworkService.addEntryArtwork(req.params.id, req.body);
res.status(201).json(media);
});
// * GET - ARTWORK BY TMDB ID (entries not in the library)
getTmdbArtwork = asyncWrapper(async (req, res) => {
const { type, tmdbId } = req.params;
const artwork = await artworkService.getTmdbArtwork(type, tmdbId);
res.status(200).json(artwork);
});
// * PUT - REQUEST COLLECTION REFRECH
fetchEntryCollection = asyncWrapper(async (req, res, next) => {
const { id } = req.params;
const collection = await _collectionManager.fetchCollectionByEntryId(id);
if (collection) {
res.status(201).json(collection);
} else {
res.status(204);
}
});
// * GET - TMDB TRENDING
getTmdbTrending = asyncWrapper(async (req, res) => {
const { type } = req.params;
const { page } = req.query;
const data = await tmdbService.fetchTrending(type, page || 1);
if (!data)
return res.status(500).json({ message: "Failed to fetch trending data" });
res.status(200).json(data);
});
// * GET - TMDB POPULAR
getTmdbPopular = asyncWrapper(async (req, res) => {
const { type } = req.params;
const { page } = req.query;
const data = await tmdbService.fetchPopular(type, page || 1);
if (!data)
return res.status(500).json({ message: "Failed to fetch popular data" });
res.status(200).json(data);
});
// * GET - TMDB SEARCH
getTmdbSearch = asyncWrapper(async (req, res) => {
const { query, page } = req.query;
if (!query)
return res.status(400).json({ message: "Query parameter is required" });
const data = await tmdbService.searchMulti(query, page || 1);
if (!data)
return res.status(500).json({ message: "Failed to search TMDB" });
res.status(200).json(data);
});
// * GET - TMDB TOP RATED
getTmdbTopRated = asyncWrapper(async (req, res) => {
const { type } = req.params;
const { page } = req.query;
const data = await tmdbService.fetchTopRated(type, page || 1);
if (!data)
return res
.status(500)
.json({ message: "Failed to fetch top rated data" });
res.status(200).json(data);
});
// * GET - TMDB UPCOMING
getTmdbUpcoming = asyncWrapper(async (req, res) => {
const { page } = req.query;
const data = await tmdbService.fetchUpcoming(page || 1);
if (!data)
return res.status(500).json({ message: "Failed to fetch upcoming data" });
res.status(200).json(data);
});
// * GET - TMDB PREVIEW (fetch details without saving to library)
getTmdbPreview = asyncWrapper(async (req, res) => {
const { type, tmdbId } = req.params;
const is_tvserie = type === "tv";
const data = await tmdbService.fetchEntryData(tmdbId, is_tvserie);
if (!data)
return res.status(404).json({ message: "Entry not found on TMDB" });
// Fetch credits in parallel
const creditsData = await tmdbService.fetchEntryCredits(tmdbId, is_tvserie);
// Map credits to a shape similar to our Credit model
const credits = [];
if (creditsData?.cast) {
for (const c of creditsData.cast) {
credits.push({
id: c.credit_id || c.id,
cast_type: "cast",
character: is_tvserie
? c.roles?.[0]?.character || c.character || null
: c.character || null,
people_id: c.id,
people: {
id: c.id,
name: c.name || c.original_name,
profile_path: c.profile_path,
tmdb_id: c.id,
},
});
}
}
// Directors, so the preview page can show "Directed by"
for (const c of creditsData?.crew ?? []) {
const jobs = Array.isArray(c.jobs) ? c.jobs.map((j) => j.job) : [c.job];
if (!jobs.includes("Director")) continue;
credits.push({
id: c.credit_id || c.jobs?.[0]?.credit_id || "crew-" + c.id,
cast_type: "crew",
job: "Director",
department: "Directing",
character: null,
people_id: c.id,
people: {
id: c.id,
name: c.name || c.original_name,
profile_path: c.profile_path,
tmdb_id: c.id,
},
});
}
// Same shape as a library entry, so the details page can render either
const entry = {
id: 0,
...mapEntryFields(data),
is_tvseries: is_tvserie,
isInLibrary: false,
entryIds: { tmdb_id: data.id, ...mapEntryIds(data) },
entryRatings: {
user_rating: null,
imdb_rating: null,
rotten_tomatoes_rating: null,
...mapEntryRatings(data),
},
genres: data.genres || [],
networks: (data.networks || []).map((n) => ({
id: n.id,
name: n.name,
origin_country: n.origin_country || null,
logo_path: n.logo_path || null,
homepage: null,
})),
};
const seasons = (data.seasons || []).map((s) => ({
id: s.id,
name: s.name,
season_number: s.season_number,
episode_count: s.episode_count,
air_date: s.air_date,
poster_path: s.poster_path,
overview: s.overview,
tmdb_rating: s.vote_average,
}));
// Normalize to a shape similar to our Entry model
const preview = {
tmdb_id: data.id,
title: data.title || data.name,
original_title: data.original_title || data.original_name || null,
release_date: data.release_date || data.first_air_date || null,
runtime: data.runtime || data.episode_run_time?.[0] || null,
storyline: data.overview || null,
country:
data.origin_country?.[0] ||
data.production_countries?.[0]?.iso_3166_1 ||
null,
is_tvseries: is_tvserie,
status: data.status || null,
poster_path: data.poster_path || null,
backdrop_path: data.backdrop_path || null,
vote_average: data.vote_average || null,
genres: data.genres || [],
tagline: data.tagline || null,
number_of_seasons: data.number_of_seasons || null,
number_of_episodes: data.number_of_episodes || null,
networks: data.networks || [],
homepage: data.homepage || null,
imdb_id: data.imdb_id || data.external_ids?.imdb_id || null,
credits,
entry,
seasons,
};
res.status(200).json(preview);
});
}
module.exports = new EntryController();