Back: entry service add: entry media routes, refrech collection, get collection on create, sync series route
This commit is contained in:
1 parent
41666cc6a3
commit
f73deb4f2d
1 file changed
+159
-101
+159
-101
@@ -1,8 +1,11 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const entryRouter = express.Router();
|
const entryRouter = express.Router();
|
||||||
|
const sharp = require('sharp');
|
||||||
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
const { fetchEntryData } = require('../services/tmdbService');
|
const { fetchEntryData, fetchCollectionData } = require('../services/tmdbService');
|
||||||
const { saveEntry, refrechEntry } = require('../services/entryService');
|
const { saveEntry, refrechEntry, SyncSeriesData } = require('../services/models-services/entryService');
|
||||||
const { bulkImportMovies, bulkImportTmdb, bulkImportEpisodes } = require('../services/importerService');
|
const { bulkImportMovies, bulkImportTmdb, bulkImportEpisodes } = require('../services/importerService');
|
||||||
|
|
||||||
const Entry = require('../models/Entry');
|
const Entry = require('../models/Entry');
|
||||||
@@ -14,6 +17,9 @@ const Season = require('../models/Season');
|
|||||||
const Media = require('../models/Media');
|
const Media = require('../models/Media');
|
||||||
const Episode = require('../models/Episode');
|
const Episode = require('../models/Episode');
|
||||||
const { entryPicturesUpload } = require('../services/mutlerStorageLocations');
|
const { entryPicturesUpload } = require('../services/mutlerStorageLocations');
|
||||||
|
const { generatePreview } = require('../services/mediaService');
|
||||||
|
const UserCollection = require('../models/UserCollection');
|
||||||
|
const { Sequelize } = require('sequelize');
|
||||||
|
|
||||||
// ! /entries
|
// ! /entries
|
||||||
|
|
||||||
@@ -47,6 +53,18 @@ entryRouter.get('/', async (req, res) => {
|
|||||||
res.status(200).json(entriesWithGenreNames);
|
res.status(200).json(entriesWithGenreNames);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// * GET - SYNC ALL ENTIRES DATA
|
||||||
|
entryRouter.get('/sync-series', async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const entries = await SyncSeriesData();
|
||||||
|
|
||||||
|
res.status(201).json(entries);
|
||||||
|
} catch (error) {
|
||||||
|
next(error)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// * GET ENTRY BY ID
|
// * GET ENTRY BY ID
|
||||||
entryRouter.get('/:id', async (req, res) => {
|
entryRouter.get('/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@@ -136,8 +154,42 @@ entryRouter.get('/:id/seasons', async (req, res) => {
|
|||||||
entryRouter.post('/', async (req, res) => {
|
entryRouter.post('/', async (req, res) => {
|
||||||
const { tmdb_id, is_tvserie } = req.body;
|
const { tmdb_id, is_tvserie } = req.body;
|
||||||
|
|
||||||
const data = await fetchEntryData(tmdb_id, is_tvserie);
|
const entryData = await fetchEntryData(tmdb_id, is_tvserie);
|
||||||
const entry = await saveEntry(data);
|
const entry = await saveEntry(entryData);
|
||||||
|
|
||||||
|
if (entryData.belongs_to_collection) {
|
||||||
|
let collectionData = entryData.belongs_to_collection;
|
||||||
|
|
||||||
|
const [oldCollection, createdCollection] = await UserCollection.findOrCreate({
|
||||||
|
where: { tmdbId: collectionData.id },
|
||||||
|
defaults: {
|
||||||
|
name: collectionData.name,
|
||||||
|
posterPath: collectionData.poster_path,
|
||||||
|
backdropPath: collectionData.backdrop_path,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let userCollection = oldCollection || createdCollection;
|
||||||
|
if (userCollection) {
|
||||||
|
collectionData = await fetchCollectionData(collectionData.id);
|
||||||
|
const partIds = collectionData.parts.map(part => part.id);
|
||||||
|
|
||||||
|
const entries = await Entry.findAll({
|
||||||
|
include: [{
|
||||||
|
model: EntryId,
|
||||||
|
as: 'entryId',
|
||||||
|
where: {
|
||||||
|
tmdb_id: {
|
||||||
|
[Sequelize.Op.in]: partIds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
|
||||||
|
await userCollection.setEntries(entries);
|
||||||
|
await userCollection.update({ description: collectionData.overview })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
res.status(201).json(entry);
|
res.status(201).json(entry);
|
||||||
});
|
});
|
||||||
@@ -246,118 +298,109 @@ entryRouter.get('/:id/refrech', async (req, res, next) => {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error)
|
next(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// // ? 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: []
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// ]
|
|
||||||
// });
|
|
||||||
|
|
||||||
// if (!oldEntry)
|
|
||||||
// return res.status(404).json({ message: `Entry id not found` });
|
|
||||||
|
|
||||||
// // ? 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);
|
|
||||||
|
|
||||||
// // ? 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' });
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// * GET - REQUEST ENTRY REFRECH
|
||||||
|
entryRouter.get('/:id/fetch-collection', async (req, res, next) => {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const oldEntry = await Entry.findByPk(id, {
|
||||||
|
include: [
|
||||||
|
{ model: EntryId, as: 'entryId' }
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
|
const entryData = await fetchEntryData(oldEntry.entryId.tmdb_id, oldEntry.is_tvseries);
|
||||||
|
if (entryData.belongs_to_collection) {
|
||||||
|
let collectionData = entryData.belongs_to_collection;
|
||||||
|
|
||||||
|
const [oldCollection, createdCollection] = await UserCollection.findOrCreate({
|
||||||
|
where: { tmdbId: collectionData.id },
|
||||||
|
defaults: {
|
||||||
|
name: collectionData.name,
|
||||||
|
posterPath: collectionData.poster_path,
|
||||||
|
backdropPath: collectionData.backdrop_path,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let userCollection = oldCollection || createdCollection;
|
||||||
|
if (userCollection) {
|
||||||
|
collectionData = await fetchCollectionData(collectionData.id);
|
||||||
|
const partIds = collectionData.parts.map(part => part.id);
|
||||||
|
|
||||||
|
const entries = await Entry.findAll({
|
||||||
|
include: [{
|
||||||
|
model: EntryId,
|
||||||
|
as: 'entryId',
|
||||||
|
where: {
|
||||||
|
tmdb_id: {
|
||||||
|
[Sequelize.Op.in]: partIds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
|
||||||
|
await userCollection.setEntries(entries);
|
||||||
|
await userCollection.update({ description: collectionData.overview })
|
||||||
|
}
|
||||||
|
res.status(201).json(userCollection);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
next(error)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// ! ENTRY MEDIA
|
||||||
|
// * POST - ADD NEW ENTRY MEDIA (poster/backdrop/logo)
|
||||||
const upload = entryPicturesUpload.fields([
|
const upload = entryPicturesUpload.fields([
|
||||||
{ name: 'poster', maxCount: 1 },
|
{ name: 'poster', maxCount: 1 },
|
||||||
{ name: 'backdrop', maxCount: 1 },
|
{ name: 'backdrop', maxCount: 1 },
|
||||||
{ name: 'logo', maxCount: 1 }
|
{ name: 'logo', maxCount: 1 }
|
||||||
]);
|
]);
|
||||||
entryRouter.post('/:id/media', upload, async (req, res) => {
|
entryRouter.post('/:id/media', upload, async (req, res) => {
|
||||||
const id = req.params.id;
|
const { id } = req.params;
|
||||||
if (req.files) {
|
|
||||||
|
if (!req.files) {
|
||||||
|
return res.status(400).json({ message: 'No files uploaded' });
|
||||||
|
}
|
||||||
|
|
||||||
const entry = await Entry.findByPk(id);
|
const entry = await Entry.findByPk(id);
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
return res.status(404).json({ message: 'Entry not found' });
|
return res.status(404).json({ message: 'Entry not found' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const posterPath = req.files.poster ? req.files.poster[0].filename : null;
|
// Function to handle media creation based on uploaded file type
|
||||||
if (posterPath) {
|
const fileType = ['poster', 'backdrop', 'logo'].find(type => req.files[type]);
|
||||||
|
if (fileType) {
|
||||||
|
const file = req.files[fileType][0];
|
||||||
const createdMedia = await Media.create({
|
const createdMedia = await Media.create({
|
||||||
entry_id: id,
|
entryId: id,
|
||||||
type: 'poster',
|
type: fileType,
|
||||||
path: posterPath,
|
path: file.filename,
|
||||||
is_local: true,
|
isLocal: true,
|
||||||
is_selected: false,
|
isSelected: false,
|
||||||
|
isCustom: true
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ? generating low resolution preview
|
||||||
|
const fileNameWithoutExt = file.filename.split('.')[0];
|
||||||
|
const originalPath = file.path;
|
||||||
|
|
||||||
|
let previewWidth = 342; // ? poster preview width
|
||||||
|
if (fileType == 'backdrop') previewWidth = 780;
|
||||||
|
if (fileType == 'logo') previewWidth = 300;
|
||||||
|
|
||||||
|
const previewPath = path.join(file.destination, `${fileNameWithoutExt}_preview.jpg`);
|
||||||
|
generatePreview(originalPath, previewPath, previewWidth);
|
||||||
|
|
||||||
return res.status(200).json(createdMedia);
|
return res.status(200).json(createdMedia);
|
||||||
}
|
}
|
||||||
|
|
||||||
const backdropPath = req.files.backdrop ? req.files.backdrop[0].filename : null;
|
return res.status(400).json({ message: 'Invalid media type' });
|
||||||
if (backdropPath) {
|
|
||||||
const createdMedia = await Media.create({
|
|
||||||
entry_id: id,
|
|
||||||
type: 'backdrop',
|
|
||||||
path: backdropPath,
|
|
||||||
is_local: true,
|
|
||||||
is_selected: false,
|
|
||||||
});
|
|
||||||
return res.status(200).json(createdMedia);
|
|
||||||
}
|
|
||||||
|
|
||||||
const logoPath = req.files.logo ? req.files.logo[0].filename : null;
|
|
||||||
if (logoPath) {
|
|
||||||
const createdMedia = await Media.create({
|
|
||||||
entry_id: id,
|
|
||||||
type: 'logo',
|
|
||||||
path: logoPath,
|
|
||||||
is_local: true,
|
|
||||||
is_selected: false,
|
|
||||||
});
|
|
||||||
return res.status(200).json(createdMedia);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// * PUT - SET ENTRY MEDIA AS SELECTED
|
||||||
entryRouter.put('/:entryId/media/:mediaId', upload, async (req, res) => {
|
entryRouter.put('/:entryId/media/:mediaId', upload, async (req, res) => {
|
||||||
const entryId = req.params.entryId;
|
const entryId = req.params.entryId;
|
||||||
const mediaId = req.params.mediaId;
|
const mediaId = req.params.mediaId;
|
||||||
@@ -373,27 +416,42 @@ entryRouter.put('/:entryId/media/:mediaId', upload, async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await Media.update(
|
await Media.update(
|
||||||
{ is_selected: false },
|
{ isSelected: false },
|
||||||
{
|
{
|
||||||
where: {
|
where: {
|
||||||
entry_id: entryId,
|
entryId: entryId,
|
||||||
type: media.type
|
type: media.type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const updatedMedia = await media.update({ is_selected: true });
|
const updatedMedia = await media.update({ isSelected: true });
|
||||||
|
|
||||||
return res.status(200).json(updatedMedia);
|
return res.status(200).json(updatedMedia);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// * DELETE - DELETE AND ENTRY MEDIA
|
||||||
entryRouter.delete('/:entryId/media/:mediaId', async (req, res) => {
|
entryRouter.delete('/:entryId/media/:mediaId', async (req, res) => {
|
||||||
const mediaId = req.params.mediaId;
|
const mediaId = req.params.mediaId;
|
||||||
|
|
||||||
// const media = await Media.findByPk(mediaId);
|
// ? just before deleting we need to check if the deleted media was the one selected
|
||||||
// if (!media) {
|
// ? if so select a new one if there is, then delete
|
||||||
|
const media = await Media.findByPk(mediaId);
|
||||||
|
if (media && media.isSelected) {
|
||||||
|
let otherMedia = await Media.findOne({
|
||||||
|
where: {
|
||||||
|
entryId: media.entryId,
|
||||||
|
type: media.type,
|
||||||
|
isSelected: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// ? if found select the first one
|
||||||
|
if (otherMedia) {
|
||||||
|
otherMedia.update({ isSelected: true });
|
||||||
|
}
|
||||||
// return res.status(404).json({ message: 'Media not found' });
|
// return res.status(404).json({ message: 'Media not found' });
|
||||||
// }
|
}
|
||||||
|
|
||||||
await Media.destroy({ where: { id: mediaId } })
|
await Media.destroy({ where: { id: mediaId } })
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user