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:
@@ -43,6 +43,22 @@ public class AppBookController {
|
||||
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(
|
||||
summary = "Search app books",
|
||||
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.AppBookFile;
|
||||
import org.booklore.app.dto.AppBookProgressResponse;
|
||||
import org.booklore.app.dto.AppBookSummary;
|
||||
import org.booklore.app.dto.AppLibrarySummary;
|
||||
import org.booklore.app.dto.AppMagicShelfSummary;
|
||||
@@ -73,6 +74,20 @@ public interface AppBookMapper {
|
||||
@Mapping(target = "koreaderProgress", source = "progress", qualifiedByName = "mapKoreaderProgress")
|
||||
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")
|
||||
default List<String> mapAuthors(List<AuthorEntity> authors) {
|
||||
if (authors == null || authors.isEmpty()) {
|
||||
|
||||
@@ -6,15 +6,18 @@ import jakarta.persistence.criteria.Root;
|
||||
import org.booklore.config.security.service.AuthenticationService;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.app.dto.AppBookDetail;
|
||||
import org.booklore.app.dto.AppBookProgressResponse;
|
||||
import org.booklore.app.dto.AppBookSummary;
|
||||
import org.booklore.app.dto.AppFilterOptions;
|
||||
import org.booklore.app.dto.AppPageResponse;
|
||||
import org.booklore.app.dto.UpdateProgressRequest;
|
||||
import org.booklore.app.dto.BookListRequest;
|
||||
import org.booklore.app.mapper.AppBookMapper;
|
||||
import org.booklore.app.specification.AppBookSpecification;
|
||||
import org.booklore.model.dto.Book;
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.Library;
|
||||
import org.booklore.model.dto.request.ReadProgressRequest;
|
||||
import org.booklore.model.entity.*;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.model.enums.ReadStatus;
|
||||
@@ -22,6 +25,7 @@ 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 org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
@@ -55,6 +59,7 @@ public class AppBookService {
|
||||
private final ShelfRepository shelfRepository;
|
||||
private final AuthenticationService authenticationService;
|
||||
private final AppBookMapper mobileBookMapper;
|
||||
private final BookService bookService;
|
||||
private final MagicShelfBookService magicShelfBookService;
|
||||
private final EntityManager entityManager;
|
||||
|
||||
@@ -69,6 +74,7 @@ public class AppBookService {
|
||||
ShelfRepository shelfRepository,
|
||||
AuthenticationService authenticationService,
|
||||
AppBookMapper mobileBookMapper,
|
||||
BookService bookService,
|
||||
MagicShelfBookService magicShelfBookService,
|
||||
EntityManager entityManager) {
|
||||
this.bookRepository = bookRepository;
|
||||
@@ -77,6 +83,7 @@ public class AppBookService {
|
||||
this.shelfRepository = shelfRepository;
|
||||
this.authenticationService = authenticationService;
|
||||
this.mobileBookMapper = mobileBookMapper;
|
||||
this.bookService = bookService;
|
||||
this.magicShelfBookService = magicShelfBookService;
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
@@ -144,6 +151,53 @@ public class AppBookService {
|
||||
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(
|
||||
String query,
|
||||
Integer page,
|
||||
|
||||
Reference in New Issue
Block a user