Support Individual Metadata Editing for Multiple Books

This commit is contained in:
aditya.chandel
2025-06-18 22:26:12 -06:00
committed by Aditya Chandel
parent 82c843bbe5
commit 551234f02a
14 changed files with 274 additions and 21 deletions
@@ -21,6 +21,7 @@ import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Set;
@RequestMapping("/api/v1/books")
@RestController
@@ -42,6 +43,11 @@ public class BookController {
return ResponseEntity.ok(bookService.getBook(bookId, withDescription));
}
@GetMapping("/batch")
public ResponseEntity<List<Book>> getBooksByIds(@RequestParam Set<Long> ids, @RequestParam(required = false, defaultValue = "false") boolean withDescription) {
return ResponseEntity.ok(bookService.getBooksByIds(ids, withDescription));
}
@GetMapping("/{bookId}/cover")
public ResponseEntity<Resource> getBookCover(@PathVariable long bookId) {
return ResponseEntity.ok(bookService.getBookCover(bookId));
@@ -74,6 +74,34 @@ public class BookService {
return books;
}
public List<Book> getBooksByIds(Set<Long> bookIds, boolean withDescription) {
BookLoreUser user = authenticationService.getAuthenticatedUser();
List<BookEntity> bookEntities;
bookEntities = bookQueryService.findAllWithMetadataByIds(bookIds);
Map<Long, UserBookProgressEntity> progressMap = userProgressService.fetchUserProgress(
user.getId(), bookEntities.stream().map(BookEntity::getId).collect(Collectors.toSet()));
return bookEntities.stream().map(bookEntity -> {
Book book = bookMapper.toBook(bookEntity);
book.setFilePath(FileUtils.getBookFullPath(bookEntity));
if (!withDescription) {
book.getMetadata().setDescription(null);
}
UserBookProgressEntity progress = progressMap.get(bookEntity.getId());
if (progress != null) {
setBookProgress(book, progress);
book.setLastReadTime(progress.getLastReadTime());
}
return book;
}).collect(Collectors.toList());
}
private void setBookProgress(Book book, UserBookProgressEntity progress) {
switch (book.getBookType()) {
case EPUB -> book.setEpubProgress(EpubProgress.builder()
@@ -139,7 +139,7 @@ public class BookMetadataUpdater {
.orElseGet(() -> categoryRepository.save(CategoryEntity.builder().name(categoryName).build()));
existingCategories.add(categoryEntity);
}
` }
metadata.setCategories(existingCategories);
} else if (!newMetadata.getCategories().isEmpty()) {
Set<CategoryEntity> newCategoryEntities = new HashSet<>();