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,7 +1,7 @@
|
||||
const express = require('express');
|
||||
const EntrySeen = require('../models/EntrySeen');
|
||||
const Entry = require('../models/Entry');
|
||||
const Media = require('../models/Media');
|
||||
const Collection = require('../models/Collection');
|
||||
|
||||
const customMediaRouter = express.Router();
|
||||
|
||||
@@ -11,12 +11,21 @@ customMediaRouter.get('/', async (req, res) => {
|
||||
|
||||
const allCustomMedia = await Media.findAll({
|
||||
where: {
|
||||
isCustom: true
|
||||
isCustom: true,
|
||||
},
|
||||
include: [
|
||||
{ model: Entry, as: 'entry' }
|
||||
{
|
||||
model: Entry,
|
||||
as: 'entry',
|
||||
required: false // This ensures a LEFT OUTER JOIN
|
||||
},
|
||||
{
|
||||
model: Collection,
|
||||
as: 'collection',
|
||||
required: false // This ensures a LEFT OUTER JOIN
|
||||
}
|
||||
]
|
||||
})
|
||||
});
|
||||
|
||||
res.status(200).json(allCustomMedia);
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@ const { fetchEntryData, fetchCollectionData } = require('../services/tmdbService
|
||||
const { saveEntry, refrechEntry, SyncSeriesData } = require('../services/models-services/entryService');
|
||||
const { bulkImportMovies, bulkImportTmdb, bulkImportEpisodes } = require('../services/importerService');
|
||||
const { entryPicturesUpload } = require('../services/mutlerStorageLocations');
|
||||
const { generatePreview } = require('../services/mediaService');
|
||||
const { generatePreview, doGeneratePreview } = require('../services/mediaService');
|
||||
|
||||
const { Sequelize } = require('sequelize');
|
||||
const Entry = require('../models/Entry');
|
||||
@@ -18,6 +18,8 @@ const Season = require('../models/Season');
|
||||
const Media = require('../models/Media');
|
||||
const Episode = require('../models/Episode');
|
||||
const UserCollection = require('../models/Collection');
|
||||
const { upsertOfficialCollection } = require('../services/models-services/collectionService');
|
||||
const { logToFile } = require('../services/logService');
|
||||
|
||||
// ! /entries
|
||||
// * GET - ALL ENTRIES
|
||||
@@ -77,7 +79,12 @@ entryRouter.get('/:id', async (req, res) => {
|
||||
{ model: EntrySeen, as: 'entrySeen' },
|
||||
{ model: Season, as: 'seasons' },
|
||||
{ model: Media, as: 'medias' },
|
||||
{ model: UserCollection, as: 'collections' },
|
||||
{
|
||||
model: UserCollection, as: 'collections',
|
||||
include: [
|
||||
{ model: Media, as: 'medias' }
|
||||
]
|
||||
},
|
||||
{
|
||||
model: Genre,
|
||||
as: 'genres',
|
||||
@@ -155,37 +162,7 @@ entryRouter.post('/', async (req, res) => {
|
||||
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 })
|
||||
}
|
||||
upsertOfficialCollection(entryData.belongs_to_collection.id);
|
||||
}
|
||||
|
||||
res.status(201).json(entry);
|
||||
@@ -285,7 +262,7 @@ entryRouter.put('/:id', async (req, res) => {
|
||||
});
|
||||
|
||||
// * GET - REQUEST ENTRY REFRECH
|
||||
entryRouter.get('/:id/refrech', async (req, res, next) => {
|
||||
entryRouter.get('/:id/refresh', async (req, res, next) => {
|
||||
const { id } = req.params;
|
||||
|
||||
try {
|
||||
@@ -382,15 +359,25 @@ entryRouter.post('/:id/media', upload, async (req, res) => {
|
||||
});
|
||||
|
||||
// ? generating low resolution preview
|
||||
const fileNameWithoutExt = file.filename.split('.')[0];
|
||||
const originalPath = file.path;
|
||||
// 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;
|
||||
// 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);
|
||||
// const previewPath = path.join(file.destination, `${fileNameWithoutExt}_preview.jpg`);
|
||||
// generatePreview(originalPath, previewPath, previewWidth);
|
||||
doGeneratePreview('entries', fileType + 's', file.filename).then((result) => {
|
||||
const stream = fs.createReadStream(filePath);
|
||||
stream.pipe(res);
|
||||
|
||||
const msg = `Media Route | Generated Entry Preview`
|
||||
logToFile(msg);
|
||||
}).catch((err) => {
|
||||
const msg = `Media Route | Error Generating Entry Preview`
|
||||
logToFile(msg);
|
||||
});
|
||||
|
||||
return res.status(200).json(createdMedia);
|
||||
}
|
||||
@@ -417,7 +404,7 @@ entryRouter.put('/:entryId/media/:mediaId', upload, async (req, res) => {
|
||||
{ isSelected: false },
|
||||
{
|
||||
where: {
|
||||
entryId: entryId,
|
||||
mediaableId: entryId,
|
||||
type: media.type
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@ const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
const { getSettings } = require('../services/settingsService');
|
||||
const { generatePreview } = require('../services/mediaService');
|
||||
const { generatePreview, doGeneratePreview } = require('../services/mediaService');
|
||||
const { logToFile } = require('../services/logService');
|
||||
|
||||
const mediaRouter = express.Router();
|
||||
|
||||
@@ -43,21 +44,22 @@ mediaRouter.get('/:category/:type/:size/:fileName', (req, res, next) => {
|
||||
// ? and generate a preview file, but still return 202 http code since the file
|
||||
// ? is still generating
|
||||
if (err && size == 'preview') {
|
||||
const originalPath = path.join(mediaPath, fileName);
|
||||
const previewFileName = fileName.split('.')[0] + '_preview.jpg'
|
||||
const previewPath = path.join(mediaPath, previewFileName);
|
||||
// const originalPath = path.join(mediaPath, fileName);
|
||||
// const previewFileName = fileName.split('.')[0] + '_preview.jpg'
|
||||
// const previewPath = path.join(mediaPath, previewFileName);
|
||||
|
||||
let previewWidth = 342;
|
||||
if (type == 'backdrops') previewWidth = 780;
|
||||
if (type == 'logos') previewWidth = 300;
|
||||
// let previewWidth = 342;
|
||||
// if (type == 'backdrops') previewWidth = 780;
|
||||
// if (type == 'logos') previewWidth = 300;
|
||||
|
||||
generatePreview(originalPath, previewPath, previewWidth).then((result) => {
|
||||
console.log(`generated a preview: `, previewFileName);
|
||||
doGeneratePreview(category, type, fileName).then((result) => {
|
||||
const stream = fs.createReadStream(filePath);
|
||||
stream.pipe(res);
|
||||
logToFile(`Media Route | Generated Preview`);
|
||||
}).catch((err) => {
|
||||
|
||||
});;
|
||||
logToFile(`Media Route | Error Generating Preview`);
|
||||
logToFile(err);
|
||||
});
|
||||
// const stream = fs.createReadStream(filePath);
|
||||
// stream.pipe(res);
|
||||
// return res.status(202).send('Preview is still generating. Please try again later.');
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const express = require('express');
|
||||
const userCollectionRoutes = require('./user-collection.routes');
|
||||
const collectionRouter = require('./user-collection.routes');
|
||||
const entryIdsRouter = require('./entry-ids.routes');
|
||||
const entrySeenRouter = require('./entry-seen.routes');
|
||||
const entryRouter = require('./entry.routes');
|
||||
@@ -16,7 +16,7 @@ const episodeRouter = require('./episode.routes');
|
||||
const router = express.Router();
|
||||
router.use('/media', mediaRouter);
|
||||
router.use('/entries', entryRouter);
|
||||
router.use('/user-collections', userCollectionRoutes);
|
||||
router.use('/collections', collectionRouter);
|
||||
router.use('/entry-seen', entrySeenRouter);
|
||||
router.use('/entry-ids', entryIdsRouter);
|
||||
router.use('/entry-ratings', entryRatingsRouter);
|
||||
|
||||
@@ -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