implemented UserCollectionEntry order update on the backend
This commit is contained in:
1 parent
537763ff7d
commit
1b21d84d0b
5 files changed
+49
-36
No files matched your search
@@ -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) => {
|
||||
|
||||
});
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -61,8 +61,8 @@ export class UserCollectionService {
|
||||
newOrder: number
|
||||
): Observable<any> {
|
||||
return this.http.patch(
|
||||
`${this.backend_apiUrl}/${this.apiAction}/${userCollectionId}/entries/${entryId}/order`,
|
||||
{ newOrder }
|
||||
`${this.backend_apiUrl}/${this.apiAction}/${userCollectionId}/entries/${entryId}`,
|
||||
{ newOrder: newOrder }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@
|
||||
<!-- <a [href]="getTmdbLink()" target="_blank" rel="noopener noreferrer">
|
||||
<h3 class="card-title">{{ entry.title }}</h3>
|
||||
</a> -->
|
||||
<a (click)="cardClick()" rel="noopener noreferrer">
|
||||
<a>
|
||||
<h3 class="card-title">{{ entry.title }}</h3>
|
||||
</a>
|
||||
<div class="card-info">
|
||||
|
||||
+2
-2
@@ -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) => {
|
||||
|
||||
Reference in new issue
Block a user