From 1b21d84d0b326cb554d37a15a96621965f0e9442 Mon Sep 17 00:00:00 2001 From: Mohammed Abad Date: Tue, 30 Apr 2024 23:53:47 +0100 Subject: [PATCH] implemented UserCollectionEntry order update on the backend --- backend/db/sequelizeHooks.js | 42 ++++++++++++------- backend/routes/userCollectionRoutes.js | 33 ++++++++------- .../core/services/userCollection.service.ts | 4 +- .../userCollection-grid-card.component.html | 2 +- .../userCollection-grid-card.component.ts | 4 +- 5 files changed, 49 insertions(+), 36 deletions(-) diff --git a/backend/db/sequelizeHooks.js b/backend/db/sequelizeHooks.js index 9fc1fb8..413fd8a 100644 --- a/backend/db/sequelizeHooks.js +++ b/backend/db/sequelizeHooks.js @@ -18,24 +18,21 @@ const sequelize = require('./database'); // * entry add UserCollectionEntry.afterCreate(async (instance, options) => { try { + // ? mimicking a change to trigger updatedAt property update for the UserCollection const userCollections = await UserCollection.findByPk(instance.user_collection_id); - + await userCollections.update({ + name: sequelize.literal('name') + }); const userCollectionEntriesCount = await UserCollectionEntry.count({ where: { user_collection_id: instance.user_collection_id } }) - // ? updating the order + // ? updating newly create UserCollectionEntry order await instance.update({ order: userCollectionEntriesCount }) - - - // ? mimicking a change to trigger updatedAt property update - await userCollections.update({ - name: sequelize.literal('name') - }) } catch (error) { console.log(`Sequelize Hook Error: UserCollectionEntry.afterCreate: `, error) } @@ -43,15 +40,30 @@ UserCollectionEntry.afterCreate(async (instance, options) => { // * entry update (order) UserCollectionEntry.afterUpdate(async (instance, options) => { + console.log("we're in after update!!!!!") try { + // ? mimicking a change to trigger updatedAt property update for the UserCollection const userCollections = await UserCollection.findByPk(instance.user_collection_id); - - // ? mimicking a change to trigger updatedAt property update await userCollections.update({ name: sequelize.literal('name') + }); + + // ? updating other entries order + const collectionEntries = await UserCollectionEntry.findAll({ + where: { + user_collection_id: instance.user_collection_id + }, + order: [['order', 'ASC']] }) - // TODO reorder entries + // * changing the order of all entries that are now after the updated entry + let startOrder = instance.order + 1; + for (let i = 0; i < collectionEntries.length; i++) { + if (collectionEntries[i].order >= instance.order && collectionEntries[i].entry_id != instance.entry_id) { + await collectionEntries[i].update({ order: startOrder }, { hooks: false }) + startOrder++; + } + } } catch (error) { console.log(`Sequelize Hook Error: UserCollectionEntry.afterUpdate: `, error) } @@ -95,7 +107,7 @@ UserCollectionEntry.afterDestroy(async (instance, options) => { if (instance.order != collectionsEntries.length) { for (let i = 0; i < collectionsEntries.length; i++) { if (collectionsEntries[i].order > instance.order) { - collectionsEntries[i].update({ order: order }) + collectionsEntries[i].update({ order: order }, { hooks: false }) order++; } } @@ -110,17 +122,17 @@ UserCollectionEntry.afterDestroy(async (instance, options) => { // ? entry-seen // * entry-seen create -EntrySeen.afterCreate(async (isntance, options) => { +EntrySeen.afterCreate(async (instance, options) => { }); // * entry-seen udpate -EntrySeen.afterUpdate(async (isntance, options) => { +EntrySeen.afterUpdate(async (instance, options) => { }); // * entry-seen delete -EntrySeen.afterDestroy(async (isntance, options) => { +EntrySeen.afterDestroy(async (instance, options) => { }); diff --git a/backend/routes/userCollectionRoutes.js b/backend/routes/userCollectionRoutes.js index a848080..ecb09b1 100644 --- a/backend/routes/userCollectionRoutes.js +++ b/backend/routes/userCollectionRoutes.js @@ -123,33 +123,34 @@ router.post('/:userCollectionId/entries/:entryId', async (req, res) => { }); // * PATCH - UPDATE ENTRY ORDER IN UserCollection -router.patch('/:userCollectionId/entries/:entryId/order', async (req, res) => { +router.patch('/:userCollectionId/entries/:entryId', async (req, res) => { try { const { userCollectionId, entryId } = req.params; - const { newOrder } = req.body; // The new order value + const { newOrder } = req.body; - // Assuming you have a model UserCollectionEntry for the junction table - const updated = await UserCollectionEntry.update( - { entryOrder: newOrder }, // Set new order - { - where: { - userCollectionId: userCollectionId, - entryId: entryId - } + // ? getting the collectionEntry + const collectionEntry = await UserCollectionEntry.findOne({ + where: { + user_collection_id: userCollectionId, + entry_id: entryId } - ); + }); - if (updated[0] > 0) { - res.status(200).json({ message: 'Entry order updated successfully.' }); - } else { - res.status(404).json({ message: 'UserCollection entry not found.' }); + if (!collectionEntry) { + res.status(404).json({ message: 'UserCollectionEntry not found.' }); } + + // ? updating + const updated = await collectionEntry.update({ order: newOrder }); + + res.status(200).json({ message: 'UserCollectionEntry order updated successfully.' }); } catch (error) { + console.log(`Error Updating UserCollectionEntry order: `, error); res.status(500).json({ error: error.message }); } }); -// * DELETE - AND ENTRY FROM A UserCollection +// * DELETE - REMOVE AN ENTRY FROM A UserCollection router.delete('/:userCollectionId/entries/:entryId', async (req, res) => { try { const { userCollectionId, entryId } = req.params; diff --git a/frontend/src/app/core/services/userCollection.service.ts b/frontend/src/app/core/services/userCollection.service.ts index f699c47..a667687 100644 --- a/frontend/src/app/core/services/userCollection.service.ts +++ b/frontend/src/app/core/services/userCollection.service.ts @@ -61,8 +61,8 @@ export class UserCollectionService { newOrder: number ): Observable { return this.http.patch( - `${this.backend_apiUrl}/${this.apiAction}/${userCollectionId}/entries/${entryId}/order`, - { newOrder } + `${this.backend_apiUrl}/${this.apiAction}/${userCollectionId}/entries/${entryId}`, + { newOrder: newOrder } ); } diff --git a/frontend/src/app/features/collections/user-collection-info/userCollection-grid-card/userCollection-grid-card.component.html b/frontend/src/app/features/collections/user-collection-info/userCollection-grid-card/userCollection-grid-card.component.html index b17cd19..2c71007 100644 --- a/frontend/src/app/features/collections/user-collection-info/userCollection-grid-card/userCollection-grid-card.component.html +++ b/frontend/src/app/features/collections/user-collection-info/userCollection-grid-card/userCollection-grid-card.component.html @@ -25,7 +25,7 @@ - +

{{ entry.title }}

diff --git a/frontend/src/app/features/collections/user-collection-info/userCollection-grid-card/userCollection-grid-card.component.ts b/frontend/src/app/features/collections/user-collection-info/userCollection-grid-card/userCollection-grid-card.component.ts index 3a4cbad..21e03bf 100644 --- a/frontend/src/app/features/collections/user-collection-info/userCollection-grid-card/userCollection-grid-card.component.ts +++ b/frontend/src/app/features/collections/user-collection-info/userCollection-grid-card/userCollection-grid-card.component.ts @@ -27,7 +27,7 @@ export class UserCollectionGridCardComponent { constructor( private libraryService: LibraryService, - private listService: UserCollectionService, + private userCollectionService: UserCollectionService, public dialog: MatDialog, private router: Router ) {} @@ -81,7 +81,7 @@ export class UserCollectionGridCardComponent { removeEntryFromList() { // TODO: add confirm modal - this.listService + this.userCollectionService .removeEntryFromCollection(this.listId, this.entry.id) .subscribe({ next: (response) => {