diff --git a/backend/routes/user-collection.routes.js b/backend/routes/user-collection.routes.js index cf48347..bda6511 100644 --- a/backend/routes/user-collection.routes.js +++ b/backend/routes/user-collection.routes.js @@ -301,18 +301,18 @@ collectionRouter.delete('/:id', async (req, res) => { }); // * POST - ADD ENTRY TO A COLLECTION -collectionRouter.post('/:userCollectionId/entries/:entryId', async (req, res) => { +collectionRouter.post('/:collectionId/entries/:entryId', async (req, res) => { try { - const { userCollectionId, entryId } = req.params; + const { collectionId, entryId } = req.params; const newUserCollectionEntry = await UserCollectionEntry.create({ - user_collection_id: userCollectionId, + user_collection_id: collectionId, entry_id: entryId }); // ? set a default poster if none exists - const userCollection = await Collection.findByPk(userCollectionId); - if (!userCollection.poster_path || userCollection.poster_path.trim() === '') { + const userCollection = await Collection.findByPk(collectionId); + if (!userCollection.poster_path || userCollection.poster_path.trim() == '') { const entry = await Entry.findByPk(entryId); userCollection.poster_path = entry.poster_path; await userCollection.save(); @@ -326,15 +326,15 @@ collectionRouter.post('/:userCollectionId/entries/:entryId', async (req, res) => }); // * PATCH - UPDATE ENTRY ORDER IN COLLECTION -collectionRouter.patch('/:userCollectionId/entries/:entryId', async (req, res) => { +collectionRouter.patch('/:collectionId/entries/:entryId', async (req, res) => { try { - const { userCollectionId, entryId } = req.params; + const { collectionId, entryId } = req.params; const { newOrder } = req.body; // ? getting the collectionEntry const collectionEntry = await UserCollectionEntry.findOne({ where: { - user_collection_id: userCollectionId, + user_collection_id: collectionId, entry_id: entryId } }); @@ -354,23 +354,21 @@ collectionRouter.patch('/:userCollectionId/entries/:entryId', async (req, res) = }); // * DELETE - REMOVE AN ENTRY FROM A COLLECTION -collectionRouter.delete('/:userCollectionId/entries/:entryId', async (req, res) => { +collectionRouter.delete('/:collectionId/entries/:entryId', async (req, res) => { try { - const { userCollectionId, entryId } = req.params; + const { collectionId, entryId } = req.params; const deleted = await UserCollectionEntry.findOne({ where: { - user_collection_id: userCollectionId, + user_collection_id: collectionId, entry_id: entryId } }); - console.log(`deleteing : `, deleted) - - const deletedCount = deleted.destroy(); if (deleted) { + await deleted.destroy(); res.status(204).send(); } else { - res.status(404).json({ message: 'UserCollection entry not found.' }); + res.status(404).json({ message: 'CollectionEntry not found.' }); } } catch (error) { res.status(500).json({ error: error.message }); diff --git a/frontend/src/app/core/services/collection.service.ts b/frontend/src/app/core/services/collection.service.ts index 4bf1c66..5fa04a2 100644 --- a/frontend/src/app/core/services/collection.service.ts +++ b/frontend/src/app/core/services/collection.service.ts @@ -23,7 +23,7 @@ export class CollectionService { } // get a list by id - getCollection(collectionId: string) { + getCollection(collectionId: number): Observable { return this.http.get( `${this.backend_apiUrl}/${this.apiAction}/${collectionId}` ); diff --git a/frontend/src/app/features/collections/collection-info/collection-entry-card/collection-entry-card.component.ts b/frontend/src/app/features/collections/collection-info/collection-entry-card/collection-entry-card.component.ts index b074f5d..64c88ac 100644 --- a/frontend/src/app/features/collections/collection-info/collection-entry-card/collection-entry-card.component.ts +++ b/frontend/src/app/features/collections/collection-info/collection-entry-card/collection-entry-card.component.ts @@ -94,15 +94,15 @@ export class CollectionEntryCardComponent implements OnInit { .removeEntryFromCollection(this.collectionId, this.entry.id) .subscribe({ next: (response) => { - // TODO confirm notification - - console.log('Entry successfully removed from list.', response); + // TODO toast: entry removed from collection + // console.log('Entry successfully removed from collection.', response); + this.emitRefrechEvent(); this.entry.id = 0; this.entry.isInLibrary = false; }, error: (error) => { // TODO error notification - console.error('Error removing entry from list:', error); + console.error('Error removing entry from collection:', error); }, }); } @@ -116,6 +116,6 @@ export class CollectionEntryCardComponent implements OnInit { // ? EVENT EMITTER emitRefrechEvent() { - this.refrechEvent.emit(); + this.refrechEvent.emit({ type: 'delete', id: this.entry.id }); } } diff --git a/frontend/src/app/features/collections/collection-info/collection-entry-grid/collection-entry-grid.component.html b/frontend/src/app/features/collections/collection-info/collection-entry-grid/collection-entry-grid.component.html index c820e47..7e8a5c7 100644 --- a/frontend/src/app/features/collections/collection-info/collection-entry-grid/collection-entry-grid.component.html +++ b/frontend/src/app/features/collections/collection-info/collection-entry-grid/collection-entry-grid.component.html @@ -1,6 +1,6 @@
- diff --git a/frontend/src/app/features/collections/collection-info/collection-entry-grid/collection-entry-grid.component.ts b/frontend/src/app/features/collections/collection-info/collection-entry-grid/collection-entry-grid.component.ts index 237f77e..4374a2f 100644 --- a/frontend/src/app/features/collections/collection-info/collection-entry-grid/collection-entry-grid.component.ts +++ b/frontend/src/app/features/collections/collection-info/collection-entry-grid/collection-entry-grid.component.ts @@ -1,4 +1,4 @@ -import { Component, Input } from '@angular/core'; +import { Component, EventEmitter, Input, Output } from '@angular/core'; import { Entry } from '../../../../shared/models/entry.model'; import { @@ -23,8 +23,9 @@ import { export class CollectionEntryGridComponent { @Input() entries: Entry[] = []; @Input() collectionId!: number; + @Output() refrechEvent = new EventEmitter(); - refrechEventReceiver() { - console.log('event recieved'); + onRefrechEvent(eventData: any) { + this.refrechEvent.emit(eventData); } } diff --git a/frontend/src/app/features/collections/collection-info/collection-entry-table/collection-entry-table.component.html b/frontend/src/app/features/collections/collection-info/collection-entry-table/collection-entry-table.component.html index 16c5fc0..3b3c62d 100644 --- a/frontend/src/app/features/collections/collection-info/collection-entry-table/collection-entry-table.component.html +++ b/frontend/src/app/features/collections/collection-info/collection-entry-table/collection-entry-table.component.html @@ -3,7 +3,7 @@
- +
diff --git a/frontend/src/app/features/collections/collection-info/collection-info-page/collection-info-page.component.html b/frontend/src/app/features/collections/collection-info/collection-info-page/collection-info-page.component.html index 3f03643..2200e99 100644 --- a/frontend/src/app/features/collections/collection-info/collection-info-page/collection-info-page.component.html +++ b/frontend/src/app/features/collections/collection-info/collection-info-page/collection-info-page.component.html @@ -14,6 +14,10 @@ + + + +
@@ -58,8 +62,8 @@

{{collection.name}}

- +
diff --git a/frontend/src/app/features/collections/collection-info/collection-info-page/collection-info-page.component.ts b/frontend/src/app/features/collections/collection-info/collection-info-page/collection-info-page.component.ts index 7481c2d..34d60a1 100644 --- a/frontend/src/app/features/collections/collection-info/collection-info-page/collection-info-page.component.ts +++ b/frontend/src/app/features/collections/collection-info/collection-info-page/collection-info-page.component.ts @@ -8,6 +8,7 @@ import { MatDialog } from '@angular/material/dialog'; import { ConfirmModalComponent } from '../../../../shared/components/modals/confirm-modal/confirm-modal.component'; import { ScreenshotService } from '../../../../core/services/screenshot.service'; import { CollectionEntryDTO } from '../../../../shared/models/dtos/collection-entry-dto.model'; +import { environment } from '../../../../../environments/environment.prod'; @Component({ selector: 'app-collection-info-page', @@ -35,11 +36,18 @@ export class CollectionInfoPageComponent implements OnInit { ngOnInit(): void { const id = this.route.snapshot.paramMap.get('id'); if (id == null) return; // todo redirect if no id is provided + this.collection.id = parseInt(id); - this.collectionService.getCollection(id.toString()).subscribe({ - next: (data) => { - this.collection = data as unknown as Collection; - this.collection.entries = this.collection.entries as CollectionEntryDTO[]; + this.fetchCollection(); + } + + fetchCollection() { + this.collectionService.getCollection(this.collection.id).subscribe({ + next: (result) => { + this.collection = result; + + if (!this.collection.isUserCollection) + this.collection.entries = this.sortEntries(this.collection.entries); this.createdAt = this.getCreatedDate()?.toString() || ''; this.updatedAt = this.getUpdatedAt()?.toString() || ''; @@ -54,6 +62,13 @@ export class CollectionInfoPageComponent implements OnInit { }); } + sortEntries(entries: CollectionEntryDTO[]) { + return entries.sort( + (a, b) => + new Date(a.release_date).getTime() - new Date(b.release_date).getTime() + ); + } + getListId() { return this.collection.id + ''; } @@ -105,13 +120,13 @@ export class CollectionInfoPageComponent implements OnInit { if (!this.collection) return; const dialogRef = this.dialog.open(CollectionEditModalComponent, { - width: '360px', + width: '50%', data: { name: this.collection.name, description: this.collection.description, userRating: this.collection.userRating || 0, - poster: this.collection.posterPath, - backdrop: this.collection.backdropPath, + poster: this.getCollectionPosterPath(this.collection), + backdrop: this.getCollectionBackdropPath(this.collection), }, }); @@ -158,18 +173,16 @@ export class CollectionInfoPageComponent implements OnInit { if (result) { if (result.confirmed != true) return; - this.collectionService - .deleteCollection(this.collection.id) - .subscribe({ - next: () => { - // TODO confirm notification - this.router.navigate(['/collections']); - }, - error: (error) => { - console.error('Error updating collection:', error); - // TODO error notification - }, - }); + this.collectionService.deleteCollection(this.collection.id).subscribe({ + next: () => { + // TODO confirm notification + this.router.navigate(['/collections']); + }, + error: (error) => { + console.error('Error updating collection:', error); + // TODO error notification + }, + }); } }); } @@ -212,7 +225,8 @@ export class CollectionInfoPageComponent implements OnInit { this.collectionService.refreshCollection(this.collection.id).subscribe({ next: (data) => { this.collection = data as unknown as Collection; - this.collection.entries = this.collection.entries as CollectionEntryDTO[]; + this.collection.entries = this.collection + .entries as CollectionEntryDTO[]; this.createdAt = this.getCreatedDate()?.toString() || ''; this.updatedAt = this.getUpdatedAt()?.toString() || ''; @@ -228,25 +242,23 @@ export class CollectionInfoPageComponent implements OnInit { } getCollectionParts() { - this.collectionService - .getCollectionParts(this.collection.id) - .subscribe({ - next: (data) => { - this.collection = data as unknown as Collection; - this.collection.entries = this.collection - .entries as CollectionEntryDTO[]; + this.collectionService.getCollectionParts(this.collection.id).subscribe({ + next: (data) => { + this.collection = data as unknown as Collection; + this.collection.entries = this.collection + .entries as CollectionEntryDTO[]; - this.createdAt = this.getCreatedDate()?.toString() || ''; - this.updatedAt = this.getUpdatedAt()?.toString() || ''; - // console.log( - // `Collection "${this.collection.name}" received:`, - // this.collection - // ); - }, - error: (error) => { - console.error('Error occurred:', error); - }, - }); + this.createdAt = this.getCreatedDate()?.toString() || ''; + this.updatedAt = this.getUpdatedAt()?.toString() || ''; + // console.log( + // `Collection "${this.collection.name}" received:`, + // this.collection + // ); + }, + error: (error) => { + console.error('Error occurred:', error); + }, + }); } redirectToTMDB() { @@ -255,4 +267,88 @@ export class CollectionInfoPageComponent implements OnInit { '_blank' ); } + + getCollectionPosterPath(collection: Collection) { + // console.log(`collection media:`, collection.medias); + if (collection.medias && collection.medias.length != 0) { + const posters = collection.medias.filter((m) => m.type == 'poster'); + if (posters.length != 0) { + const selectedPosters = posters.filter((m) => m.isSelected); + + // console.log(selectedPosters); + let selectedPoster; + if (selectedPosters.length != 0) { + selectedPoster = selectedPosters[0].path; + } else { + selectedPoster = posters[0].path; + } + + return ( + environment.API_URL + + `/media/collections/posters/preview/${selectedPoster}` + ); + } + } + + // if (collection.entries && collection.entries.length != 0) { + // // * use the poster of the first entry in the collection + // // * if the collection is empty + // return this.getEntryBackdropPath(collection.entries[0]); + // } else + if (collection.posterPath) { + // * use first remote tmdb poster + // * if collection has none and has no entry + return `https://image.tmdb.org/t/p/w1280/${collection.posterPath}`; + } else { + // * use default poster + // * if collection has totaly no poster + return 'assets/images/default/default-poster.png'; + } + } + + getCollectionBackdropPath(collection: Collection) { + // console.log(`collection media:`, collection.medias); + if (collection.medias && collection.medias.length != 0) { + const backdrops = collection.medias.filter((m) => m.type == 'backdrop'); + if (backdrops.length != 0) { + const selectedBackdrops = backdrops.filter((m) => m.isSelected); + + // console.log(selectedBackdrops); + let selectedBackdrop; + if (selectedBackdrops.length != 0) { + selectedBackdrop = selectedBackdrops[0].path; + } else { + selectedBackdrop = backdrops[0].path; + } + + return ( + environment.API_URL + + `/media/collections/backdrops/preview/${selectedBackdrop}` + ); + } + } + + // if (collection.entries && collection.entries.length != 0) { + // // * use the poster of the first entry in the collection + // // * if the collection is empty + // return this.getEntryBackdropPath(collection.entries[0]); + // } else + if (collection.backdropPath) { + // * use first remote tmdb poster + // * if collection has none and has no entry + return `https://image.tmdb.org/t/p/w1280/${collection.backdropPath}`; + } else { + // * use default poster + // * if collection has totaly no poster + return 'assets/images/default/default-backdrop.png'; + } + } + + onRefrechEvent(eventData: any) { + if (eventData.type == 'delete') { + this.collection.entries = this.collection.entries.filter( + (e) => e.id != eventData.id + ); + } + } } diff --git a/frontend/src/app/features/collections/collections/collection-card/collection-card.component.ts b/frontend/src/app/features/collections/collections/collection-card/collection-card.component.ts index 15619ca..830c80b 100644 --- a/frontend/src/app/features/collections/collections/collection-card/collection-card.component.ts +++ b/frontend/src/app/features/collections/collections/collection-card/collection-card.component.ts @@ -3,8 +3,6 @@ import { Collection } from '../../../../shared/models/collection.model'; import { CollectionService } from '../../../../core/services/collection.service'; import { Router } from '@angular/router'; import { trigger, transition, style, animate } from '@angular/animations'; -import { environment } from '../../../../../environments/environment.prod'; -import { Entry } from '../../../../shared/models/entry.model'; @Component({ selector: 'app-collection-card', @@ -27,7 +25,7 @@ import { Entry } from '../../../../shared/models/entry.model'; export class CollectionCardComponent { @Input() collection!: Collection; - constructor(private listService: CollectionService, private router: Router) {} + constructor(private collectionService: CollectionService, private router: Router) {} getCreatedDate(): string { if (this.collection.createdAt == null) return ''; @@ -40,6 +38,6 @@ export class CollectionCardComponent { } deleteList() { - this.listService.deleteCollection(this.collection.id); + this.collectionService.deleteCollection(this.collection.id); } } diff --git a/frontend/src/app/shared/components/modals/list-selection-modal/user-collections-modal.component.ts b/frontend/src/app/shared/components/modals/list-selection-modal/user-collections-modal.component.ts index b0483ef..6f1b8bf 100644 --- a/frontend/src/app/shared/components/modals/list-selection-modal/user-collections-modal.component.ts +++ b/frontend/src/app/shared/components/modals/list-selection-modal/user-collections-modal.component.ts @@ -3,7 +3,7 @@ import { FormControl } from '@angular/forms'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; import { CollectionService } from '../../../../core/services/collection.service'; import { NotificationPopupComponent } from '../../notification-popup/notification-popup.component'; -import { NotificationService } from '../../../services/notification.service'; +import { ToastService } from '../../../services/toast.service'; import { Collection } from '../../../models/collection.model'; import { @@ -50,7 +50,7 @@ export class UserCollectionsModalComponent implements OnInit { public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: any, private collectionService: CollectionService, - private notificationService: NotificationService + private toastService: ToastService ) {} ngOnInit() { @@ -174,6 +174,6 @@ export class UserCollectionsModalComponent implements OnInit { showNotification(title: string, message: string, type: string): void { // this.notificationComponent.showNotification(message, type); - this.notificationService.showNotification(title, message, type); + this.toastService.show(title, message, type); } }