add media download service and started using it
This commit is contained in:
1 parent
78399912f7
commit
06be888123
12 files changed
+614
-80
No files matched your search
@@ -5,9 +5,10 @@ const EntryId = require('../models/EntryId');
|
||||
const EntryRating = require('../models/EntryRating');
|
||||
const Genre = require('../models/Genre');
|
||||
const EntrySeen = require('../models/EntrySeen');
|
||||
const { saveEntry, updateEntry } = require('../services/entryService');
|
||||
const { saveEntry, refrechEntry } = require('../services/entryService');
|
||||
const Season = require('../models/Season');
|
||||
const { bulkImportMovies, bulkImportTmdb, bulkImportEpisodes } = require('../services/importerService');
|
||||
const Media = require('../models/Media');
|
||||
const entryRouter = express.Router();
|
||||
|
||||
// ! /entries
|
||||
@@ -19,6 +20,7 @@ entryRouter.get('/', async (req, res) => {
|
||||
include: [
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{ model: EntryRating, as: 'entryRating' },
|
||||
{ model: Media, as: 'medias' },
|
||||
{
|
||||
model: Genre,
|
||||
as: 'genres',
|
||||
@@ -54,6 +56,7 @@ entryRouter.get('/:id', async (req, res) => {
|
||||
{ model: EntryRating, as: 'entryRating' },
|
||||
{ model: EntrySeen, as: 'entrySeen' },
|
||||
{ model: Season, as: 'seasons' },
|
||||
{ model: Media, as: 'medias' },
|
||||
{
|
||||
model: Genre,
|
||||
as: 'genres',
|
||||
@@ -155,6 +158,7 @@ entryRouter.put('/:id', async (req, res) => {
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{ model: EntryRating, as: 'entryRating' },
|
||||
{ model: EntrySeen, as: 'entrySeen' },
|
||||
{ model: Media, as: 'medias' },
|
||||
{
|
||||
model: Genre,
|
||||
as: 'genres',
|
||||
@@ -180,63 +184,73 @@ entryRouter.put('/:id', async (req, res) => {
|
||||
});
|
||||
|
||||
// * GET - REQUEST ENTRY REFRECH
|
||||
entryRouter.get('/:id/refrech', async (req, res) => {
|
||||
entryRouter.get('/:id/refrech', async (req, res, next) => {
|
||||
const { id } = req.params;
|
||||
|
||||
// ? checking if the provied id is valid
|
||||
// ! note: the includes are required for the update
|
||||
const oldEntry = await Entry.findByPk(id, {
|
||||
include: [
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{ model: EntryRating, as: 'entryRating' },
|
||||
{ model: EntrySeen, as: 'entrySeen' },
|
||||
{ model: Season, as: 'seasons' },
|
||||
{
|
||||
model: Genre,
|
||||
as: 'genres',
|
||||
attributes: ['id'],
|
||||
through: {
|
||||
attributes: []
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
try {
|
||||
const entry = await refrechEntry(id);
|
||||
|
||||
if (!oldEntry)
|
||||
return res.status(404).json({ message: `Entry id not found` });
|
||||
res.status(201).json(entry);
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
|
||||
// ? 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` });
|
||||
// // ? checking if the provied id is valid
|
||||
// // ! note: the includes are required for the update
|
||||
// const oldEntry = await Entry.findByPk(id, {
|
||||
// include: [
|
||||
// { model: EntryId, as: 'entryId' },
|
||||
// { model: EntryRating, as: 'entryRating' },
|
||||
// { model: EntrySeen, as: 'entrySeen' },
|
||||
// { model: Season, as: 'seasons' },
|
||||
// { model: Media, as: 'medias' },
|
||||
// {
|
||||
// model: Genre,
|
||||
// as: 'genres',
|
||||
// attributes: ['id'],
|
||||
// through: {
|
||||
// attributes: []
|
||||
// }
|
||||
// }
|
||||
// ]
|
||||
// });
|
||||
|
||||
// ? updating
|
||||
await updateEntry(oldEntry, data);
|
||||
// if (!oldEntry)
|
||||
// return res.status(404).json({ message: `Entry id not found` });
|
||||
|
||||
// ? returning a new entry with updated values
|
||||
let includes = [
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{ model: EntryRating, as: 'entryRating' },
|
||||
{ model: EntrySeen, as: 'entrySeen' },
|
||||
{ model: Season, as: 'seasons' },
|
||||
{
|
||||
model: Genre,
|
||||
as: 'genres',
|
||||
attributes: ['name'],
|
||||
through: {
|
||||
attributes: []
|
||||
}
|
||||
}
|
||||
];
|
||||
if (oldEntry.is_tvseries) includes.push({ model: Season, as: 'seasons' });
|
||||
// // ? 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` });
|
||||
|
||||
const updatedEntry = await Entry.findByPk(id, {
|
||||
include: includes
|
||||
});
|
||||
// // ? updating
|
||||
// await updateEntry(oldEntry, data);
|
||||
|
||||
const entryData = updatedEntry.get({ plain: true });
|
||||
entryData.genres = entryData.genres.map(genre => genre.name);
|
||||
// // ? returning a new entry with updated values
|
||||
// let includes = [
|
||||
// { model: EntryId, as: 'entryId' },
|
||||
// { model: EntryRating, as: 'entryRating' },
|
||||
// { model: EntrySeen, as: 'entrySeen' },
|
||||
// { model: Season, as: 'seasons' },
|
||||
// { model: Media, as: 'medias' },
|
||||
// {
|
||||
// model: Genre,
|
||||
// as: 'genres',
|
||||
// attributes: ['name'],
|
||||
// through: {
|
||||
// attributes: []
|
||||
// }
|
||||
// }
|
||||
// ];
|
||||
// if (oldEntry.is_tvseries) includes.push({ model: Season, as: 'seasons' });
|
||||
|
||||
res.status(201).json(entryData);
|
||||
// const updatedEntry = await Entry.findByPk(id, {
|
||||
// include: includes
|
||||
// });
|
||||
|
||||
// const entryData = updatedEntry.get({ plain: true });
|
||||
// entryData.genres = entryData.genres.map(genre => genre.name);
|
||||
|
||||
// res.status(201).json(entryData);
|
||||
});
|
||||
|
||||
// * DELETE - ENTRY BY ID
|
||||
|
||||
@@ -6,6 +6,7 @@ const EntryId = require('../models/EntryId');
|
||||
const EntryRating = require('../models/EntryRating');
|
||||
const Season = require('../models/Season');
|
||||
const Genre = require('../models/Genre');
|
||||
const Media = require('../models/Media');
|
||||
|
||||
const { Sequelize } = require('sequelize');
|
||||
const router = express.Router();
|
||||
@@ -51,6 +52,7 @@ router.get('/:id', async (req, res) => {
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{ model: EntryRating, as: 'entryRating' },
|
||||
{ model: Season, as: 'seasons' },
|
||||
{ model: Media, as: 'medias' },
|
||||
{ model: Genre, as: 'genres' },
|
||||
],
|
||||
through: {
|
||||
@@ -203,7 +205,7 @@ router.patch('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
}
|
||||
|
||||
// ? updating
|
||||
const updated = await collectionEntry.update({ order: newOrder });
|
||||
await collectionEntry.update({ order: newOrder });
|
||||
|
||||
res.status(200).json({ message: 'UserCollectionEntry order updated successfully.' });
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user