implemented hooks for user-collections & user-collection-entry models to update updatedAt and order fields

This commit is contained in:
X4Lo committed 2024-04-30 22:21:17 +01:00
1 parent 36866dabe6
commit cea1b207a3
12 files changed
+170 -61

No files matched your search

+17 -18
View File
@@ -4,6 +4,7 @@ const UserCollectionEntry = require('../models/UserCollectionEntry');
const Entry = require('../models/Entry');
const EntryId = require('../models/EntryId');
const EntryRating = require('../models/EntryRating');
const { Sequelize } = require('sequelize');
const router = express.Router();
// ! /user-collections
@@ -40,30 +41,26 @@ router.get('/:id', async (req, res) => {
const userCollection = await UserCollection.findByPk(req.params.id, {
include: [{
model: Entry,
as: 'entries', // Make sure this matches your alias in the association, if any
as: 'entries',
include: [
{ model: EntryId, as: 'entryId' },
{ model: EntryRating, as: 'entryRating' }
],
through: {
attributes: [], // Exclude attributes from the join model, if you don't need them
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 (userCollection) {
// Optionally, sort the entries here if needed, using JavaScript's sort()
// This is a workaround for the direct ordering limitation via Sequelize in many-to-many relationships
for (let i = 0; i < userCollection.entries.length; i++) {
userCollection.entries[i] = await Entry.findByPk(userCollection.entries[i].id,
{
include: [
{ model: EntryId, as: 'entryId' },
{ model: EntryRating, as: 'entryRating' }
]
})
}
res.status(200).json(userCollection);
} else {
res.status(404).json({ message: 'UserCollection not found' });
}
} catch (error) {
console.log(`Error getting collection info: `, error)
res.status(500).json({ error: error.message });
}
});
@@ -104,12 +101,10 @@ router.delete('/:id', async (req, res) => {
router.post('/:userCollectionId/entries/:entryId', async (req, res) => {
try {
const { userCollectionId, entryId } = req.params;
const { order } = req.body;
const newUserCollectionEntry = await UserCollectionEntry.create({
user_collection_id: userCollectionId,
entry_id: entryId,
order: order || 0
entry_id: entryId
});
// ? set a default poster if none exists
@@ -122,6 +117,7 @@ router.post('/:userCollectionId/entries/:entryId', async (req, res) => {
res.status(201).json(newUserCollectionEntry);
} catch (error) {
console.log(error)
res.status(400).json({ error: error.message });
}
});
@@ -157,12 +153,15 @@ router.patch('/:userCollectionId/entries/:entryId/order', async (req, res) => {
router.delete('/:userCollectionId/entries/:entryId', async (req, res) => {
try {
const { userCollectionId, entryId } = req.params;
const deleted = await UserCollectionEntry.destroy({
const deleted = await UserCollectionEntry.findOne({
where: {
user_collection_id: userCollectionId,
entry_id: entryId
}
});
console.log(`deleteing : `, deleted)
const deletedCount = deleted.destroy();
if (deleted) {
res.status(204).send();