diff --git a/backend/routes/entryRatingsRoutes.js b/backend/routes/entryRatingsRoutes.js index 410463e..0d9fac5 100644 --- a/backend/routes/entryRatingsRoutes.js +++ b/backend/routes/entryRatingsRoutes.js @@ -9,7 +9,6 @@ entryRatingsRouter.put('/:id', async (req, res) => { try { const { id } = req.params; const { entryR } = req.body; - console.log(entryR) const entryRating = await EntryRating.findByPk(id); if (!entryRating) { diff --git a/backend/routes/entryRoutes.js b/backend/routes/entryRoutes.js index e9dcc79..1395665 100644 --- a/backend/routes/entryRoutes.js +++ b/backend/routes/entryRoutes.js @@ -7,7 +7,7 @@ const Genre = require('../models/Genre'); const EntrySeen = require('../models/EntrySeen'); const { saveEntry, updateEntry } = require('../services/entryService'); const Season = require('../models/Season'); -const { bulkImportMovies } = require('../services/importerService'); +const { bulkImportMovies, bulkImportTmdb, bulkImportEpisodes } = require('../services/importerService'); const entryRouter = express.Router(); // ! /entries @@ -18,11 +18,25 @@ entryRouter.get('/', async (req, res) => { const entries = await Entry.findAll({ include: [ { model: EntryId, as: 'entryId' }, - { model: EntryRating, as: 'entryRating' } + { model: EntryRating, as: 'entryRating' }, + { + model: Genre, + as: 'genres', + attributes: ['name'], // Only fetch the genre ID + through: { + attributes: [] // Do not fetch any attributes from the join table + } + } ] }); - res.status(200).json(entries); + // converting genres from object list to string list + const entriesWithGenreNames = entries.map(entry => ({ + ...entry.get({ plain: true }), + genres: entry.genres.map(genre => genre.name) // Convert genre objects to string array + })); + + res.status(200).json(entriesWithGenreNames); }); // * GET ENTRY BY ID @@ -91,6 +105,33 @@ entryRouter.post('/bulk-import-movies', async (req, res) => { res.status(201).json(entries); }); +entryRouter.post('/bulk-import-tmdb', async (req, res) => { + const { values } = req.body; + + let entries = []; + try { + entries = await bulkImportTmdb(values); + } catch (error) { + res.status(500); + } + + res.status(201).json(entries); +}); + +entryRouter.post('/bulk-import-episodes-seen', async (req, res) => { + const { values } = req.body; + + let createdRows = 0; + try { + createdRows = await bulkImportEpisodes(values); + console.log(`Episodes Seen - counts ${episodes.length}`) + } catch (error) { + res.status(500); + } + + res.status(201).json(createdRows); +}); + // * PUT - ENTRY UPDATE entryRouter.put('/:id', async (req, res) => { @@ -139,7 +180,20 @@ entryRouter.get('/:id/refrech', async (req, res) => { const { id } = req.params; // ? checking if the provied id is valid - const oldEntry = await Entry.findByPk(id); + const oldEntry = await Entry.findByPk(id, { + include: [ + { model: EntryId, as: 'entryId' }, + { model: EntryRating, as: 'entryRating' }, + { + model: Genre, + as: 'genres', + attributes: ['id'], + through: { + attributes: [] + } + } + ] + }); if (!oldEntry) return res.status(404).json({ message: `Entry id not found` }); @@ -147,10 +201,8 @@ entryRouter.get('/:id/refrech', async (req, res) => { // ? getting new data from TMDB const data = await fetchEntryData(oldEntry.entryId.tmdb_id, oldEntry.is_tvseries); - if (data == null) return res.status(500).json({ message: `Counldn't fetch data from TMDB` }); - // ? updating await updateEntry(oldEntry, data); @@ -162,7 +214,7 @@ entryRouter.get('/:id/refrech', async (req, res) => { { model: Genre, as: 'genres', - attributes: ['id'], + attributes: ['name'], through: { attributes: [] } @@ -175,7 +227,12 @@ entryRouter.get('/:id/refrech', async (req, res) => { include: includes }); - res.status(201).json(entry); + const entryData = entry.get({ plain: true }); + const genres = entryData.genres.map(genre => genre.name); + + entryData.genres = genres; + + res.status(201).json(entryData); }); // * DELETE - ENTRY BY ID diff --git a/frontend/src/app/core/services/tmdb.service.ts b/frontend/src/app/core/services/tmdb.service.ts index 7aefdee..30cbf6c 100644 --- a/frontend/src/app/core/services/tmdb.service.ts +++ b/frontend/src/app/core/services/tmdb.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; -import { Observable, map, of } from 'rxjs'; +import { Observable, from, map, of, switchMap, toArray } from 'rxjs'; import { Entry } from '../../shared/models/entry.model'; import { EntryId } from '../../shared/models/entryId.model'; import { EntryRating } from '../../shared/models/entryRating.model'; @@ -75,14 +75,18 @@ export class TmdbService { getByKeyword(keyword: string, type: string, page: number) { var endPoint = `${this.tmdb_apiUrl}/search/${type}?query=${keyword}&page=${page}&api_key=${this.tmdb_apiKey}&language=en-US`; - - // return this.getEntriesUsingEndPoint(endPoint); + return this.fetchdataNoCach(endPoint).pipe( - map((response) => { - return response.results.map((movie: any) => - this.adaptMovieToEntry(movie) - ); - }) + switchMap((response) => from(response.results.map((movie: any) => this.adaptMovieToEntry(movie))) + .pipe( + toArray(), + map(entries => ({ + entries: entries, + total_pages: response.total_pages, + total_results: response.total_results + })) + ) + ) ); }