implemented UserCollection edit modal + custom collection poster + media access route
This commit is contained in:
1 parent
0ff3c49ca6
commit
e8c4fba943
17 files changed
+499
-73
No files matched your search
@@ -6,6 +6,8 @@ const EntryId = require('../models/EntryId');
|
||||
const EntryRating = require('../models/EntryRating');
|
||||
const { Sequelize } = require('sequelize');
|
||||
const router = express.Router();
|
||||
const multer = require('multer');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
|
||||
// ! /user-collections
|
||||
// * POST - CREATE UserCollection
|
||||
@@ -65,19 +67,68 @@ router.get('/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
cb(null, 'uploads/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', async (req, res) => {
|
||||
router.put('/:id', upload.single('poster'), async (req, res) => {
|
||||
try {
|
||||
const { name, description, user_rating, poster_path } = req.body;
|
||||
const [updated] = await UserCollection.update({ name, description, user_rating, poster_path }, { where: { id: req.params.id } });
|
||||
const userCollectionId = req.params.id;
|
||||
const { name, description, user_rating, is_poster_link } = req.body;
|
||||
|
||||
let poster_path = req.file ? req.file.filename : undefined;
|
||||
|
||||
const updateData = {
|
||||
name,
|
||||
description,
|
||||
user_rating: parseInt(user_rating, 10)
|
||||
};
|
||||
|
||||
if (poster_path) {
|
||||
updateData.poster_path = poster_path;
|
||||
}
|
||||
|
||||
const updated = await UserCollection.update(updateData, {
|
||||
where: { id: userCollectionId }
|
||||
});
|
||||
|
||||
if (!updated[0]) {
|
||||
return res.status(404).json({ message: 'UserCollection not found' });
|
||||
}
|
||||
|
||||
if (updated) {
|
||||
const updatedUserCollection = await UserCollection.findByPk(req.params.id);
|
||||
const updatedUserCollection = await UserCollection.findByPk(req.params.id, {
|
||||
include: [{
|
||||
model: Entry,
|
||||
as: 'entries',
|
||||
include: [
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{ model: EntryRating, as: 'entryRating' }
|
||||
],
|
||||
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
|
||||
});
|
||||
|
||||
res.status(200).json(updatedUserCollection);
|
||||
} else {
|
||||
res.status(404).json({ message: 'UserCollection not found' });
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`UserCollection update (put) error: `, error)
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user