From 77d68108abafe6f343e63aa47b93c5e47bb30142 Mon Sep 17 00:00:00 2001 From: "aditya.chandel" Date: Mon, 14 Jul 2025 09:18:38 -0600 Subject: [PATCH] Add support for bulk reading progress reset --- .../booklore/controller/BookController.java | 13 +++--- .../booklore/service/BookService.java | 40 +++++++++++------ .../book-browser/book-browser.component.html | 15 +++++-- .../book-browser/book-browser.component.ts | 43 +++++++++++++++++++ .../book-card/book-card.component.ts | 40 +++++++++++++++++ .../src/app/book/service/book.service.ts | 7 +-- 6 files changed, 133 insertions(+), 25 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/controller/BookController.java b/booklore-api/src/main/java/com/adityachandel/booklore/controller/BookController.java index 94639df66..9c3758158 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/controller/BookController.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/controller/BookController.java @@ -1,6 +1,7 @@ package com.adityachandel.booklore.controller; import com.adityachandel.booklore.config.security.annotation.CheckBookAccess; +import com.adityachandel.booklore.exception.ApiError; import com.adityachandel.booklore.model.dto.Book; import com.adityachandel.booklore.model.dto.BookRecommendation; import com.adityachandel.booklore.model.dto.BookViewerSettings; @@ -125,10 +126,12 @@ public class BookController { return ResponseEntity.noContent().build(); } - @PostMapping("/{bookId}/reset-progress") - @CheckBookAccess(bookIdParam = "bookId") - public ResponseEntity resetProgress(@PathVariable long bookId) { - Book book = bookService.resetProgress(bookId); - return ResponseEntity.ok(book); + @PostMapping("/reset-progress") + public ResponseEntity> resetProgress(@RequestBody List bookIds) { + if (bookIds == null || bookIds.isEmpty()) { + throw ApiError.BAD_REQUEST.createException("No book IDs provided"); + } + List updatedBooks = bookService.resetProgress(bookIds); + return ResponseEntity.ok(updatedBooks); } } \ No newline at end of file diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/BookService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/BookService.java index 3dc3176b5..79cbe1248 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/BookService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/BookService.java @@ -285,22 +285,34 @@ public class BookService { return book; } - public Book resetProgress(long bookId) { + public List resetProgress(List bookIds) { BookLoreUser user = authenticationService.getAuthenticatedUser(); - BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); + List updatedBooks = new ArrayList<>(); + Optional userEntity = userRepository.findById(user.getId()); - UserBookProgressEntity progress = userBookProgressRepository.findByUserIdAndBookId(user.getId(), bookId).orElse(new UserBookProgressEntity()); - progress.setBook(bookEntity); - progress.setReadStatus(null); - progress.setLastReadTime(null); - progress.setPdfProgress(null); - progress.setPdfProgressPercent(null); - progress.setEpubProgress(null); - progress.setEpubProgressPercent(null); - progress.setCbxProgress(null); - progress.setCbxProgressPercent(null); - userBookProgressRepository.save(progress); - return bookMapper.toBook(bookEntity); + for (Long bookId : bookIds) { + BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); + + UserBookProgressEntity progress = userBookProgressRepository + .findByUserIdAndBookId(user.getId(), bookId) + .orElse(new UserBookProgressEntity()); + + progress.setBook(bookEntity); + progress.setUser(userEntity.orElseThrow()); + progress.setReadStatus(null); + progress.setLastReadTime(null); + progress.setPdfProgress(null); + progress.setPdfProgressPercent(null); + progress.setEpubProgress(null); + progress.setEpubProgressPercent(null); + progress.setCbxProgress(null); + progress.setCbxProgressPercent(null); + + userBookProgressRepository.save(progress); + updatedBooks.add(bookMapper.toBook(bookEntity)); + } + + return updatedBooks; } diff --git a/booklore-ui/src/app/book/components/book-browser/book-browser.component.html b/booklore-ui/src/app/book/components/book-browser/book-browser.component.html index b593c097f..d3fe6b0d7 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-browser.component.html +++ b/booklore-ui/src/app/book/components/book-browser/book-browser.component.html @@ -293,9 +293,9 @@
@if (entityType$ | async; as entityType) { -
+
@if (userData.permissions.canEditMetadata) { - +
} -
+
this.multiBookEditMetadata() } ]; + + this.moreActionsMenuItems = [ + { + label: 'Reset Progress', + icon: 'pi pi-undo', + command: () => { + this.confirmationService.confirm({ + message: 'Are you sure you want to reset progress for selected books?', + header: 'Confirm Reset', + icon: 'pi pi-exclamation-triangle', + acceptLabel: 'Yes', + rejectLabel: 'No', + accept: () => { + this.resetProgress(); + } + }); + } + } + ]; } + ngAfterViewInit() { combineLatest({ @@ -688,4 +709,26 @@ export class BookBrowserComponent implements OnInit, AfterViewInit { moveFiles() { this.dialogHelperService.openFileMoverDialog(this.selectedBooks); } + + resetProgress() { + const bookIds = Array.from(this.selectedBooks); + this.bookService.resetProgress(bookIds).subscribe({ + next: () => { + this.messageService.add({ + severity: 'success', + summary: 'Progress Reset', + detail: 'Reading progress has been reset.', + life: 1500 + }); + }, + error: () => { + this.messageService.add({ + severity: 'error', + summary: 'Failed', + detail: 'Could not reset progress.', + life: 1500 + }); + } + }); + } } diff --git a/booklore-ui/src/app/book/components/book-browser/book-card/book-card.component.ts b/booklore-ui/src/app/book/components/book-browser/book-card/book-card.component.ts index 45bf0ce9f..823f28b2f 100644 --- a/booklore-ui/src/app/book/components/book-browser/book-card/book-card.component.ts +++ b/booklore-ui/src/app/book/components/book-browser/book-card/book-card.component.ts @@ -122,6 +122,7 @@ export class BookCardComponent implements OnInit, OnDestroy { }, }, ...this.getPermissionBasedMenuItems(), + ...this.moreMenuItems(), ]; } @@ -258,6 +259,45 @@ export class BookCardComponent implements OnInit, OnDestroy { return items; } + private moreMenuItems(): MenuItem[] { + const items: MenuItem[] = []; + + if (this.hasEditMetadataPermission()) { + items.push({ + label: 'More Actions', + icon: 'pi pi-ellipsis-v', + items: [ + { + label: 'Reset Progress', + icon: 'pi pi-undo', + command: () => { + this.bookService.resetProgress(this.book.id).subscribe({ + next: () => { + this.messageService.add({ + severity: 'success', + summary: 'Progress Reset', + detail: 'Reading progress has been reset.', + life: 1500 + }); + }, + error: () => { + this.messageService.add({ + severity: 'error', + summary: 'Failed', + detail: 'Could not reset progress.', + life: 1500 + }); + } + }); + }, + } + ] + }); + } + + return items; + } + private openShelfDialog(): void { this.dialogService.open(ShelfAssignerComponent, { header: `Update Book's Shelves`, diff --git a/booklore-ui/src/app/book/service/book.service.ts b/booklore-ui/src/app/book/service/book.service.ts index 46707c23e..2e367d36d 100644 --- a/booklore-ui/src/app/book/service/book.service.ts +++ b/booklore-ui/src/app/book/service/book.service.ts @@ -397,9 +397,10 @@ export class BookService { ); } - resetProgress(bookId: number): Observable { - return this.http.post(`${this.url}/${bookId}/reset-progress`, null).pipe( - tap(updatedBook => this.handleBookUpdate(updatedBook)) + resetProgress(bookIds: number | number[]): Observable { + const ids = Array.isArray(bookIds) ? bookIds : [bookIds]; + return this.http.post(`${this.url}/reset-progress`, ids).pipe( + tap(updatedBooks => updatedBooks.forEach(book => this.handleBookUpdate(book))) ); }