From c8141fb87bd1fb75b8d6665968f3fefb88815929 Mon Sep 17 00:00:00 2001 From: X4Lo Date: Sat, 12 Jul 2025 15:29:43 +0100 Subject: [PATCH] Backend: minor changes --- backend/services/entryManager.js | 1 + backend/services/entryService.js | 39 ++++++++++++++++--------------- backend/services/peopleService.js | 36 +++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/backend/services/entryManager.js b/backend/services/entryManager.js index cf19943..308c941 100644 --- a/backend/services/entryManager.js +++ b/backend/services/entryManager.js @@ -457,6 +457,7 @@ class EntryManager { if (!oldEntry) throw NotFoundError('Entry Not Found'); + const entryData = await fetchEntryData(oldEntry.entryId.tmdb_id, oldEntry.is_tvseries); if (!entryData) diff --git a/backend/services/entryService.js b/backend/services/entryService.js index 42b6590..f17b1b9 100644 --- a/backend/services/entryService.js +++ b/backend/services/entryService.js @@ -62,6 +62,15 @@ class EntryService { return entriesWithGenreNames; } + /** + * Retrieves an entry by its ID, optionally as a plain object. + * + * @param {number} id - The entry ID to retrieve. + * @param {boolean} [plain=true] - Whether to return a plain object or a Sequelize instance. + * @returns {Promise} The entry data. + * @throws {BadRequestError} If no ID is provided. + * @throws {NotFoundError} If no entry is found with the given ID. + */ async getEntryById(id, plain = true) { if (!id) { throw new BadRequestError("Entry id not provided"); @@ -77,44 +86,36 @@ class EntryService { { model: Network, as: 'networks' }, { model: Cast, as: 'credits', - include: [ - { model: People, as: 'person' } - ] + where: { cast_type: 'cast' }, + order: [['order', 'ASC']], + limit: 10, + include: [{ model: People, as: 'person' }] }, { model: Collection, as: 'collections', - include: [ - { model: Media, as: 'medias' } - ] + include: [{ model: Media, as: 'medias' }] }, { model: Genre, as: 'genres', attributes: ['name', 'id'], - through: { - attributes: [] // Do not fetch any attributes from the join table - } + through: { attributes: [] } } ], - order: [ - [{ model: EntrySeen, as: 'entrySeen' }, 'date_seen', 'DESC'] // Order the entry-seen list - ] + order: [[{ model: EntrySeen, as: 'entrySeen' }, 'date_seen', 'DESC']] }); if (!entry) { - throw new NotFoundError("Entry with the provided id was not found") + throw new NotFoundError("Entry with the provided id was not found"); } if (plain) { const plainEntry = entry.get({ plain: true }); - const genres = plainEntry.genres.map(genre => genre.name); - - plainEntry.genres = genres; - + plainEntry.genres = plainEntry.genres.map(g => g.name); return plainEntry; - } else { - return entry; } + + return entry; } async createEntry(data, plain = true) { diff --git a/backend/services/peopleService.js b/backend/services/peopleService.js index 1790ee0..13c47b2 100644 --- a/backend/services/peopleService.js +++ b/backend/services/peopleService.js @@ -25,7 +25,7 @@ class PeopleService { } }; - if(includeCrew){ + if (includeCrew) { where = {}; } @@ -41,7 +41,8 @@ class PeopleService { include: { model: Media, as: "medias" } }, ...where - } + }, + { model: Media, as: 'medias' } ], }); @@ -59,7 +60,8 @@ class PeopleService { as: "entry", include: { model: Media, as: "medias" } }, - } + }, + { model: Media, as: 'medias' } ] }); @@ -83,6 +85,7 @@ class PeopleService { include: { model: Media, as: "medias" } } }, + { model: Media, as: 'medias' } ] }); @@ -107,6 +110,33 @@ class PeopleService { } } + async createOrUpdatePerson(personData) { + return await People.findOrCreate({ + where: { tmdb_id: personData.tmdb_id }, + defaults: { + name: personData.name, + biography: personData.biography, + birthday: personData.birthday, + deathday: personData.deathday, + place_of_birth: personData.place_of_birth, + profile_path: personData.profile_path, + popularity: personData.popularity, + fully_fetched: true, + last_fetch: new Date(), + }, + include: [ + { + model: Credit, as: 'roles', + include: { + model: Entry, as: 'entry', + include: [{ model: Media, as: 'medias' }] + } + }, + { model: Media, as: 'medias' } + ] + }); + } + async updatePerson(id, personData) { const person = await People.findByPk(id);