diff --git a/backend/controllers/peopleController.js b/backend/controllers/peopleController.js new file mode 100644 index 0000000..450a610 --- /dev/null +++ b/backend/controllers/peopleController.js @@ -0,0 +1,81 @@ +const peopleService = require("../services/peopleService"); +const { logToFile } = require("../services/logService"); + +const _peopleService = peopleService; + +class PeopleController { + constructor() { + if (!PeopleController.instance) { + PeopleController.instance = this; + } + return PeopleController.instance; + } + + async getAllPeople(req, res) { + const people = await _peopleService.getAllPeople(); + 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 _peopleService.getPersonById(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 } = req.body; + const person = await _peopleService.fetchAndSavePersonByTmdbId(tmdb_id); + 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 credits = await _peopleService.getPersonCredits(id); + res.status(200).json(credits); + } + + 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" }); + } + } +} + +module.exports = new PeopleController(); diff --git a/backend/routes/people.routes.js b/backend/routes/people.routes.js new file mode 100644 index 0000000..92128ae --- /dev/null +++ b/backend/routes/people.routes.js @@ -0,0 +1,72 @@ +const express = require('express'); +const peopleRouter = express.Router(); +const { peoplePicturesUpload } = require('../services/mutlerStorageLocations'); +const mediaOptimizerService = require('../services/mediaOptimizerService'); +const peopleController = require('../controllers/peopleController'); + +// ! /people + +// Get all people +peopleRouter.get('/', peopleController.getAllPeople); + +// Get popular people +peopleRouter.get('/popular', peopleController.getPopularPeople); + +// Search people +peopleRouter.get('/search', peopleController.searchPeople); + +// Get person by ID +peopleRouter.get('/:id', peopleController.getPersonById); + +// Create a new person +peopleRouter.post('/', peopleController.createPerson); + +// Fetch and save person from TMDB +peopleRouter.post('/fetch', peopleController.fetchAndSavePersonByTmdbId); + +// Update a person +peopleRouter.put('/:id', peopleController.updatePerson); + +// Refresh person data from TMDB +peopleRouter.put('/:id/refresh', peopleController.refreshPerson); + +// Delete a person +peopleRouter.delete('/:id', peopleController.deletePerson); + +// Get person credits +peopleRouter.get('/:id/credits', peopleController.getPersonCredits); + +// Person media upload handler +const upload = peoplePicturesUpload.fields([ + { name: 'profile', maxCount: 1 } +]); + +// Upload person profile picture +peopleRouter.post('/:id/media', upload, async (req, res) => { + try { + const personId = req.params.id; + let result = { success: true, files: [] }; + + // Handle profile picture + if (req.files.profile) { + const profileFile = req.files.profile[0]; + const filePath = profileFile.path; + const fileName = profileFile.filename; + + // Generate preview for profile picture + const previewPath = await mediaOptimizerService.doGeneratePreview('people', 'profiles', fileName); + + result.files.push({ + type: 'profile', + original: fileName, + preview: previewPath ? path.basename(previewPath) : null + }); + } + + res.status(200).json(result); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}); + +module.exports = peopleRouter; diff --git a/backend/routes/routes.js b/backend/routes/routes.js index 49d9468..6a99da0 100644 --- a/backend/routes/routes.js +++ b/backend/routes/routes.js @@ -11,6 +11,7 @@ const mediaRouter = require('./media.routes'); const episodeRouter = require('./episode.routes'); const pingRouter = require('./ping.routes'); const importRouter = require('./import.routes'); +const peopleRouter = require('./people.routes'); const router = express.Router(); router.use('/media', mediaRouter); @@ -28,5 +29,6 @@ router.use('/statistics', statisticsRouter); router.use('/settings', settingsRouter); router.use('/maintenance', maintenanceRouter); router.use('/ping', pingRouter); +router.use('/people', peopleRouter); module.exports = router; \ No newline at end of file diff --git a/backend/services/peopleService.js b/backend/services/peopleService.js new file mode 100644 index 0000000..1790ee0 --- /dev/null +++ b/backend/services/peopleService.js @@ -0,0 +1,183 @@ +const People = require("../models/People"); +const Credit = require("../models/Credit"); +const Entry = require("../models/Entry"); +const Media = require("../models/Media"); +const { fetchPersonData } = require("./tmdbService"); +const mediaDownloaderService = require("./mediaDownloaderService"); +const { logToFile } = require("./logService"); +const BadRequestError = require("../errors/BadRequestError"); +const NotFoundError = require("../errors/NotFoundError"); + +const _mediaDownloaderService = mediaDownloaderService; + +class PeopleService { + constructor() { + if (!PeopleService.instance) { + PeopleService.instance = this; + } + return PeopleService.instance; + } + + async getAllPeople(includeCrew = false) { + let where = { + where: { + cast_type: 'cast' + } + }; + + if(includeCrew){ + where = {}; + } + + const people = await People.findAll({ + order: [["popularity", "DESC"]], + include: [ + { + model: Credit, + as: "roles", + include: { + model: Entry, + as: "entry", + include: { model: Media, as: "medias" } + }, + ...where + } + ], + }); + + return people; + } + + async getPersonById(id) { + const person = await People.findByPk(id, { + include: [ + { + model: Credit, + as: "roles", + include: { + model: Entry, + as: "entry", + include: { model: Media, as: "medias" } + }, + } + ] + }); + + if (!person) { + throw new NotFoundError(`Person with ID ${id} not found`); + } + + return person; + } + + async getPersonByTmdbId(tmdbId) { + const person = await People.findOne({ + where: { tmdb_id: tmdbId }, + include: [ + { + model: Credit, + as: "roles", + include: { + model: Entry, + as: "entry", + include: { model: Media, as: "medias" } + } + }, + ] + }); + + return person; + } + + async createPerson(personData) { + try { + const existingPerson = await People.findOne({ + where: { tmdb_id: personData.tmdb_id } + }); + + if (existingPerson) { + return existingPerson; + } + + const person = await People.create(personData); + return person; + } catch (error) { + logToFile(`Error creating person: ${error.message}`); + throw error; + } + } + + async updatePerson(id, personData) { + const person = await People.findByPk(id); + + if (!person) { + throw new NotFoundError(`Person with ID ${id} not found`); + } + + await person.update(personData); + + return await this.getPersonById(id); + } + + async deletePerson(id) { + const person = await People.findByPk(id); + + if (!person) { + throw new NotFoundError(`Person with ID ${id} not found`); + } + + await person.destroy(); + return { success: true, message: `Person with ID ${id} was deleted` }; + } + + async fetchAndSavePersonByTmdbId(tmdbId) { + if (!tmdbId) { + throw new BadRequestError("TMDB ID is required"); + } + + // Check if person already exists in database + let person = await this.getPersonByTmdbId(tmdbId); + + if (person) { + // If person exists but hasn't been fully fetched, we'll update it + if (!person.fully_fetched) { + const personData = await fetchPersonData(tmdbId); + if (personData) { + person = await this.updatePerson(person.id, { + ...personData, + fully_fetched: true, + last_fetch: new Date(), + }); + + // Download profile image if available + if (personData.profile_path) { + await _mediaDownloaderService.downloadPersonMedia(person); + } + } + } + return person; + } + + // Fetch person data from TMDB + const personData = await fetchPersonData(tmdbId); + if (!personData) { + throw new NotFoundError(`Person with TMDB ID ${tmdbId} not found in TMDB`); + } + + // Create new person in database + person = await this.createPerson({ + ...personData, + fully_fetched: true, + last_fetch: new Date(), + }); + + // Download profile image if available + if (personData.profile_path) { + await _mediaDownloaderService.downloadPersonMedia(person); + } + + return person; + } +} + +module.exports = new PeopleService();