diff --git a/backend/controllers/entryController.js b/backend/controllers/entryController.js index 551c338..3b0c47b 100644 --- a/backend/controllers/entryController.js +++ b/backend/controllers/entryController.js @@ -99,6 +99,13 @@ class EntryController { 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; diff --git a/backend/controllers/seasonController.js b/backend/controllers/seasonController.js index 62ef442..4e2c224 100644 --- a/backend/controllers/seasonController.js +++ b/backend/controllers/seasonController.js @@ -1,4 +1,5 @@ const Episode = require("../models/Episode"); +const EpisodeSeen = require("../models/EpisodeSeen"); const Season = require("../models/Season"); @@ -53,7 +54,12 @@ class SeasonController { const episodes = await Episode.findAll({ where: { season_id: req.params.id - } + }, + include: [{ model: EpisodeSeen }], + order: [ + ['episode_number', 'ASC'], + [EpisodeSeen, 'date_seen', 'ASC'], + ], }); if (episodes) { diff --git a/backend/routes/entry.routes.js b/backend/routes/entry.routes.js index 0b417bf..5b0ce02 100644 --- a/backend/routes/entry.routes.js +++ b/backend/routes/entry.routes.js @@ -30,6 +30,7 @@ entryRouter.put('/:id/ids', entryController.updateEntryIds); entryRouter.delete('/:id', entryController.deleteEntry); entryRouter.get('/:id/seasons', entryController.getEntrySeasons); +entryRouter.get('/:id/credits', entryController.getEntryCredits); entryRouter.put('/:id/refresh', entryController.refrechEntry); entryRouter.put('/:id/fetch-collection', entryController.fetchEntryCollection); diff --git a/backend/services/entryService.js b/backend/services/entryService.js index f3c997c..8e1bb78 100644 --- a/backend/services/entryService.js +++ b/backend/services/entryService.js @@ -118,6 +118,36 @@ class EntryService { return entry; } + /** + * Retrieves ALL credits (cast + crew) for an entry with associated people. + * Used by the Cast & Crew page to lazy-render large credit lists. + * + * @param {number} id - The entry ID. + * @returns {Promise} Array of plain credit objects with `people` populated. + */ + async getEntryCredits(id) { + if (!id) { + throw new BadRequestError("Entry id not provided"); + } + + const entry = await Entry.findByPk(id, { attributes: ['id'] }); + if (!entry) { + throw new NotFoundError("Entry with the provided id was not found"); + } + + const credits = await Credit.findAll({ + where: { entry_id: id }, + include: [{ model: People, as: 'person' }], + order: [ + ['cast_type', 'ASC'], + ['order', 'ASC'], + ['episode_count', 'DESC'], + ], + }); + + return credits.map(c => c.get({ plain: true })); + } + async createEntry(data, plain = true) { // creating the object var is_tvseries = data.release_date == null ? true : false; @@ -454,4 +484,4 @@ class EntryService { } -module.exports = new EntryService(); \ No newline at end of file +module.exports = new EntryService(); diff --git a/docs-vault/API Clients.md b/docs-vault/API Clients.md index cf14aee..d6e2a07 100644 --- a/docs-vault/API Clients.md +++ b/docs-vault/API Clients.md @@ -30,6 +30,7 @@ Entry CRUD and management. | `refresh(id)` | PUT | `/entries/:id/refresh` | | `fetchCollection(id)` | PUT | `/entries/:id/fetch-collection` | | `getSeasons(id)` | GET | `/entries/:id/seasons` | +| `getCredits(id)` | GET | `/entries/:id/credits` | | `syncSeries()` | GET | `/entries/sync-series` | | `uploadMedia(id, formData)` | POST | `/entries/:id/media` | | `setMediaSelected(entryId, mediaId)` | PUT | `/entries/:entryId/media/:mediaId` | diff --git a/docs-vault/API Reference.md b/docs-vault/API Reference.md index a0dd01c..8ad6056 100644 --- a/docs-vault/API Reference.md +++ b/docs-vault/API Reference.md @@ -29,6 +29,7 @@ All endpoints are prefixed with `/api/v1`. See [[Backend]] for server setup. | `PUT` | `/entries/:id/ids` | Update external IDs | | `DELETE` | `/entries/:id` | Delete entry and all related data | | `GET` | `/entries/:id/seasons` | Get all seasons for a TV entry | +| `GET` | `/entries/:id/credits` | Get ALL credits (cast + crew) for an entry, with associated `person` populated. Used by [[Pages#EntryCastsPage\|EntryCastsPage]] | | `PUT` | `/entries/:id/refresh` | Force refresh from TMDB | | `PUT` | `/entries/:id/fetch-collection` | Fetch/create TMDB collection for entry | | `GET` | `/entries/sync-series` | Batch refresh all non-ended TV series | diff --git a/docs-vault/Pages.md b/docs-vault/Pages.md index ac5227f..789fa5e 100644 --- a/docs-vault/Pages.md +++ b/docs-vault/Pages.md @@ -74,10 +74,12 @@ Sections: Full cast & crew grid for an entry. Route: `/entry/:id/cast`. Width: `max-w-4xl`. - Breadcrumb: `Entry Title / Cast & Crew` (Title links back to EntryDetailsPage) +- Loads ALL credits (cast + crew) via `useEntryCredits` (`GET /entries/:id/credits`) — not limited to the 10-cast preview shown on EntryDetailsPage - Filter: All / Cast / Crew -- Sort: Credit Order, Popularity, Name, Birthday, Gender, Episode Count (TV only) +- Sort: Credit Order, Popularity, Name, Birthday, Gender, Episodes (TV only) - Sort direction toggle (asc/desc) - Shows role/job label and episode count for TV series +- **Lazy loading**: renders 24 credits per chunk; an `IntersectionObserver` sentinel auto-loads the next chunk on scroll - Links to [[Pages#PersonDetailsPage|PersonDetailsPage]] ## SeasonPage