various fixes to the entryRoute...
This commit is contained in:
1 parent
98f77b385c
commit
7fc1bdd875
3 files changed
+77
-17
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user