- Return local_id in GET /people/tmdb/preview/:tmdbId if person exists in DB - Prevents duplicate "Add to Library" prompts for already-saved people
152 lines
5.0 KiB
JavaScript
152 lines
5.0 KiB
JavaScript
const peopleService = require("../services/peopleService");
|
|
const peopleManager = require("../services/peopleManager");
|
|
const tmdbService = require('../services/tmdbService');
|
|
|
|
const _peopleService = peopleService;
|
|
const _peopleManager = peopleManager;
|
|
|
|
class PeopleController {
|
|
constructor() {
|
|
if (!PeopleController.instance) {
|
|
PeopleController.instance = this;
|
|
}
|
|
return PeopleController.instance;
|
|
}
|
|
|
|
async getAllPeople(req, res) {
|
|
const { page, limit, search } = req.query;
|
|
const options = {};
|
|
if (page && limit) {
|
|
options.page = parseInt(page);
|
|
options.limit = parseInt(limit);
|
|
}
|
|
if (search) {
|
|
options.search = search;
|
|
}
|
|
const people = await _peopleService.getAllPeople(false, options);
|
|
res.status(200).json(people);
|
|
}
|
|
|
|
async getPopularPeople(req, res) {
|
|
const { limit } = req.query;
|
|
const people = await _peopleService.getPopularPeople(parseInt(limit) || 10);
|
|
res.status(200).json(people);
|
|
}
|
|
|
|
async getPersonById(req, res) {
|
|
const { id } = req.params;
|
|
const person = await _peopleManager.getUpdatedPerson(id);
|
|
res.status(200).json(person);
|
|
}
|
|
|
|
async createPerson(req, res) {
|
|
const personData = req.body;
|
|
const newPerson = await _peopleService.createPerson(personData);
|
|
res.status(201).json(newPerson);
|
|
}
|
|
|
|
async updatePerson(req, res) {
|
|
const { id } = req.params;
|
|
const personData = req.body;
|
|
const updatedPerson = await _peopleService.updatePerson(id, personData);
|
|
res.status(200).json(updatedPerson);
|
|
}
|
|
|
|
async deletePerson(req, res) {
|
|
const { id } = req.params;
|
|
const result = await _peopleService.deletePerson(id);
|
|
res.status(200).json(result);
|
|
}
|
|
|
|
async fetchAndSavePersonByTmdbId(req, res) {
|
|
const { tmdb_id, pinned } = req.body;
|
|
const person = await _peopleService.fetchAndSavePersonByTmdbId(tmdb_id, { pinned: !!pinned });
|
|
res.status(201).json(person);
|
|
}
|
|
|
|
async searchPeople(req, res) {
|
|
const { query } = req.query;
|
|
const people = await _peopleService.searchPeople(query);
|
|
res.status(200).json(people);
|
|
}
|
|
|
|
async getPersonCredits(req, res) {
|
|
const { id } = req.params;
|
|
const person = await _peopleService.getPersonById(id);
|
|
if (!person) return res.status(404).json({ message: 'Person not found' });
|
|
res.status(200).json(person.roles ?? []);
|
|
}
|
|
|
|
async getPersonTmdbFilmography(req, res) {
|
|
const { id } = req.params;
|
|
const person = await _peopleService.getPersonById(id);
|
|
if (!person || !person.tmdb_id) {
|
|
return res.status(404).json({ message: 'Person not found or missing TMDB ID' });
|
|
}
|
|
const data = await tmdbService.fetchPersonCombinedCredits(person.tmdb_id);
|
|
if (!data) return res.status(500).json({ message: 'Failed to fetch filmography from TMDB' });
|
|
res.status(200).json(data);
|
|
}
|
|
|
|
async refreshPerson(req, res) {
|
|
const { id } = req.params;
|
|
const person = await _peopleService.getPersonById(id);
|
|
|
|
if (person && person.tmdb_id) {
|
|
const updatedPerson = await _peopleService.fetchAndSavePersonByTmdbId(person.tmdb_id);
|
|
res.status(200).json(updatedPerson);
|
|
} else {
|
|
res.status(404).json({ message: "Person not found or missing TMDB ID" });
|
|
}
|
|
}
|
|
|
|
async getTmdbPopularPeople(req, res) {
|
|
const { page } = req.query;
|
|
const data = await tmdbService.fetchPopularPeople(page || 1);
|
|
if (!data) return res.status(500).json({ message: 'Failed to fetch popular people' });
|
|
res.status(200).json(data);
|
|
}
|
|
|
|
async searchTmdbPeople(req, res) {
|
|
const { query, page } = req.query;
|
|
if (!query) return res.status(400).json({ message: 'Query parameter is required' });
|
|
const data = await tmdbService.searchPeople(query, page || 1);
|
|
if (!data) return res.status(500).json({ message: 'Failed to search TMDB people' });
|
|
res.status(200).json(data);
|
|
}
|
|
|
|
async getTmdbPersonPreview(req, res) {
|
|
const { tmdbId } = req.params;
|
|
const personData = await tmdbService.fetchPersonDetails(tmdbId);
|
|
if (!personData) return res.status(404).json({ message: 'Person not found on TMDB' });
|
|
|
|
const filmography = await tmdbService.fetchPersonCombinedCredits(tmdbId);
|
|
|
|
// Check if person already exists in local DB
|
|
let localId = null;
|
|
try {
|
|
const localPerson = await _peopleService.getPersonByTmdbId(parseInt(tmdbId));
|
|
if (localPerson) localId = localPerson.id;
|
|
} catch (_) { /* not found */ }
|
|
|
|
const preview = {
|
|
tmdb_id: personData.id,
|
|
local_id: localId,
|
|
name: personData.name,
|
|
biography: personData.biography || null,
|
|
birthday: personData.birthday || null,
|
|
deathday: personData.deathday || null,
|
|
place_of_birth: personData.place_of_birth || null,
|
|
profile_path: personData.profile_path || null,
|
|
known_for_department: personData.known_for_department || null,
|
|
popularity: personData.popularity || null,
|
|
imdb_id: personData.imdb_id || personData.external_ids?.imdb_id || null,
|
|
filmography: filmography || { cast: [], crew: [] },
|
|
};
|
|
|
|
res.status(200).json(preview);
|
|
}
|
|
}
|
|
|
|
module.exports = new PeopleController();
|