implemented UserCollectionEntry order update on the backend

This commit is contained in:
X4Lo committed 2024-04-30 23:53:47 +01:00
1 parent 537763ff7d
commit 1b21d84d0b
5 files changed
+48 -35

No files matched your search

+27 -15
View File
@@ -18,24 +18,21 @@ const sequelize = require('./database');
// * entry add // * entry add
UserCollectionEntry.afterCreate(async (instance, options) => { UserCollectionEntry.afterCreate(async (instance, options) => {
try { try {
// ? mimicking a change to trigger updatedAt property update for the UserCollection
const userCollections = await UserCollection.findByPk(instance.user_collection_id); const userCollections = await UserCollection.findByPk(instance.user_collection_id);
await userCollections.update({
name: sequelize.literal('name')
});
const userCollectionEntriesCount = await UserCollectionEntry.count({ const userCollectionEntriesCount = await UserCollectionEntry.count({
where: { where: {
user_collection_id: instance.user_collection_id user_collection_id: instance.user_collection_id
} }
}) })
// ? updating the order // ? updating newly create UserCollectionEntry order
await instance.update({ await instance.update({
order: userCollectionEntriesCount order: userCollectionEntriesCount
}) })
// ? mimicking a change to trigger updatedAt property update
await userCollections.update({
name: sequelize.literal('name')
})
} catch (error) { } catch (error) {
console.log(`Sequelize Hook Error: UserCollectionEntry.afterCreate: `, error) console.log(`Sequelize Hook Error: UserCollectionEntry.afterCreate: `, error)
} }
@@ -43,15 +40,30 @@ UserCollectionEntry.afterCreate(async (instance, options) => {
// * entry update (order) // * entry update (order)
UserCollectionEntry.afterUpdate(async (instance, options) => { UserCollectionEntry.afterUpdate(async (instance, options) => {
console.log("we're in after update!!!!!")
try { try {
// ? mimicking a change to trigger updatedAt property update for the UserCollection
const userCollections = await UserCollection.findByPk(instance.user_collection_id); const userCollections = await UserCollection.findByPk(instance.user_collection_id);
// ? mimicking a change to trigger updatedAt property update
await userCollections.update({ await userCollections.update({
name: sequelize.literal('name') 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) { } catch (error) {
console.log(`Sequelize Hook Error: UserCollectionEntry.afterUpdate: `, error) console.log(`Sequelize Hook Error: UserCollectionEntry.afterUpdate: `, error)
} }
@@ -95,7 +107,7 @@ UserCollectionEntry.afterDestroy(async (instance, options) => {
if (instance.order != collectionsEntries.length) { if (instance.order != collectionsEntries.length) {
for (let i = 0; i < collectionsEntries.length; i++) { for (let i = 0; i < collectionsEntries.length; i++) {
if (collectionsEntries[i].order > instance.order) { if (collectionsEntries[i].order > instance.order) {
collectionsEntries[i].update({ order: order }) collectionsEntries[i].update({ order: order }, { hooks: false })
order++; order++;
} }
} }
@@ -110,17 +122,17 @@ UserCollectionEntry.afterDestroy(async (instance, options) => {
// ? entry-seen // ? entry-seen
// * entry-seen create // * entry-seen create
EntrySeen.afterCreate(async (isntance, options) => { EntrySeen.afterCreate(async (instance, options) => {
}); });
// * entry-seen udpate // * entry-seen udpate
EntrySeen.afterUpdate(async (isntance, options) => { EntrySeen.afterUpdate(async (instance, options) => {
}); });
// * entry-seen delete // * entry-seen delete
EntrySeen.afterDestroy(async (isntance, options) => { EntrySeen.afterDestroy(async (instance, options) => {
}); });
+16 -15
View File
@@ -123,33 +123,34 @@ router.post('/:userCollectionId/entries/:entryId', async (req, res) => {
}); });
// * PATCH - UPDATE ENTRY ORDER IN UserCollection // * PATCH - UPDATE ENTRY ORDER IN UserCollection
router.patch('/:userCollectionId/entries/:entryId/order', async (req, res) => { router.patch('/:userCollectionId/entries/:entryId', async (req, res) => {
try { try {
const { userCollectionId, entryId } = req.params; 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 // ? getting the collectionEntry
const updated = await UserCollectionEntry.update( const collectionEntry = await UserCollectionEntry.findOne({
{ entryOrder: newOrder }, // Set new order
{
where: { where: {
userCollectionId: userCollectionId, user_collection_id: userCollectionId,
entryId: entryId entry_id: entryId
} }
} });
);
if (updated[0] > 0) { if (!collectionEntry) {
res.status(200).json({ message: 'Entry order updated successfully.' }); res.status(404).json({ message: 'UserCollectionEntry not found.' });
} else {
res.status(404).json({ message: 'UserCollection entry not found.' });
} }
// ? updating
const updated = await collectionEntry.update({ order: newOrder });
res.status(200).json({ message: 'UserCollectionEntry order updated successfully.' });
} catch (error) { } catch (error) {
console.log(`Error Updating UserCollectionEntry order: `, error);
res.status(500).json({ error: error.message }); 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) => { router.delete('/:userCollectionId/entries/:entryId', async (req, res) => {
try { try {
const { userCollectionId, entryId } = req.params; const { userCollectionId, entryId } = req.params;
@@ -61,8 +61,8 @@ export class UserCollectionService {
newOrder: number newOrder: number
): Observable<any> { ): Observable<any> {
return this.http.patch( return this.http.patch(
`${this.backend_apiUrl}/${this.apiAction}/${userCollectionId}/entries/${entryId}/order`, `${this.backend_apiUrl}/${this.apiAction}/${userCollectionId}/entries/${entryId}`,
{ newOrder } { newOrder: newOrder }
); );
} }
@@ -25,7 +25,7 @@
<!-- <a [href]="getTmdbLink()" target="_blank" rel="noopener noreferrer"> <!-- <a [href]="getTmdbLink()" target="_blank" rel="noopener noreferrer">
<h3 class="card-title">{{ entry.title }}</h3> <h3 class="card-title">{{ entry.title }}</h3>
</a> --> </a> -->
<a (click)="cardClick()" rel="noopener noreferrer"> <a>
<h3 class="card-title">{{ entry.title }}</h3> <h3 class="card-title">{{ entry.title }}</h3>
</a> </a>
<div class="card-info"> <div class="card-info">
@@ -27,7 +27,7 @@ export class UserCollectionGridCardComponent {
constructor( constructor(
private libraryService: LibraryService, private libraryService: LibraryService,
private listService: UserCollectionService, private userCollectionService: UserCollectionService,
public dialog: MatDialog, public dialog: MatDialog,
private router: Router private router: Router
) {} ) {}
@@ -81,7 +81,7 @@ export class UserCollectionGridCardComponent {
removeEntryFromList() { removeEntryFromList() {
// TODO: add confirm modal // TODO: add confirm modal
this.listService this.userCollectionService
.removeEntryFromCollection(this.listId, this.entry.id) .removeEntryFromCollection(this.listId, this.entry.id)
.subscribe({ .subscribe({
next: (response) => { next: (response) => {