Major rework of media handelling, and collections
This commit is contained in:
1 parent
b42e055691
commit
f7cb2bed53
37 files changed
+1120
-411
No files matched your search
@@ -1,5 +1,10 @@
|
||||
const express = require('express');
|
||||
const UserCollection = require('../models/UserCollection');
|
||||
const collectionRouter = express.Router();
|
||||
|
||||
const { doGeneratePreview } = require('../services/mediaService');
|
||||
|
||||
const { Sequelize } = require('sequelize');
|
||||
const Collection = require('../models/Collection');
|
||||
const UserCollectionEntry = require('../models/UserCollectionEntry');
|
||||
const Entry = require('../models/Entry');
|
||||
const EntryId = require('../models/EntryId');
|
||||
@@ -8,58 +13,61 @@ const Season = require('../models/Season');
|
||||
const Genre = require('../models/Genre');
|
||||
const Media = require('../models/Media');
|
||||
|
||||
const { Sequelize } = require('sequelize');
|
||||
const router = express.Router();
|
||||
const multer = require('multer');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
const path = require('path');
|
||||
const { getMediaFolder, generatePreview } = require('../services/mediaService');
|
||||
const { getCollectionParts, upsertOfficialCollection } = require('../services/models-services/collectionService');
|
||||
const { collectionPicturesUpload } = require('../services/mutlerStorageLocations');
|
||||
const { logToFile } = require('../services/logService');
|
||||
|
||||
// ! /user-collections
|
||||
// * GET - ALL UserCollections
|
||||
router.get('/', async (req, res) => {
|
||||
let userCollections; // Declare outside to broaden scope, albeit unnecessary in your given context
|
||||
// ! /collections
|
||||
// * GET - ALL COLLECTIONS
|
||||
collectionRouter.get('/', async (req, res) => {
|
||||
let collections; // Declare outside to broaden scope, albeit unnecessary in your given context
|
||||
try {
|
||||
userCollections = await UserCollection.findAll({
|
||||
collections = await Collection.findAll({
|
||||
include: [
|
||||
{
|
||||
model: Entry, as: 'entries',
|
||||
include: { model: Media, as: 'medias' }
|
||||
},
|
||||
{ model: Media, as: 'medias' }
|
||||
],
|
||||
order: [['name', 'ASC']]
|
||||
});
|
||||
res.status(200).json(userCollections);
|
||||
res.status(200).json(collections);
|
||||
} catch (error) {
|
||||
console.error("Error fetching UserCollections:", error); // More detailed error logging
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// * GET - userCollection BY ID
|
||||
router.get('/:id', async (req, res) => {
|
||||
// * GET - COLLECTION BY ID
|
||||
collectionRouter.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const userCollection = await UserCollection.findByPk(req.params.id, {
|
||||
include: [{
|
||||
model: Entry,
|
||||
as: 'entries',
|
||||
include: [
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{ model: EntryRating, as: 'entryRating' },
|
||||
{ model: Season, as: 'seasons' },
|
||||
{ model: Media, as: 'medias' },
|
||||
{ model: Genre, as: 'genres' },
|
||||
],
|
||||
through: {
|
||||
model: UserCollectionEntry,
|
||||
attributes: ['order'], // attributes (to include) from the join model
|
||||
const collectionId = req.params.id;
|
||||
|
||||
const collection = await Collection.findByPk(collectionId, {
|
||||
include: [
|
||||
{
|
||||
model: Entry,
|
||||
as: 'entries',
|
||||
include: [
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{ model: EntryRating, as: 'entryRating' },
|
||||
{ model: Season, as: 'seasons' },
|
||||
{ model: Media, as: 'medias' },
|
||||
{ model: Genre, as: 'genres' },
|
||||
],
|
||||
through: {
|
||||
model: UserCollectionEntry,
|
||||
attributes: ['order'], // attributes (to include) from the join model
|
||||
},
|
||||
},
|
||||
}],
|
||||
{ model: Media, as: 'medias' },
|
||||
],
|
||||
order: [[Sequelize.literal("`entries->UserCollectionEntry`.`order`"), 'ASC']], // sort by 'order' in ascending order
|
||||
});
|
||||
|
||||
if (userCollection) {
|
||||
const collectionData = userCollection.get({ plain: true });
|
||||
if (collection) {
|
||||
const collectionData = collection.get({ plain: true });
|
||||
for (let i = 0; i < collectionData.entries.length; i++) {
|
||||
const genres = collectionData.entries[i].genres.map(genre => genre.name);
|
||||
collectionData.entries[i].genres = genres
|
||||
@@ -75,70 +83,173 @@ router.get('/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// * POST - CREATE UserCollection
|
||||
router.post('/', async (req, res) => {
|
||||
// * POST - CREATE COLLECTION
|
||||
collectionRouter.post('/', async (req, res) => {
|
||||
try {
|
||||
const { name, description, userRating, posterPath } = req.body;
|
||||
const isUserCollection = true;
|
||||
const newUserCollection = await UserCollection.create({ name, description, userRating, posterPath, isUserCollection });
|
||||
const newUserCollection = await Collection.create({ name, description, userRating, posterPath, isUserCollection });
|
||||
res.status(201).json(newUserCollection);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
cb(null, path.join(getMediaFolder(), 'images', 'collections', 'posters'))
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
const uniqueFilename = uuidv4();
|
||||
const extension = file.originalname.split('.').pop();
|
||||
cb(null, `${uniqueFilename}.${extension}`);
|
||||
}
|
||||
});
|
||||
|
||||
const upload = multer({ storage: storage });
|
||||
// * PUT - UPDATE A UserCollection
|
||||
router.put('/:id', upload.single('poster'), async (req, res) => {
|
||||
// * PUT - UPDATE COLLECTION
|
||||
const upload = collectionPicturesUpload.fields([
|
||||
{ name: 'poster', maxCount: 1 },
|
||||
{ name: 'backdrop', maxCount: 1 }
|
||||
]);
|
||||
collectionRouter.put('/:id', upload, async (req, res) => {
|
||||
try {
|
||||
const userCollectionId = req.params.id;
|
||||
const { name, description, userRating, is_poster_link } = req.body;
|
||||
const collectionId = req.params.id;
|
||||
const { name, description, userRating } = req.body;
|
||||
|
||||
let posterPath = req.file ? req.file.filename : undefined;
|
||||
const posterFile = req.files.poster ? req.files.poster[0] : undefined;
|
||||
const backdropFile = req.files.backdrop ? req.files.backdrop[0] : undefined;
|
||||
|
||||
let posterPath = posterFile ? posterFile.filename : undefined;
|
||||
let backdropPath = backdropFile ? backdropFile.filename : undefined;
|
||||
|
||||
const updateData = {
|
||||
name,
|
||||
description,
|
||||
userRating: parseInt(userRating, 10),
|
||||
isLocal: false,
|
||||
|
||||
};
|
||||
|
||||
if (posterPath) {
|
||||
updateData.posterPath = posterPath;
|
||||
updateData.isLocal = true;
|
||||
// ? generating preview
|
||||
doGeneratePreview("collections", 'posters', posterPath).then((result) => {
|
||||
const msg = `Collection Service | Generated Collection Preview`;
|
||||
logToFile(msg);
|
||||
}).catch((err) => {
|
||||
const msg = `Collection Service | Error Generating Collection Preview`;
|
||||
logToFile(msg);
|
||||
logToFile(err);
|
||||
});
|
||||
|
||||
// ? generating preview
|
||||
const fileNameWithoutExt = req.file.filename.split('.')[0];
|
||||
const originalPath = req.file.path;
|
||||
|
||||
let previewWidth = 342; // ? poster preview width
|
||||
const previewPath = path.join(req.file.destination, `${fileNameWithoutExt}_preview.jpg`);
|
||||
generatePreview(originalPath, previewPath, previewWidth);
|
||||
await Media.create({
|
||||
mediaable: 'collection',
|
||||
mediaableId: collectionId,
|
||||
type: 'poster',
|
||||
path: posterPath,
|
||||
isLocal: true,
|
||||
isSelected: true,
|
||||
isCustom: true
|
||||
})
|
||||
}
|
||||
|
||||
const updated = await UserCollection.update(updateData, {
|
||||
where: { id: userCollectionId }
|
||||
if (backdropPath) {
|
||||
// ? generating preview
|
||||
doGeneratePreview("collections", 'backdrops', backdropPath).then((result) => {
|
||||
const msg = `Collection Routes | Generated Collection Preview`;
|
||||
logToFile(msg);
|
||||
}).catch((err) => {
|
||||
const msg = `Collection Routes | Error Generating Collection Preview`;
|
||||
logToFile(msg);
|
||||
logToFile(err);
|
||||
});
|
||||
|
||||
// ? inserting data
|
||||
await Media.create({
|
||||
mediaable: 'collection',
|
||||
mediaableId: collectionId,
|
||||
type: 'backdrop',
|
||||
path: backdropPath,
|
||||
isLocal: true,
|
||||
isSelected: true,
|
||||
isCustom: true
|
||||
})
|
||||
}
|
||||
|
||||
const oldCollection = await Collection.findByPk(collectionId, {
|
||||
include: [{
|
||||
model: Entry,
|
||||
as: 'entries',
|
||||
include: [
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{ model: EntryRating, as: 'entryRating' },
|
||||
{ model: Season, as: 'seasons' },
|
||||
{ model: Media, as: 'medias' },
|
||||
{ model: Genre, as: 'genres' },
|
||||
],
|
||||
through: {
|
||||
model: UserCollectionEntry,
|
||||
attributes: ['order'], // attributes (to include) from the join model
|
||||
},
|
||||
},
|
||||
{ model: Media, as: 'medias' },
|
||||
],
|
||||
order: [[Sequelize.literal("`entries->UserCollectionEntry`.`order`"), 'ASC']], // sort by 'order' in ascending order
|
||||
});
|
||||
|
||||
if (!updated[0]) {
|
||||
return res.status(404).json({ message: 'UserCollection not found' });
|
||||
}
|
||||
if (!oldCollection)
|
||||
res.status(404).json({ message: 'Collection not found' });
|
||||
|
||||
if (updated) {
|
||||
const updatedUserCollection = await UserCollection.findByPk(userCollectionId, {
|
||||
include: [{
|
||||
await oldCollection.update(updateData);
|
||||
|
||||
res.status(200).json(oldCollection);
|
||||
} catch (error) {
|
||||
console.log(`UserCollection update (put) error: `, error)
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// * PUT - REFRESH COLLECTION
|
||||
collectionRouter.put('/:id/refresh', async (req, res) => {
|
||||
try {
|
||||
const collectionId = req.params.id;
|
||||
const oldCollection = await Collection.findByPk(collectionId);
|
||||
|
||||
const collection = await upsertOfficialCollection(oldCollection.tmdbId);
|
||||
|
||||
// const collection = await Collection.findByPk(collectionId, {
|
||||
// include: [{
|
||||
// model: Entry,
|
||||
// as: 'entries',
|
||||
// include: [
|
||||
// { model: EntryId, as: 'entryId' },
|
||||
// { model: EntryRating, as: 'entryRating' },
|
||||
// { model: Season, as: 'seasons' },
|
||||
// { model: Media, as: 'medias' },
|
||||
// { model: Genre, as: 'genres' },
|
||||
// ],
|
||||
// through: {
|
||||
// model: UserCollectionEntry,
|
||||
// attributes: ['order'], // attributes (to include) from the join model
|
||||
// },
|
||||
// }],
|
||||
// order: [[Sequelize.literal("`entries->UserCollectionEntry`.`order`"), 'ASC']], // sort by 'order' in ascending order
|
||||
// });
|
||||
|
||||
if (collection) {
|
||||
const collectionData = collection.get({ plain: true });
|
||||
for (let i = 0; i < collectionData.entries.length; i++) {
|
||||
const genres = collectionData.entries[i].genres.map(genre => genre.name);
|
||||
collectionData.entries[i].genres = genres
|
||||
}
|
||||
|
||||
res.status(200).json(collectionData);
|
||||
} else {
|
||||
res.status(404).json({ message: 'Collection not found' });
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`UserCollection update (put) error: `, error)
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// * PUT - GET COLLECTION PARTS
|
||||
collectionRouter.put('/:id/parts', async (req, res) => {
|
||||
try {
|
||||
const collectionId = req.params.id;
|
||||
|
||||
await getCollectionParts(collectionId);
|
||||
|
||||
const collection = await Collection.findByPk(collectionId, {
|
||||
include: [
|
||||
{
|
||||
model: Entry,
|
||||
as: 'entries',
|
||||
include: [
|
||||
@@ -152,13 +263,22 @@ router.put('/:id', upload.single('poster'), async (req, res) => {
|
||||
model: UserCollectionEntry,
|
||||
attributes: ['order'], // attributes (to include) from the join model
|
||||
},
|
||||
}],
|
||||
order: [[Sequelize.literal("`entries->UserCollectionEntry`.`order`"), 'ASC']], // sort by 'order' in ascending order
|
||||
});
|
||||
},
|
||||
{ model: Media, as: 'medias' },
|
||||
],
|
||||
order: [[Sequelize.literal("`entries->UserCollectionEntry`.`order`"), 'ASC']], // sort by 'order' in ascending order
|
||||
});
|
||||
|
||||
res.status(200).json(updatedUserCollection);
|
||||
if (collection) {
|
||||
const collectionData = collection.get({ plain: true });
|
||||
for (let i = 0; i < collectionData.entries.length; i++) {
|
||||
const genres = collectionData.entries[i].genres.map(genre => genre.name);
|
||||
collectionData.entries[i].genres = genres
|
||||
}
|
||||
|
||||
res.status(200).json(collectionData);
|
||||
} else {
|
||||
res.status(404).json({ message: 'UserCollection not found' });
|
||||
res.status(404).json({ message: 'Collection not found' });
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`UserCollection update (put) error: `, error)
|
||||
@@ -166,10 +286,10 @@ router.put('/:id', upload.single('poster'), async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// * DELETE - A UserCollection
|
||||
router.delete('/:id', async (req, res) => {
|
||||
// * DELETE - DELETE COLLECTION
|
||||
collectionRouter.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const deleted = await UserCollection.destroy({ where: { id: req.params.id } });
|
||||
const deleted = await Collection.destroy({ where: { id: req.params.id } });
|
||||
if (deleted) {
|
||||
res.status(204).send();
|
||||
} else {
|
||||
@@ -180,8 +300,8 @@ router.delete('/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// * POST - ADD ENTRY TO A UserCollection
|
||||
router.post('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
// * POST - ADD ENTRY TO A COLLECTION
|
||||
collectionRouter.post('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
try {
|
||||
const { userCollectionId, entryId } = req.params;
|
||||
|
||||
@@ -191,7 +311,7 @@ router.post('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
});
|
||||
|
||||
// ? set a default poster if none exists
|
||||
const userCollection = await UserCollection.findByPk(userCollectionId);
|
||||
const userCollection = await Collection.findByPk(userCollectionId);
|
||||
if (!userCollection.poster_path || userCollection.poster_path.trim() === '') {
|
||||
const entry = await Entry.findByPk(entryId);
|
||||
userCollection.poster_path = entry.poster_path;
|
||||
@@ -205,8 +325,8 @@ router.post('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// * PATCH - UPDATE ENTRY ORDER IN UserCollection
|
||||
router.patch('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
// * PATCH - UPDATE ENTRY ORDER IN COLLECTION
|
||||
collectionRouter.patch('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
try {
|
||||
const { userCollectionId, entryId } = req.params;
|
||||
const { newOrder } = req.body;
|
||||
@@ -233,8 +353,8 @@ router.patch('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// * DELETE - REMOVE AN ENTRY FROM A UserCollection
|
||||
router.delete('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
// * DELETE - REMOVE AN ENTRY FROM A COLLECTION
|
||||
collectionRouter.delete('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
try {
|
||||
const { userCollectionId, entryId } = req.params;
|
||||
const deleted = await UserCollectionEntry.findOne({
|
||||
@@ -258,13 +378,13 @@ router.delete('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
});
|
||||
|
||||
// * POST - CREATE UserCollection WITH ENTRY
|
||||
router.post('/lists-with-entry', async (req, res) => {
|
||||
collectionRouter.post('/lists-with-entry', async (req, res) => {
|
||||
const { listDetails, entryId, entryOrder } = req.body; // Destructure needed properties from the request body
|
||||
|
||||
try {
|
||||
const result = await sequelize.transaction(async (t) => {
|
||||
// Step 1: Create the userCollection
|
||||
const newUserCollection = await UserCollection.create(listDetails, { transaction: t });
|
||||
const newUserCollection = await Collection.create(listDetails, { transaction: t });
|
||||
|
||||
// Step 2: Add an entry to the newly created userCollection
|
||||
const newUserCollectionEntry = await UserCollectionEntry.create({
|
||||
@@ -284,4 +404,4 @@ router.post('/lists-with-entry', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
module.exports = collectionRouter;
|
||||
Reference in new issue
Block a user