changes + fixes to collections related component + add filter by collection type
This commit is contained in:
1 parent
eba9721a3c
commit
776506b232
14 files changed
+219
-129
No files matched your search
@@ -4,12 +4,18 @@ const sequelize = require('../db/database');
|
||||
const UserCollection = sequelize.define("userCollection",
|
||||
{
|
||||
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
|
||||
tmdbId: { type: DataTypes.STRING(30), field: 'tmdb_id' },
|
||||
imdbId: { type: DataTypes.STRING(30), field: 'imdb_id' },
|
||||
tvdbId: { type: DataTypes.STRING(30), field: 'tvdb_id' },
|
||||
|
||||
name: { type: DataTypes.STRING(150) },
|
||||
description: { type: DataTypes.STRING(500) },
|
||||
user_rating: { type: DataTypes.FLOAT },
|
||||
description: { type: DataTypes.STRING(3000) },
|
||||
isUserCollection: { type: DataTypes.BOOLEAN, defaultValue: false, field: 'is_user_collection' },
|
||||
userRating: { type: DataTypes.FLOAT, field: 'user_rating' },
|
||||
|
||||
poster_path: { type: DataTypes.STRING(200) },
|
||||
posterPath: { type: DataTypes.STRING(200), field: 'poster_path' },
|
||||
backdropPath: { type: DataTypes.STRING(200), field: 'backdrop_path' },
|
||||
isLocal: { type: DataTypes.BOOLEAN, defaultValue: false, field: 'is_local' }
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -13,28 +13,21 @@ const router = express.Router();
|
||||
const multer = require('multer');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
const path = require('path');
|
||||
const { getMediaFolder } = require('../services/mediaService');
|
||||
const { getMediaFolder, generatePreview } = require('../services/mediaService');
|
||||
|
||||
// ! /user-collections
|
||||
// * POST - CREATE UserCollection
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const { name, description, user_rating, poster_path } = req.body;
|
||||
const newUserCollection = await UserCollection.create({ name, description, user_rating, poster_path });
|
||||
res.status(201).json(newUserCollection);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// * GET - ALL UserCollections
|
||||
router.get('/', async (req, res) => {
|
||||
let userCollections; // Declare outside to broaden scope, albeit unnecessary in your given context
|
||||
try {
|
||||
userCollections = await UserCollection.findAll({
|
||||
include: [
|
||||
{ model: UserCollectionEntry, as: 'userCollectionEntries' },
|
||||
]
|
||||
{
|
||||
model: Entry, as: 'entries',
|
||||
include: { model: Media, as: 'medias' }
|
||||
},
|
||||
],
|
||||
order: [['name', 'ASC']]
|
||||
});
|
||||
res.status(200).json(userCollections);
|
||||
} catch (error) {
|
||||
@@ -82,6 +75,18 @@ router.get('/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// * POST - CREATE UserCollection
|
||||
router.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 });
|
||||
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'))
|
||||
@@ -94,23 +99,33 @@ const storage = multer.diskStorage({
|
||||
});
|
||||
|
||||
const upload = multer({ storage: storage });
|
||||
|
||||
// * PUT - UPDATE A UserCollection
|
||||
router.put('/:id', upload.single('poster'), async (req, res) => {
|
||||
try {
|
||||
const userCollectionId = req.params.id;
|
||||
const { name, description, user_rating, is_poster_link } = req.body;
|
||||
const { name, description, userRating, is_poster_link } = req.body;
|
||||
|
||||
let poster_path = req.file ? req.file.filename : undefined;
|
||||
let posterPath = req.file ? req.file.filename : undefined;
|
||||
|
||||
const updateData = {
|
||||
name,
|
||||
description,
|
||||
user_rating: parseInt(user_rating, 10)
|
||||
userRating: parseInt(userRating, 10),
|
||||
isLocal: false,
|
||||
|
||||
};
|
||||
|
||||
if (poster_path) {
|
||||
updateData.poster_path = poster_path;
|
||||
if (posterPath) {
|
||||
updateData.posterPath = posterPath;
|
||||
updateData.isLocal = true;
|
||||
|
||||
// ? 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);
|
||||
}
|
||||
|
||||
const updated = await UserCollection.update(updateData, {
|
||||
@@ -122,13 +137,16 @@ router.put('/:id', upload.single('poster'), async (req, res) => {
|
||||
}
|
||||
|
||||
if (updated) {
|
||||
const updatedUserCollection = await UserCollection.findByPk(req.params.id, {
|
||||
const updatedUserCollection = await UserCollection.findByPk(userCollectionId, {
|
||||
include: [{
|
||||
model: Entry,
|
||||
as: 'entries',
|
||||
include: [
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{ model: EntryRating, as: 'entryRating' }
|
||||
{ model: EntryRating, as: 'entryRating' },
|
||||
{ model: Season, as: 'seasons' },
|
||||
{ model: Media, as: 'medias' },
|
||||
{ model: Genre, as: 'genres' },
|
||||
],
|
||||
through: {
|
||||
model: UserCollectionEntry,
|
||||
@@ -162,7 +180,6 @@ router.delete('/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// * POST - ADD ENTRY TO A UserCollection
|
||||
router.post('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user