Fix book browser header title overflow on mobile (#2808)
* feat(mobile-api): add book filtering and filter-options endpoint Add fileType, minRating, maxRating, authors, and language query params to GET /api/mobile/v1/books. Add GET /api/mobile/v1/filter-options endpoint returning available authors, languages, file types, and read statuses for the authenticated user's accessible libraries. * Fix book browser header title overflowing into toolbar on mobile --------- Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package org.booklore.mobile.controller;
|
||||
|
||||
import org.booklore.mobile.dto.*;
|
||||
import org.booklore.mobile.service.MobileBookService;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.model.enums.ReadStatus;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
@@ -38,10 +39,16 @@ public class MobileBookController {
|
||||
@Parameter(description = "Filter by library ID") @RequestParam(required = false) Long libraryId,
|
||||
@Parameter(description = "Filter by shelf ID") @RequestParam(required = false) Long shelfId,
|
||||
@Parameter(description = "Filter by read status") @RequestParam(required = false) ReadStatus status,
|
||||
@Parameter(description = "Search in title, author, series") @RequestParam(required = false) String search) {
|
||||
@Parameter(description = "Search in title, author, series") @RequestParam(required = false) String search,
|
||||
@Parameter(description = "Filter by file type (EBOOK, AUDIOBOOK, PDF, CBX, etc.)") @RequestParam(required = false) BookFileType fileType,
|
||||
@Parameter(description = "Minimum personal rating (1-5)") @RequestParam(required = false) Integer minRating,
|
||||
@Parameter(description = "Maximum personal rating (1-5, 0 for unrated)") @RequestParam(required = false) Integer maxRating,
|
||||
@Parameter(description = "Filter by author name") @RequestParam(required = false) String authors,
|
||||
@Parameter(description = "Filter by language code (e.g. en, fr)") @RequestParam(required = false) String language) {
|
||||
|
||||
return ResponseEntity.ok(mobileBookService.getBooks(
|
||||
page, size, sort, dir, libraryId, shelfId, status, search));
|
||||
page, size, sort, dir, libraryId, shelfId, status, search,
|
||||
fileType, minRating, maxRating, authors, language));
|
||||
}
|
||||
|
||||
@Operation(summary = "Get book details",
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.booklore.mobile.controller;
|
||||
|
||||
import org.booklore.mobile.dto.MobileFilterOptions;
|
||||
import org.booklore.mobile.service.MobileBookService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@AllArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/api/mobile/v1")
|
||||
@Tag(name = "Mobile Filters", description = "Filter options for mobile book browsing")
|
||||
public class MobileFilterController {
|
||||
|
||||
private final MobileBookService mobileBookService;
|
||||
|
||||
@Operation(summary = "Get available filter options",
|
||||
description = "Returns available authors, languages, file types, and read statuses for filtering books.")
|
||||
@ApiResponse(responseCode = "200", description = "Filter options retrieved successfully")
|
||||
@GetMapping("/filter-options")
|
||||
public ResponseEntity<MobileFilterOptions> getFilterOptions() {
|
||||
return ResponseEntity.ok(mobileBookService.getFilterOptions());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.booklore.mobile.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class MobileFilterOptions {
|
||||
private List<AuthorOption> authors;
|
||||
private List<LanguageOption> languages;
|
||||
private List<String> readStatuses;
|
||||
private List<String> fileTypes;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class AuthorOption {
|
||||
private String name;
|
||||
private long count;
|
||||
}
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class LanguageOption {
|
||||
private String code;
|
||||
private String label;
|
||||
private long count;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import org.booklore.config.security.service.AuthenticationService;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.mobile.dto.MobileBookDetail;
|
||||
import org.booklore.mobile.dto.MobileBookSummary;
|
||||
import org.booklore.mobile.dto.MobileFilterOptions;
|
||||
import org.booklore.mobile.dto.MobilePageResponse;
|
||||
import org.booklore.mobile.mapper.MobileBookMapper;
|
||||
import org.booklore.mobile.specification.MobileBookSpecification;
|
||||
@@ -11,6 +12,7 @@ import org.booklore.model.dto.Book;
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.Library;
|
||||
import org.booklore.model.entity.*;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.model.enums.ReadStatus;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.ShelfRepository;
|
||||
@@ -26,6 +28,8 @@ import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.Tuple;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
@@ -45,6 +49,7 @@ public class MobileBookService {
|
||||
private final AuthenticationService authenticationService;
|
||||
private final MobileBookMapper mobileBookMapper;
|
||||
private final MagicShelfBookService magicShelfBookService;
|
||||
private final EntityManager entityManager;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public MobilePageResponse<MobileBookSummary> getBooks(
|
||||
@@ -55,7 +60,12 @@ public class MobileBookService {
|
||||
Long libraryId,
|
||||
Long shelfId,
|
||||
ReadStatus status,
|
||||
String search) {
|
||||
String search,
|
||||
BookFileType fileType,
|
||||
Integer minRating,
|
||||
Integer maxRating,
|
||||
String authors,
|
||||
String language) {
|
||||
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
Long userId = user.getId();
|
||||
@@ -68,7 +78,8 @@ public class MobileBookService {
|
||||
Pageable pageable = PageRequest.of(pageNum, pageSize, sort);
|
||||
|
||||
Specification<BookEntity> spec = buildSpecification(
|
||||
accessibleLibraryIds, libraryId, shelfId, status, search, userId);
|
||||
accessibleLibraryIds, libraryId, shelfId, status, search, userId,
|
||||
fileType, minRating, maxRating, authors, language);
|
||||
|
||||
Page<BookEntity> bookPage = bookRepository.findAll(spec, pageable);
|
||||
|
||||
@@ -293,6 +304,87 @@ public class MobileBookService {
|
||||
return MobilePageResponse.of(summaries, pageNum, pageSize, booksPage.getTotalElements());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public MobileFilterOptions getFilterOptions() {
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
Set<Long> accessibleLibraryIds = getAccessibleLibraryIds(user);
|
||||
|
||||
// Base filter: non-deleted digital books in accessible libraries
|
||||
String libraryClause = accessibleLibraryIds != null
|
||||
? "AND b.library.id IN :libraryIds"
|
||||
: "";
|
||||
|
||||
// Authors with book count (top 200 by count)
|
||||
String authorQuery = """
|
||||
SELECT a.name, COUNT(DISTINCT b.id) FROM BookEntity b
|
||||
JOIN b.metadata m JOIN m.authors a
|
||||
WHERE (b.deleted IS NULL OR b.deleted = false)
|
||||
AND (b.isPhysical IS NULL OR b.isPhysical = false)
|
||||
""" + libraryClause + """
|
||||
GROUP BY a.name ORDER BY COUNT(DISTINCT b.id) DESC
|
||||
""";
|
||||
var authorQ = entityManager.createQuery(authorQuery, Tuple.class);
|
||||
if (accessibleLibraryIds != null) authorQ.setParameter("libraryIds", accessibleLibraryIds);
|
||||
authorQ.setMaxResults(200);
|
||||
|
||||
List<MobileFilterOptions.AuthorOption> authors = authorQ.getResultList().stream()
|
||||
.map(t -> MobileFilterOptions.AuthorOption.builder()
|
||||
.name(t.get(0, String.class))
|
||||
.count(t.get(1, Long.class))
|
||||
.build())
|
||||
.toList();
|
||||
|
||||
// Languages with book count
|
||||
String langQuery = """
|
||||
SELECT m.language, COUNT(DISTINCT b.id) FROM BookEntity b
|
||||
JOIN b.metadata m
|
||||
WHERE (b.deleted IS NULL OR b.deleted = false)
|
||||
AND (b.isPhysical IS NULL OR b.isPhysical = false)
|
||||
AND m.language IS NOT NULL AND m.language <> ''
|
||||
""" + libraryClause + """
|
||||
GROUP BY m.language ORDER BY COUNT(DISTINCT b.id) DESC
|
||||
""";
|
||||
var langQ = entityManager.createQuery(langQuery, Tuple.class);
|
||||
if (accessibleLibraryIds != null) langQ.setParameter("libraryIds", accessibleLibraryIds);
|
||||
|
||||
List<MobileFilterOptions.LanguageOption> languages = langQ.getResultList().stream()
|
||||
.map(t -> MobileFilterOptions.LanguageOption.builder()
|
||||
.code(t.get(0, String.class))
|
||||
.label(Locale.forLanguageTag(t.get(0, String.class)).getDisplayLanguage(Locale.ENGLISH))
|
||||
.count(t.get(1, Long.class))
|
||||
.build())
|
||||
.toList();
|
||||
|
||||
// Distinct file types present in accessible books
|
||||
String fileTypeQuery = """
|
||||
SELECT DISTINCT bf.bookType FROM BookEntity b
|
||||
JOIN b.bookFiles bf
|
||||
WHERE (b.deleted IS NULL OR b.deleted = false)
|
||||
AND (b.isPhysical IS NULL OR b.isPhysical = false)
|
||||
AND bf.isBookFormat = true
|
||||
""" + libraryClause;
|
||||
var ftQ = entityManager.createQuery(fileTypeQuery, BookFileType.class);
|
||||
if (accessibleLibraryIds != null) ftQ.setParameter("libraryIds", accessibleLibraryIds);
|
||||
|
||||
List<String> fileTypes = ftQ.getResultList().stream()
|
||||
.map(Enum::name)
|
||||
.sorted()
|
||||
.toList();
|
||||
|
||||
// Read statuses — return all meaningful values
|
||||
List<String> readStatuses = Arrays.stream(ReadStatus.values())
|
||||
.filter(s -> s != ReadStatus.UNSET)
|
||||
.map(Enum::name)
|
||||
.toList();
|
||||
|
||||
return MobileFilterOptions.builder()
|
||||
.authors(authors)
|
||||
.languages(languages)
|
||||
.fileTypes(fileTypes)
|
||||
.readStatuses(readStatuses)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateReadStatus(Long bookId, ReadStatus status) {
|
||||
UserBookProgressEntity progress = validateAccessAndGetProgress(bookId);
|
||||
@@ -372,7 +464,12 @@ public class MobileBookService {
|
||||
Long shelfId,
|
||||
ReadStatus status,
|
||||
String search,
|
||||
Long userId) {
|
||||
Long userId,
|
||||
BookFileType fileType,
|
||||
Integer minRating,
|
||||
Integer maxRating,
|
||||
String authors,
|
||||
String language) {
|
||||
|
||||
List<Specification<BookEntity>> specs = new ArrayList<>();
|
||||
specs.add(MobileBookSpecification.notDeleted());
|
||||
@@ -407,6 +504,26 @@ public class MobileBookService {
|
||||
specs.add(MobileBookSpecification.searchText(search));
|
||||
}
|
||||
|
||||
if (fileType != null) {
|
||||
specs.add(MobileBookSpecification.withFileType(fileType));
|
||||
}
|
||||
|
||||
if (minRating != null) {
|
||||
specs.add(MobileBookSpecification.withMinRating(minRating, userId));
|
||||
}
|
||||
|
||||
if (maxRating != null) {
|
||||
specs.add(MobileBookSpecification.withMaxRating(maxRating, userId));
|
||||
}
|
||||
|
||||
if (authors != null && !authors.trim().isEmpty()) {
|
||||
specs.add(MobileBookSpecification.withAuthor(authors.trim()));
|
||||
}
|
||||
|
||||
if (language != null && !language.trim().isEmpty()) {
|
||||
specs.add(MobileBookSpecification.withLanguage(language.trim()));
|
||||
}
|
||||
|
||||
return MobileBookSpecification.combine(specs.toArray(new Specification[0]));
|
||||
}
|
||||
|
||||
|
||||
+90
@@ -139,6 +139,96 @@ public class MobileBookSpecification {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter books that have at least one file of the given type.
|
||||
*/
|
||||
public static Specification<BookEntity> withFileType(BookFileType fileType) {
|
||||
return (root, query, cb) -> {
|
||||
if (fileType == null) {
|
||||
return cb.conjunction();
|
||||
}
|
||||
Subquery<Long> subquery = query.subquery(Long.class);
|
||||
Root<BookFileEntity> bookFileRoot = subquery.from(BookFileEntity.class);
|
||||
subquery.select(bookFileRoot.get("book").get("id"))
|
||||
.where(cb.equal(bookFileRoot.get("bookType"), fileType));
|
||||
return root.get("id").in(subquery);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter books where the user's personal rating is >= minRating.
|
||||
*/
|
||||
public static Specification<BookEntity> withMinRating(int minRating, Long userId) {
|
||||
return (root, query, cb) -> {
|
||||
Subquery<Long> subquery = query.subquery(Long.class);
|
||||
Root<UserBookProgressEntity> progressRoot = subquery.from(UserBookProgressEntity.class);
|
||||
subquery.select(progressRoot.get("book").get("id"))
|
||||
.where(
|
||||
cb.equal(progressRoot.get("user").get("id"), userId),
|
||||
cb.greaterThanOrEqualTo(progressRoot.get("personalRating"), minRating)
|
||||
);
|
||||
return root.get("id").in(subquery);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter books where the user's personal rating is <= maxRating.
|
||||
* Use maxRating=0 to find unrated books.
|
||||
*/
|
||||
public static Specification<BookEntity> withMaxRating(int maxRating, Long userId) {
|
||||
return (root, query, cb) -> {
|
||||
Subquery<Long> subquery = query.subquery(Long.class);
|
||||
Root<UserBookProgressEntity> progressRoot = subquery.from(UserBookProgressEntity.class);
|
||||
|
||||
if (maxRating == 0) {
|
||||
// Unrated: books with no progress entry or null personalRating
|
||||
Subquery<Long> ratedSubquery = query.subquery(Long.class);
|
||||
Root<UserBookProgressEntity> ratedRoot = ratedSubquery.from(UserBookProgressEntity.class);
|
||||
ratedSubquery.select(ratedRoot.get("book").get("id"))
|
||||
.where(
|
||||
cb.equal(ratedRoot.get("user").get("id"), userId),
|
||||
cb.isNotNull(ratedRoot.get("personalRating"))
|
||||
);
|
||||
return cb.not(root.get("id").in(ratedSubquery));
|
||||
}
|
||||
|
||||
subquery.select(progressRoot.get("book").get("id"))
|
||||
.where(
|
||||
cb.equal(progressRoot.get("user").get("id"), userId),
|
||||
cb.lessThanOrEqualTo(progressRoot.get("personalRating"), maxRating)
|
||||
);
|
||||
return root.get("id").in(subquery);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter books by author name (case-insensitive exact match).
|
||||
*/
|
||||
public static Specification<BookEntity> withAuthor(String authorName) {
|
||||
return (root, query, cb) -> {
|
||||
if (authorName == null || authorName.trim().isEmpty()) {
|
||||
return cb.conjunction();
|
||||
}
|
||||
Join<BookEntity, BookMetadataEntity> metadataJoin = root.join("metadata", JoinType.LEFT);
|
||||
Join<BookMetadataEntity, AuthorEntity> authorsJoin = metadataJoin.join("authors", JoinType.LEFT);
|
||||
query.distinct(true);
|
||||
return cb.equal(cb.lower(authorsJoin.get("name")), authorName.toLowerCase().trim());
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter books by language code (case-insensitive).
|
||||
*/
|
||||
public static Specification<BookEntity> withLanguage(String language) {
|
||||
return (root, query, cb) -> {
|
||||
if (language == null || language.trim().isEmpty()) {
|
||||
return cb.conjunction();
|
||||
}
|
||||
Join<BookEntity, BookMetadataEntity> metadataJoin = root.join("metadata", JoinType.LEFT);
|
||||
return cb.equal(cb.lower(metadataJoin.get("language")), language.toLowerCase().trim());
|
||||
};
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static Specification<BookEntity> combine(Specification<BookEntity>... specs) {
|
||||
Specification<BookEntity> result = (root, query, cb) -> cb.conjunction();
|
||||
|
||||
+13
-7
@@ -17,7 +17,7 @@
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
width: 100%;
|
||||
gap: 1rem;
|
||||
gap: 0.5rem;
|
||||
padding: 0.5rem 0.5rem;
|
||||
border-radius: 0.75rem 0.75rem 0 0;
|
||||
margin-bottom: 0.5rem;
|
||||
@@ -25,6 +25,7 @@
|
||||
border: 1px solid var(--p-content-border-color);
|
||||
|
||||
@media (min-width: 768px) {
|
||||
gap: 1rem;
|
||||
padding: 0.5rem 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
@@ -35,14 +36,16 @@
|
||||
display: flex;
|
||||
align-items: center;
|
||||
min-width: 0;
|
||||
max-width: 100%;
|
||||
flex-shrink: 1;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.entity-info {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.125rem;
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
|
||||
@media (max-width: 767px) {
|
||||
gap: 0;
|
||||
@@ -54,7 +57,6 @@
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
width: 100%;
|
||||
margin: 0;
|
||||
|
||||
@media (max-width: 767px) {
|
||||
@@ -83,17 +85,21 @@
|
||||
// Toolbar actions
|
||||
.toolbar-actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
flex-wrap: nowrap;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
gap: 0.5rem;
|
||||
flex-shrink: 0;
|
||||
|
||||
@media (min-width: 768px) {
|
||||
gap: 1rem;
|
||||
}
|
||||
}
|
||||
|
||||
.toolbar-icons {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
flex-wrap: nowrap;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
gap: 0.5rem;
|
||||
|
||||
@media (min-width: 768px) {
|
||||
gap: 1.5rem;
|
||||
|
||||
Reference in New Issue
Block a user