various fixes to the entryRoute...
This commit is contained in:
1 parent
98f77b385c
commit
7fc1bdd875
3 files changed
+76
-16
No files matched your search
@@ -9,7 +9,6 @@ entryRatingsRouter.put('/:id', async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const { entryR } = req.body;
|
const { entryR } = req.body;
|
||||||
console.log(entryR)
|
|
||||||
|
|
||||||
const entryRating = await EntryRating.findByPk(id);
|
const entryRating = await EntryRating.findByPk(id);
|
||||||
if (!entryRating) {
|
if (!entryRating) {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ const Genre = require('../models/Genre');
|
|||||||
const EntrySeen = require('../models/EntrySeen');
|
const EntrySeen = require('../models/EntrySeen');
|
||||||
const { saveEntry, updateEntry } = require('../services/entryService');
|
const { saveEntry, updateEntry } = require('../services/entryService');
|
||||||
const Season = require('../models/Season');
|
const Season = require('../models/Season');
|
||||||
const { bulkImportMovies } = require('../services/importerService');
|
const { bulkImportMovies, bulkImportTmdb, bulkImportEpisodes } = require('../services/importerService');
|
||||||
const entryRouter = express.Router();
|
const entryRouter = express.Router();
|
||||||
|
|
||||||
// ! /entries
|
// ! /entries
|
||||||
@@ -18,11 +18,25 @@ entryRouter.get('/', async (req, res) => {
|
|||||||
const entries = await Entry.findAll({
|
const entries = await Entry.findAll({
|
||||||
include: [
|
include: [
|
||||||
{ model: EntryId, as: 'entryId' },
|
{ 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
|
// * GET ENTRY BY ID
|
||||||
@@ -91,6 +105,33 @@ entryRouter.post('/bulk-import-movies', async (req, res) => {
|
|||||||
res.status(201).json(entries);
|
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
|
// * PUT - ENTRY UPDATE
|
||||||
entryRouter.put('/:id', async (req, res) => {
|
entryRouter.put('/:id', async (req, res) => {
|
||||||
@@ -139,7 +180,20 @@ entryRouter.get('/:id/refrech', async (req, res) => {
|
|||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
// ? checking if the provied id is valid
|
// ? 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)
|
if (!oldEntry)
|
||||||
return res.status(404).json({ message: `Entry id not found` });
|
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
|
// ? getting new data from TMDB
|
||||||
const data = await fetchEntryData(oldEntry.entryId.tmdb_id, oldEntry.is_tvseries);
|
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` });
|
if (data == null) return res.status(500).json({ message: `Counldn't fetch data from TMDB` });
|
||||||
|
|
||||||
|
|
||||||
// ? updating
|
// ? updating
|
||||||
await updateEntry(oldEntry, data);
|
await updateEntry(oldEntry, data);
|
||||||
|
|
||||||
@@ -162,7 +214,7 @@ entryRouter.get('/:id/refrech', async (req, res) => {
|
|||||||
{
|
{
|
||||||
model: Genre,
|
model: Genre,
|
||||||
as: 'genres',
|
as: 'genres',
|
||||||
attributes: ['id'],
|
attributes: ['name'],
|
||||||
through: {
|
through: {
|
||||||
attributes: []
|
attributes: []
|
||||||
}
|
}
|
||||||
@@ -175,7 +227,12 @@ entryRouter.get('/:id/refrech', async (req, res) => {
|
|||||||
include: includes
|
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
|
// * DELETE - ENTRY BY ID
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { HttpClient } from '@angular/common/http';
|
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 { Entry } from '../../shared/models/entry.model';
|
||||||
import { EntryId } from '../../shared/models/entryId.model';
|
import { EntryId } from '../../shared/models/entryId.model';
|
||||||
import { EntryRating } from '../../shared/models/entryRating.model';
|
import { EntryRating } from '../../shared/models/entryRating.model';
|
||||||
@@ -76,13 +76,17 @@ export class TmdbService {
|
|||||||
getByKeyword(keyword: string, type: string, page: number) {
|
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`;
|
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(
|
return this.fetchdataNoCach(endPoint).pipe(
|
||||||
map((response) => {
|
switchMap((response) => from(response.results.map((movie: any) => this.adaptMovieToEntry(movie)))
|
||||||
return response.results.map((movie: any) =>
|
.pipe(
|
||||||
this.adaptMovieToEntry(movie)
|
toArray(),
|
||||||
);
|
map(entries => ({
|
||||||
})
|
entries: entries,
|
||||||
|
total_pages: response.total_pages,
|
||||||
|
total_results: response.total_results
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user