Feature: Add support for recording when a book is finished, so that users can create Magic Shelves based on this information. (#803)
* Added migration for new field in user_book_progress table * Added ability to set date finished when book status changes to read, and reset date finished when book status changes to another value * Added ability to use date published in a Magic Shelf
This commit is contained in:
+3
-3
@@ -120,9 +120,9 @@ public class BookController {
|
||||
}
|
||||
|
||||
@PutMapping("/read-status")
|
||||
public ResponseEntity<Void> updateReadStatus(@RequestBody @Valid ReadStatusUpdateRequest request) {
|
||||
bookService.updateReadStatus(request.ids(), request.status());
|
||||
return ResponseEntity.noContent().build();
|
||||
public ResponseEntity<List<Book>> updateReadStatus(@RequestBody @Valid ReadStatusUpdateRequest request) {
|
||||
List<Book> updatedBooks = bookService.updateReadStatus(request.ids(), request.status());
|
||||
return ResponseEntity.ok(updatedBooks);
|
||||
}
|
||||
|
||||
@PostMapping("/reset-progress")
|
||||
|
||||
@@ -29,5 +29,6 @@ public class Book {
|
||||
private CbxProgress cbxProgress;
|
||||
private Set<Shelf> shelves;
|
||||
private String readStatus;
|
||||
private Instant dateFinished;
|
||||
private LibraryPath libraryPath;
|
||||
}
|
||||
|
||||
+3
@@ -51,4 +51,7 @@ public class UserBookProgressEntity {
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(name = "read_status")
|
||||
private ReadStatus readStatus;
|
||||
|
||||
@Column(name = "date_finished")
|
||||
private Instant dateFinished;
|
||||
}
|
||||
|
||||
@@ -80,6 +80,7 @@ public class BookService {
|
||||
setBookProgress(book, progress);
|
||||
book.setLastReadTime(progress.getLastReadTime());
|
||||
book.setReadStatus(String.valueOf(progress.getReadStatus()));
|
||||
book.setDateFinished(progress.getDateFinished());
|
||||
}
|
||||
});
|
||||
|
||||
@@ -108,6 +109,8 @@ public class BookService {
|
||||
if (progress != null) {
|
||||
setBookProgress(book, progress);
|
||||
book.setLastReadTime(progress.getLastReadTime());
|
||||
book.setReadStatus(String.valueOf(progress.getReadStatus()));
|
||||
book.setDateFinished(progress.getDateFinished());
|
||||
}
|
||||
|
||||
return book;
|
||||
@@ -277,6 +280,7 @@ public class BookService {
|
||||
}
|
||||
book.setFilePath(FileUtils.getBookFullPath(bookEntity));
|
||||
book.setReadStatus(String.valueOf(userProgress.getReadStatus()));
|
||||
book.setDateFinished(userProgress.getDateFinished());
|
||||
|
||||
if (!withDescription) {
|
||||
book.getMetadata().setDescription(null);
|
||||
@@ -307,6 +311,7 @@ public class BookService {
|
||||
progress.setEpubProgressPercent(null);
|
||||
progress.setCbxProgress(null);
|
||||
progress.setCbxProgressPercent(null);
|
||||
progress.setDateFinished(null);
|
||||
|
||||
userBookProgressRepository.save(progress);
|
||||
updatedBooks.add(bookMapper.toBook(bookEntity));
|
||||
@@ -374,7 +379,7 @@ public class BookService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateReadStatus(List<Long> bookIds, String status) {
|
||||
public List<Book> updateReadStatus(List<Long> bookIds, String status) {
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
ReadStatus readStatus = EnumUtils.getEnumIgnoreCase(ReadStatus.class, status);
|
||||
|
||||
@@ -392,8 +397,37 @@ public class BookService {
|
||||
progress.setUser(userEntity);
|
||||
progress.setBook(book);
|
||||
progress.setReadStatus(readStatus);
|
||||
|
||||
// Set dateFinished when status is READ, clear it otherwise
|
||||
if (readStatus == ReadStatus.READ) {
|
||||
progress.setDateFinished(Instant.now());
|
||||
} else {
|
||||
progress.setDateFinished(null);
|
||||
}
|
||||
|
||||
userBookProgressRepository.save(progress);
|
||||
}
|
||||
|
||||
// Return updated books with the latest data
|
||||
return books.stream()
|
||||
.map(bookEntity -> {
|
||||
Book book = bookMapper.toBook(bookEntity);
|
||||
book.setFilePath(FileUtils.getBookFullPath(bookEntity));
|
||||
|
||||
UserBookProgressEntity progress = userBookProgressRepository
|
||||
.findByUserIdAndBookId(user.getId(), bookEntity.getId())
|
||||
.orElse(null);
|
||||
|
||||
if (progress != null) {
|
||||
setBookProgress(book, progress);
|
||||
book.setLastReadTime(progress.getLastReadTime());
|
||||
book.setReadStatus(String.valueOf(progress.getReadStatus()));
|
||||
book.setDateFinished(progress.getDateFinished());
|
||||
}
|
||||
|
||||
return book;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public ResponseEntity<Resource> downloadBook(Long bookId) {
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE `user_book_progress`
|
||||
ADD COLUMN `date_finished` timestamp NULL DEFAULT NULL,
|
||||
ADD INDEX `idx_user_book_progress_date_finished` (`date_finished`);
|
||||
Reference in New Issue
Block a user