Add support for bulk reading progress reset

This commit is contained in:
aditya.chandel
2025-07-14 09:19:22 -06:00
committed by Aditya Chandel
parent ec7846abf6
commit 77d68108ab
6 changed files with 133 additions and 25 deletions
@@ -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<Book> resetProgress(@PathVariable long bookId) {
Book book = bookService.resetProgress(bookId);
return ResponseEntity.ok(book);
@PostMapping("/reset-progress")
public ResponseEntity<List<Book>> resetProgress(@RequestBody List<Long> bookIds) {
if (bookIds == null || bookIds.isEmpty()) {
throw ApiError.BAD_REQUEST.createException("No book IDs provided");
}
List<Book> updatedBooks = bookService.resetProgress(bookIds);
return ResponseEntity.ok(updatedBooks);
}
}
@@ -285,22 +285,34 @@ public class BookService {
return book;
}
public Book resetProgress(long bookId) {
public List<Book> resetProgress(List<Long> bookIds) {
BookLoreUser user = authenticationService.getAuthenticatedUser();
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
List<Book> updatedBooks = new ArrayList<>();
Optional<BookLoreUserEntity> 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;
}