feat: add reading progress endpoints to mobile api (#377)

* feat: add reading progress endpoints to mobile api

* fix(app-api): replace ReadProgressRequest with app-specific DTO for progress endpoint

* fix(app-api): missing BookService dependecy for constructor
This commit is contained in:
Alex Fair
2026-04-22 17:27:14 -04:00
committed by Zach Yale
parent 39de8e31ac
commit 4bca3f6674
7 changed files with 335 additions and 1 deletions
@@ -43,6 +43,22 @@ public class AppBookController {
return ResponseEntity.ok(mobileBookService.getBookDetail(bookId)); return ResponseEntity.ok(mobileBookService.getBookDetail(bookId));
} }
@GetMapping("/{bookId}/progress")
public ResponseEntity<AppBookProgressResponse> getBookProgress(
@PathVariable Long bookId) {
return ResponseEntity.ok(mobileBookService.getBookProgress(bookId));
}
@PutMapping("/{bookId}/progress")
public ResponseEntity<Void> updateBookProgress(
@PathVariable Long bookId,
@Valid @RequestBody UpdateProgressRequest request) {
mobileBookService.updateBookProgress(bookId, request);
return ResponseEntity.ok().build();
}
@Operation( @Operation(
summary = "Search app books", summary = "Search app books",
description = "Search books in the app catalog using a free-text query.", description = "Search books in the app catalog using a free-text query.",
@@ -0,0 +1,25 @@
package org.booklore.app.dto;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.Instant;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AppBookProgressResponse {
private Float readProgress;
private String readStatus;
private Instant lastReadTime;
private AppBookDetail.EpubProgress epubProgress;
private AppBookDetail.PdfProgress pdfProgress;
private AppBookDetail.CbxProgress cbxProgress;
private AppBookDetail.AudiobookProgress audiobookProgress;
private AppBookDetail.KoreaderProgress koreaderProgress;
}
@@ -0,0 +1,33 @@
package org.booklore.app.dto;
import jakarta.validation.constraints.AssertTrue;
import lombok.Data;
import org.booklore.model.dto.progress.AudiobookProgress;
import org.booklore.model.dto.progress.CbxProgress;
import org.booklore.model.dto.progress.EpubProgress;
import org.booklore.model.dto.progress.PdfProgress;
import org.booklore.model.dto.request.BookFileProgress;
import java.time.Instant;
@Data
public class UpdateProgressRequest {
private BookFileProgress fileProgress;
@Deprecated
private EpubProgress epubProgress;
@Deprecated
private PdfProgress pdfProgress;
@Deprecated
private CbxProgress cbxProgress;
@Deprecated
private AudiobookProgress audiobookProgress;
private Instant dateFinished;
@AssertTrue(message = "At least one progress field must be provided")
public boolean isProgressValid() {
return fileProgress != null || epubProgress != null || pdfProgress != null || cbxProgress != null || audiobookProgress != null || dateFinished != null;
}
}
@@ -2,6 +2,7 @@ package org.booklore.app.mapper;
import org.booklore.app.dto.AppBookDetail; import org.booklore.app.dto.AppBookDetail;
import org.booklore.app.dto.AppBookFile; import org.booklore.app.dto.AppBookFile;
import org.booklore.app.dto.AppBookProgressResponse;
import org.booklore.app.dto.AppBookSummary; import org.booklore.app.dto.AppBookSummary;
import org.booklore.app.dto.AppLibrarySummary; import org.booklore.app.dto.AppLibrarySummary;
import org.booklore.app.dto.AppMagicShelfSummary; import org.booklore.app.dto.AppMagicShelfSummary;
@@ -73,6 +74,20 @@ public interface AppBookMapper {
@Mapping(target = "koreaderProgress", source = "progress", qualifiedByName = "mapKoreaderProgress") @Mapping(target = "koreaderProgress", source = "progress", qualifiedByName = "mapKoreaderProgress")
AppBookDetail toDetail(BookEntity book, UserBookProgressEntity progress, UserBookFileProgressEntity fileProgress); AppBookDetail toDetail(BookEntity book, UserBookProgressEntity progress, UserBookFileProgressEntity fileProgress);
default AppBookProgressResponse toProgressResponse(UserBookProgressEntity progress, UserBookFileProgressEntity fileProgress) {
return AppBookProgressResponse.builder()
.readProgress(mapReadProgress(progress))
.readStatus(progress != null && progress.getReadStatus() != null
? progress.getReadStatus().name() : null)
.lastReadTime(progress != null ? progress.getLastReadTime() : null)
.epubProgress(mapEpubProgress(progress))
.pdfProgress(mapPdfProgress(progress))
.cbxProgress(mapCbxProgress(progress))
.audiobookProgress(mapAudiobookProgress(fileProgress))
.koreaderProgress(mapKoreaderProgress(progress))
.build();
}
@Named("mapAuthors") @Named("mapAuthors")
default List<String> mapAuthors(List<AuthorEntity> authors) { default List<String> mapAuthors(List<AuthorEntity> authors) {
if (authors == null || authors.isEmpty()) { if (authors == null || authors.isEmpty()) {
@@ -6,15 +6,18 @@ import jakarta.persistence.criteria.Root;
import org.booklore.config.security.service.AuthenticationService; import org.booklore.config.security.service.AuthenticationService;
import org.booklore.exception.ApiError; import org.booklore.exception.ApiError;
import org.booklore.app.dto.AppBookDetail; import org.booklore.app.dto.AppBookDetail;
import org.booklore.app.dto.AppBookProgressResponse;
import org.booklore.app.dto.AppBookSummary; import org.booklore.app.dto.AppBookSummary;
import org.booklore.app.dto.AppFilterOptions; import org.booklore.app.dto.AppFilterOptions;
import org.booklore.app.dto.AppPageResponse; import org.booklore.app.dto.AppPageResponse;
import org.booklore.app.dto.UpdateProgressRequest;
import org.booklore.app.dto.BookListRequest; import org.booklore.app.dto.BookListRequest;
import org.booklore.app.mapper.AppBookMapper; import org.booklore.app.mapper.AppBookMapper;
import org.booklore.app.specification.AppBookSpecification; import org.booklore.app.specification.AppBookSpecification;
import org.booklore.model.dto.Book; import org.booklore.model.dto.Book;
import org.booklore.model.dto.BookLoreUser; import org.booklore.model.dto.BookLoreUser;
import org.booklore.model.dto.Library; import org.booklore.model.dto.Library;
import org.booklore.model.dto.request.ReadProgressRequest;
import org.booklore.model.entity.*; import org.booklore.model.entity.*;
import org.booklore.model.enums.BookFileType; import org.booklore.model.enums.BookFileType;
import org.booklore.model.enums.ReadStatus; import org.booklore.model.enums.ReadStatus;
@@ -22,6 +25,7 @@ import org.booklore.repository.BookRepository;
import org.booklore.repository.ShelfRepository; import org.booklore.repository.ShelfRepository;
import org.booklore.repository.UserBookFileProgressRepository; import org.booklore.repository.UserBookFileProgressRepository;
import org.booklore.repository.UserBookProgressRepository; import org.booklore.repository.UserBookProgressRepository;
import org.booklore.service.book.BookService;
import org.booklore.service.opds.MagicShelfBookService; import org.booklore.service.opds.MagicShelfBookService;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
@@ -55,6 +59,7 @@ public class AppBookService {
private final ShelfRepository shelfRepository; private final ShelfRepository shelfRepository;
private final AuthenticationService authenticationService; private final AuthenticationService authenticationService;
private final AppBookMapper mobileBookMapper; private final AppBookMapper mobileBookMapper;
private final BookService bookService;
private final MagicShelfBookService magicShelfBookService; private final MagicShelfBookService magicShelfBookService;
private final EntityManager entityManager; private final EntityManager entityManager;
@@ -69,6 +74,7 @@ public class AppBookService {
ShelfRepository shelfRepository, ShelfRepository shelfRepository,
AuthenticationService authenticationService, AuthenticationService authenticationService,
AppBookMapper mobileBookMapper, AppBookMapper mobileBookMapper,
BookService bookService,
MagicShelfBookService magicShelfBookService, MagicShelfBookService magicShelfBookService,
EntityManager entityManager) { EntityManager entityManager) {
this.bookRepository = bookRepository; this.bookRepository = bookRepository;
@@ -77,6 +83,7 @@ public class AppBookService {
this.shelfRepository = shelfRepository; this.shelfRepository = shelfRepository;
this.authenticationService = authenticationService; this.authenticationService = authenticationService;
this.mobileBookMapper = mobileBookMapper; this.mobileBookMapper = mobileBookMapper;
this.bookService = bookService;
this.magicShelfBookService = magicShelfBookService; this.magicShelfBookService = magicShelfBookService;
this.entityManager = entityManager; this.entityManager = entityManager;
} }
@@ -144,6 +151,53 @@ public class AppBookService {
return mobileBookMapper.toDetail(book, progress, fileProgress); return mobileBookMapper.toDetail(book, progress, fileProgress);
} }
@Transactional(readOnly = true)
public AppBookProgressResponse getBookProgress(Long bookId) {
BookLoreUser user = authenticationService.getAuthenticatedUser();
Long userId = user.getId();
Set<Long> accessibleLibraryIds = getAccessibleLibraryIds(user);
BookEntity book = bookRepository.findByIdWithBookFiles(bookId)
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
if (accessibleLibraryIds != null && !accessibleLibraryIds.contains(book.getLibrary().getId())) {
throw ApiError.FORBIDDEN.createException("Access denied to this book");
}
UserBookProgressEntity progress = userBookProgressRepository
.findByUserIdAndBookId(userId, bookId)
.orElse(null);
UserBookFileProgressEntity fileProgress = userBookFileProgressRepository
.findMostRecentAudiobookProgressByUserIdAndBookId(userId, bookId)
.orElse(null);
return mobileBookMapper.toProgressResponse(progress, fileProgress);
}
@Transactional
public void updateBookProgress(Long bookId, UpdateProgressRequest request) {
BookLoreUser user = authenticationService.getAuthenticatedUser();
Set<Long> accessibleLibraryIds = getAccessibleLibraryIds(user);
BookEntity book = bookRepository.findByIdWithBookFiles(bookId)
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
validateLibraryAccess(accessibleLibraryIds, book.getLibrary().getId());
ReadProgressRequest progressRequest = new ReadProgressRequest();
progressRequest.setBookId(bookId);
progressRequest.setFileProgress(request.getFileProgress());
progressRequest.setEpubProgress(request.getEpubProgress());
progressRequest.setPdfProgress(request.getPdfProgress());
progressRequest.setCbxProgress(request.getCbxProgress());
progressRequest.setAudiobookProgress(request.getAudiobookProgress());
progressRequest.setDateFinished(request.getDateFinished());
bookService.updateReadProgress(progressRequest);
}
@Transactional(readOnly = true)
public AppPageResponse<AppBookSummary> searchBooks( public AppPageResponse<AppBookSummary> searchBooks(
String query, String query,
Integer page, Integer page,
@@ -16,6 +16,7 @@ import org.booklore.repository.BookRepository;
import org.booklore.repository.ShelfRepository; import org.booklore.repository.ShelfRepository;
import org.booklore.repository.UserBookFileProgressRepository; import org.booklore.repository.UserBookFileProgressRepository;
import org.booklore.repository.UserBookProgressRepository; import org.booklore.repository.UserBookProgressRepository;
import org.booklore.service.book.BookService;
import org.booklore.service.opds.MagicShelfBookService; import org.booklore.service.opds.MagicShelfBookService;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -49,6 +50,7 @@ class AppBookServiceFilterOptionsTest {
@Mock private ShelfRepository shelfRepository; @Mock private ShelfRepository shelfRepository;
@Mock private AuthenticationService authenticationService; @Mock private AuthenticationService authenticationService;
@Mock private AppBookMapper mobileBookMapper; @Mock private AppBookMapper mobileBookMapper;
@Mock private BookService bookService;
@Mock private MagicShelfBookService magicShelfBookService; @Mock private MagicShelfBookService magicShelfBookService;
@Mock private EntityManager entityManager; @Mock private EntityManager entityManager;
@@ -61,7 +63,7 @@ class AppBookServiceFilterOptionsTest {
service = new AppBookService( service = new AppBookService(
bookRepository, userBookProgressRepository, userBookFileProgressRepository, bookRepository, userBookProgressRepository, userBookFileProgressRepository,
shelfRepository, authenticationService, mobileBookMapper, shelfRepository, authenticationService, mobileBookMapper,
magicShelfBookService, entityManager bookService, magicShelfBookService, entityManager
); );
} }
@@ -0,0 +1,189 @@
package org.booklore.app.service;
import jakarta.persistence.EntityManager;
import org.booklore.app.dto.UpdateProgressRequest;
import org.booklore.app.mapper.AppBookMapper;
import org.booklore.config.security.service.AuthenticationService;
import org.booklore.exception.APIException;
import org.booklore.model.dto.BookLoreUser;
import org.booklore.model.dto.Library;
import org.booklore.model.dto.request.BookFileProgress;
import org.booklore.model.dto.request.ReadProgressRequest;
import org.booklore.model.entity.BookEntity;
import org.booklore.model.entity.LibraryEntity;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.booklore.repository.BookRepository;
import org.booklore.repository.ShelfRepository;
import org.booklore.repository.UserBookFileProgressRepository;
import org.booklore.repository.UserBookProgressRepository;
import org.booklore.service.book.BookService;
import org.booklore.service.opds.MagicShelfBookService;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class AppBookServiceProgressTest {
@Mock private BookRepository bookRepository;
@Mock private UserBookProgressRepository userBookProgressRepository;
@Mock private UserBookFileProgressRepository userBookFileProgressRepository;
@Mock private ShelfRepository shelfRepository;
@Mock private AuthenticationService authenticationService;
@Mock private AppBookMapper mobileBookMapper;
@Mock private BookService bookService;
@Mock private MagicShelfBookService magicShelfBookService;
@Mock private EntityManager entityManager;
private AppBookService service;
private final Long userId = 1L;
private final Long bookId = 42L;
private final Long libraryId = 5L;
@BeforeEach
void setUp() {
service = new AppBookService(
bookRepository, userBookProgressRepository, userBookFileProgressRepository,
shelfRepository, authenticationService, mobileBookMapper,
bookService, magicShelfBookService, entityManager
);
}
// -------------------------------------------------------------------------
// updateBookProgress — success
// -------------------------------------------------------------------------
@Test
void updateBookProgress_success_delegatesToBookServiceWithPathBookId() {
mockAdminUser();
mockBookWithLibrary(bookId, libraryId);
UpdateProgressRequest request = new UpdateProgressRequest();
request.setFileProgress(new BookFileProgress(1L, "pos", "href", 0.5f, null));
service.updateBookProgress(bookId, request);
ArgumentCaptor<ReadProgressRequest> captor = ArgumentCaptor.forClass(ReadProgressRequest.class);
verify(bookService).updateReadProgress(captor.capture());
ReadProgressRequest delegated = captor.getValue();
assertEquals(bookId, delegated.getBookId());
assertNotNull(delegated.getFileProgress());
assertEquals(0.5f, delegated.getFileProgress().progressPercent());
}
@Test
void updateBookProgress_success_mapsAllFieldsFromAppRequest() {
mockAdminUser();
mockBookWithLibrary(bookId, libraryId);
UpdateProgressRequest request = new UpdateProgressRequest();
request.setFileProgress(new BookFileProgress(1L, null, null, 0.75f, null));
service.updateBookProgress(bookId, request);
ArgumentCaptor<ReadProgressRequest> captor = ArgumentCaptor.forClass(ReadProgressRequest.class);
verify(bookService).updateReadProgress(captor.capture());
ReadProgressRequest delegated = captor.getValue();
assertEquals(bookId, delegated.getBookId());
assertEquals(0.75f, delegated.getFileProgress().progressPercent());
assertNull(delegated.getEpubProgress());
assertNull(delegated.getPdfProgress());
assertNull(delegated.getCbxProgress());
assertNull(delegated.getAudiobookProgress());
}
// -------------------------------------------------------------------------
// updateBookProgress — forbidden access
// -------------------------------------------------------------------------
@Test
void updateBookProgress_nonAdminWithoutAccess_throwsForbidden() {
mockNonAdminUser(Set.of(99L));
mockBookWithLibrary(bookId, libraryId);
UpdateProgressRequest request = new UpdateProgressRequest();
request.setFileProgress(new BookFileProgress(1L, null, null, 0.5f, null));
assertThrows(APIException.class, () -> service.updateBookProgress(bookId, request));
verify(bookService, never()).updateReadProgress(any());
}
@Test
void updateBookProgress_nonAdminWithAccess_succeeds() {
mockNonAdminUser(Set.of(libraryId));
mockBookWithLibrary(bookId, libraryId);
UpdateProgressRequest request = new UpdateProgressRequest();
request.setFileProgress(new BookFileProgress(1L, null, null, 0.5f, null));
service.updateBookProgress(bookId, request);
verify(bookService).updateReadProgress(any(ReadProgressRequest.class));
}
// -------------------------------------------------------------------------
// updateBookProgress — book not found
// -------------------------------------------------------------------------
@Test
void updateBookProgress_bookNotFound_throwsException() {
mockAdminUser();
when(bookRepository.findByIdWithBookFiles(bookId)).thenReturn(Optional.empty());
UpdateProgressRequest request = new UpdateProgressRequest();
request.setFileProgress(new BookFileProgress(1L, null, null, 0.5f, null));
assertThrows(APIException.class, () -> service.updateBookProgress(bookId, request));
verify(bookService, never()).updateReadProgress(any());
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
private void mockAdminUser() {
var permissions = new BookLoreUser.UserPermissions();
permissions.setAdmin(true);
BookLoreUser user = BookLoreUser.builder()
.id(userId)
.permissions(permissions)
.build();
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
}
private void mockNonAdminUser(Set<Long> libraryIds) {
List<Library> assignedLibraries = libraryIds.stream()
.map(id -> Library.builder().id(id).build())
.toList();
var permissions = new BookLoreUser.UserPermissions();
permissions.setAdmin(false);
BookLoreUser user = BookLoreUser.builder()
.id(userId)
.permissions(permissions)
.assignedLibraries(assignedLibraries)
.build();
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
}
private void mockBookWithLibrary(Long bookId, Long libraryId) {
LibraryEntity library = LibraryEntity.builder().id(libraryId).build();
BookEntity book = BookEntity.builder().id(bookId).library(library).build();
when(bookRepository.findByIdWithBookFiles(bookId)).thenReturn(Optional.of(book));
}
}