Add setting/toggle to automatically download reviews (#1221)

This commit is contained in:
Aditya Chandel
2025-09-26 14:39:52 -06:00
committed by GitHub
parent a9c1be9854
commit 06523d4c24
10 changed files with 124 additions and 31 deletions
@@ -2,11 +2,13 @@ package com.adityachandel.booklore.controller;
import com.adityachandel.booklore.model.dto.BookReview;
import com.adityachandel.booklore.service.BookReviewService;
import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@@ -17,8 +19,12 @@ public class BookReviewController {
private final BookReviewService bookReviewService;
@GetMapping("/book/{bookId}")
public List<BookReview> listByBook(@PathVariable Long bookId) {
return bookReviewService.getByBookId(bookId);
public ResponseEntity<List<BookReview>> listByBook(@PathVariable @Positive(message = "Book ID must be positive") Long bookId) {
List<BookReview> reviews = bookReviewService.getByBookId(bookId);
if (reviews.isEmpty()) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(reviews);
}
@PostMapping("/book/{bookId}/refresh")
@@ -11,6 +11,7 @@ import java.util.Set;
public class MetadataPublicReviewsSettings {
private boolean downloadEnabled;
private boolean autoDownloadEnabled;
private Set<ReviewProviderConfig> providers;
@Builder
@@ -37,25 +37,40 @@ public class BookReviewService {
private final AuthenticationService authenticationService;
public List<BookReview> getByBookId(Long bookId) {
List<BookReview> reviews = bookReviewRepository.findByBookMetadataBookId(bookId).stream()
.map(mapper::toDto)
.collect(Collectors.toList());
if (!appSettingService.getAppSettings().getMetadataPublicReviewsSettings().isDownloadEnabled() || !reviews.isEmpty()) {
return reviews;
}
BookLoreUser user = authenticationService.getAuthenticatedUser();
if (!(user.getPermissions().isAdmin() || user.getPermissions().isCanManipulateLibrary())) {
return reviews;
}
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
bookReviewUpdateService.addReviewsToBook(fetchBookReviews(bookEntity), bookEntity.getMetadata());
bookRepository.save(bookEntity);
return bookReviewRepository.findByBookMetadataBookId(bookId).stream()
List<BookReview> existingReviews = bookReviewRepository.findByBookMetadataBookId(bookId).stream()
.map(mapper::toDto)
.collect(Collectors.toList());
MetadataPublicReviewsSettings reviewSettings = appSettingService.getAppSettings().getMetadataPublicReviewsSettings();
// Return existing reviews if download is disabled or reviews already exist
if (!reviewSettings.isDownloadEnabled() || !existingReviews.isEmpty()) {
return existingReviews;
}
// Check user permissions for auto-download
BookLoreUser currentUser = authenticationService.getAuthenticatedUser();
boolean hasPermission = currentUser.getPermissions().isAdmin() || currentUser.getPermissions().isCanManipulateLibrary();
if (!hasPermission || !reviewSettings.isAutoDownloadEnabled()) {
return existingReviews;
}
try {
List<BookReview> fetchedReviews = fetchBookReviews(bookEntity);
if (!fetchedReviews.isEmpty()) {
bookReviewUpdateService.addReviewsToBook(fetchedReviews, bookEntity.getMetadata());
bookRepository.save(bookEntity);
return bookReviewRepository.findByBookMetadataBookId(bookId).stream()
.map(mapper::toDto)
.collect(Collectors.toList());
}
} catch (Exception ignored) {
}
return existingReviews;
}
public List<BookReview> fetchBookReviews(BookEntity bookEntity) {
@@ -95,7 +110,7 @@ public class BookReviewService {
bookRepository.save(bookEntity);
bookReviewRepository.deleteByBookMetadataBookId(bookId);
List<BookReview> freshReviews = fetchBookReviews(bookEntity);
bookReviewUpdateService.addReviewsToBook(freshReviews, bookEntity.getMetadata());
bookRepository.save(bookEntity);
@@ -191,6 +191,7 @@ public class SettingPersistenceHelper {
public MetadataPublicReviewsSettings getDefaultMetadataPublicReviewsSettings() {
return MetadataPublicReviewsSettings.builder()
.downloadEnabled(true)
.autoDownloadEnabled(false)
.providers(Set.of(
MetadataPublicReviewsSettings.ReviewProviderConfig.builder()
.provider(MetadataProvider.Amazon)
@@ -108,6 +108,7 @@ class BookReviewServiceTest {
return MetadataPublicReviewsSettings.builder()
.downloadEnabled(enabled)
.autoDownloadEnabled(enabled)
.providers(configs)
.build();
}
@@ -115,11 +116,13 @@ class BookReviewServiceTest {
@Test
void getByBookId_returnsExistingReviews_whenReviewsExist() {
Long bookId = 1L;
BookEntity bookEntity = new BookEntity();
BookReviewEntity entity = createBookReviewEntity(MetadataProvider.Amazon);
BookReview dto = createBookReview(MetadataProvider.Amazon);
AppSettings appSettings = new AppSettings();
appSettings.setMetadataPublicReviewsSettings(createReviewSettings(true));
when(bookRepository.findById(bookId)).thenReturn(Optional.of(bookEntity));
when(bookReviewRepository.findByBookMetadataBookId(bookId))
.thenReturn(Collections.singletonList(entity));
when(mapper.toDto(entity)).thenReturn(dto);
@@ -135,9 +138,11 @@ class BookReviewServiceTest {
@Test
void getByBookId_returnsEmptyList_whenDownloadDisabled() {
Long bookId = 1L;
BookEntity bookEntity = new BookEntity();
AppSettings appSettings = new AppSettings();
appSettings.setMetadataPublicReviewsSettings(createReviewSettings(false));
when(bookRepository.findById(bookId)).thenReturn(Optional.of(bookEntity));
when(bookReviewRepository.findByBookMetadataBookId(bookId))
.thenReturn(Collections.emptyList());
when(appSettingService.getAppSettings()).thenReturn(appSettings);
@@ -151,10 +156,12 @@ class BookReviewServiceTest {
@Test
void getByBookId_returnsEmptyList_whenUserLacksPermissions() {
Long bookId = 1L;
BookEntity bookEntity = new BookEntity();
AppSettings appSettings = new AppSettings();
appSettings.setMetadataPublicReviewsSettings(createReviewSettings(true, MetadataProvider.Amazon));
BookLoreUser user = createUser(false, false);
when(bookRepository.findById(bookId)).thenReturn(Optional.of(bookEntity));
when(bookReviewRepository.findByBookMetadataBookId(bookId))
.thenReturn(Collections.emptyList());
when(appSettingService.getAppSettings()).thenReturn(appSettings);
@@ -239,14 +246,7 @@ class BookReviewServiceTest {
@Test
void getByBookId_throwsException_whenBookNotFound() {
Long bookId = 1L;
AppSettings appSettings = new AppSettings();
appSettings.setMetadataPublicReviewsSettings(createReviewSettings(true, MetadataProvider.Amazon));
BookLoreUser user = createUser(true, false);
when(bookReviewRepository.findByBookMetadataBookId(bookId))
.thenReturn(Collections.emptyList());
when(appSettingService.getAppSettings()).thenReturn(appSettings);
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
when(bookRepository.findById(bookId)).thenReturn(Optional.empty());
RuntimeException exception = assertThrows(RuntimeException.class,