feat(api): add book sorts for author name / author sort name (#2497)
This commit is contained in:
@@ -55,8 +55,21 @@ public class BookQueryService {
|
||||
return findBooksPaged(visibleBooks(libraryIds, userId), pageable, userId);
|
||||
}
|
||||
|
||||
private Specification<BookEntity> distinct(Specification<BookEntity> filter) {
|
||||
return (root, query, cb) -> {
|
||||
// We need to query distinct because otherwise the `JOIN` + `LIMIT` cause the page sizes to be wrong.
|
||||
//
|
||||
// I've tried a few ways to just get only what we need - distinct IDs from the repository.
|
||||
// With Projections, we ran into issues with the order by not getting applied. I'm sure
|
||||
// there's a better way but right now this works well enough, even though it over-fetches.
|
||||
|
||||
query.distinct(true);
|
||||
return filter.toPredicate(root, query, cb);
|
||||
};
|
||||
}
|
||||
|
||||
public Page<Book> findBooksPaged(Specification<BookEntity> spec, Pageable pageable, Long userId) {
|
||||
Page<BookEntity> page = bookRepository.findAll(spec, pageable);
|
||||
Page<BookEntity> page = bookRepository.findAll(distinct(spec), pageable);
|
||||
Map<Long, BookEntity> booksById = bookRepository
|
||||
.findAllWithMetadataByIds(page.getContent().stream().map(BookEntity::getId).collect(Collectors.toSet()))
|
||||
.stream()
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.booklore.service.browse;
|
||||
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.Expression;
|
||||
import jakarta.persistence.criteria.From;
|
||||
import jakarta.persistence.criteria.Join;
|
||||
import jakarta.persistence.criteria.JoinType;
|
||||
import jakarta.persistence.criteria.Order;
|
||||
@@ -45,6 +46,10 @@ public class BookSortRegistry {
|
||||
for (String field : List.of("personalRating", "lastReadTime", "readStatus", "dateFinished")) {
|
||||
registry.register(field, progressField(field));
|
||||
}
|
||||
|
||||
registry.register("authorName", authorField("name"));
|
||||
registry.register("authorSortName", authorField("sortName"));
|
||||
|
||||
registry.register("readingProgress", readingProgress());
|
||||
registry.register("random", random());
|
||||
|
||||
@@ -56,17 +61,21 @@ public class BookSortRegistry {
|
||||
}
|
||||
|
||||
private static SortOrderBuilder<BookEntity> metadataField(String field) {
|
||||
return ctx -> List.of(order(ctx, metadataJoin(ctx.root()).get(field)));
|
||||
return ctx -> List.of(order(ctx, metadataJoin(ctx).get(field)));
|
||||
}
|
||||
|
||||
private static SortOrderBuilder<BookEntity> progressField(String field) {
|
||||
return ctx -> List.of(order(ctx, progressJoin(ctx).get(field)));
|
||||
}
|
||||
|
||||
private static SortOrderBuilder<BookEntity> authorField(String field) {
|
||||
return ctx -> List.of(order(ctx, authorJoin(ctx).get(field)));
|
||||
}
|
||||
|
||||
private static SortOrderBuilder<BookEntity> readingProgress() {
|
||||
return ctx -> {
|
||||
CriteriaBuilder cb = ctx.cb();
|
||||
Join<BookEntity, ?> progress = progressJoin(ctx);
|
||||
var progress = progressJoin(ctx);
|
||||
Expression<Float> greatest = null;
|
||||
for (String field : PROGRESS_PERCENT_FIELDS) {
|
||||
Expression<Float> value = cb.coalesce(progress.get(field), cb.literal(0f));
|
||||
@@ -112,28 +121,49 @@ public class BookSortRegistry {
|
||||
return ctx.descending() ? ctx.cb().desc(expression) : ctx.cb().asc(expression);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <Y> Join<BookEntity, Y> metadataJoin(Root<BookEntity> root) {
|
||||
for (Join<BookEntity, ?> join : root.getJoins()) {
|
||||
if (join.getAttribute().getName().equals("metadata") && join.getJoinType() == JoinType.LEFT) {
|
||||
return (Join<BookEntity, Y>) join;
|
||||
private static Join<?, ?> getSortJoin(From<?, ?> root, String name) {
|
||||
String alias = "sort_" + name;
|
||||
for (var join : root.getJoins()) {
|
||||
if (!alias.equals(join.getAlias())) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return root.join("metadata", JoinType.LEFT);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Join<BookEntity, ?> progressJoin(SortContext<BookEntity> ctx) {
|
||||
Root<BookEntity> root = ctx.root();
|
||||
for (Join<BookEntity, ?> join : root.getJoins()) {
|
||||
if (join.getAttribute().getName().equals("userBookProgress")) {
|
||||
return join;
|
||||
if (!name.equals(join.getAttribute().getName())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!JoinType.LEFT.equals(join.getJoinType())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return join;
|
||||
}
|
||||
Join<BookEntity, ?> join = root.join("userBookProgress", JoinType.LEFT);
|
||||
CriteriaBuilder cb = ctx.cb();
|
||||
Path<Object> joinUserId = ((Join<BookEntity, Object>) join).get("user").get("id");
|
||||
join.on(ctx.userId() != null ? cb.equal(joinUserId, ctx.userId()) : cb.disjunction());
|
||||
|
||||
Join<?, ?> join = root.join(name, JoinType.LEFT);
|
||||
join.alias(alias);
|
||||
return join;
|
||||
}
|
||||
|
||||
private static Join<?, ?> metadataJoin(SortContext<BookEntity> ctx) {
|
||||
Root<BookEntity> root = ctx.root();
|
||||
return getSortJoin(root, "metadata");
|
||||
}
|
||||
|
||||
private static Join<?, ?> progressJoin(SortContext<BookEntity> ctx) {
|
||||
Root<BookEntity> root = ctx.root();
|
||||
|
||||
var join = getSortJoin(root, "userBookProgress");
|
||||
|
||||
CriteriaBuilder cb = ctx.cb();
|
||||
Path<?> joinUserId = join.get("user").get("id");
|
||||
join.on(ctx.userId() != null ? cb.equal(joinUserId, ctx.userId()) : cb.disjunction());
|
||||
|
||||
return join;
|
||||
}
|
||||
|
||||
private static Join<?, ?> authorJoin(SortContext<BookEntity> ctx) {
|
||||
var parent = metadataJoin(ctx);
|
||||
|
||||
return getSortJoin(parent, "authors");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
"spring.jpa.hibernate.ddl-auto=create-drop",
|
||||
"spring.jpa.database-platform=org.hibernate.dialect.H2Dialect",
|
||||
"spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect",
|
||||
"spring.datasource.url=jdbc:h2:mem:browsetest;DB_CLOSE_DELAY=-1;NON_KEYWORDS=VALUE",
|
||||
"spring.datasource.url=jdbc:h2:mem:browsetest;DB_CLOSE_DELAY=-1;NON_KEYWORDS=VALUE;MODE=MYSQL",
|
||||
"spring.datasource.driver-class-name=org.h2.Driver",
|
||||
"spring.datasource.username=sa",
|
||||
"spring.datasource.password=",
|
||||
@@ -144,6 +144,7 @@ class BookBrowseRegistryTest {
|
||||
private AuthorEntity author(String name) {
|
||||
return authors.computeIfAbsent(name, n -> {
|
||||
AuthorEntity e = AuthorEntity.builder().name(n).build();
|
||||
e.computeSortName();
|
||||
em.persist(e);
|
||||
return e;
|
||||
});
|
||||
@@ -175,6 +176,49 @@ class BookBrowseRegistryTest {
|
||||
assertThat(sortedIds("-title", user.getId())).containsExactly(c, b, a);
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorAscendingAndDescending() {
|
||||
Long a = book("Alpha", null, null, Instant.now(), List.of(), List.of("Ned McDodd"), null).getId();
|
||||
Long b = book("Bravo", null, null, Instant.now(), List.of(), List.of("Sally O'Malley"), null).getId();
|
||||
Long c = book("Charlie", null, null, Instant.now(), List.of(), List.of("Haily Yelp"), null).getId();
|
||||
em.flush();
|
||||
assertThat(sortedIds("authorName", user.getId())).containsExactly(c, a, b);
|
||||
assertThat(sortedIds("-authorName", user.getId())).containsExactly(b, a, c);
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyAuthorAscendingAndDescending() {
|
||||
Long a = book("Alpha", null, null, Instant.now(), List.of(), List.of(), null).getId();
|
||||
Long b = book("Bravo", null, null, Instant.now(), List.of(), List.of("Sally O'Malley"), null).getId();
|
||||
Long c = book("Charlie", null, null, Instant.now(), List.of(), List.of("Haily Yelp"), null).getId();
|
||||
em.flush();
|
||||
assertThat(sortedIds("authorName", user.getId())).containsExactly(a, c, b);
|
||||
assertThat(sortedIds("-authorName", user.getId())).containsExactly(b, c, a);
|
||||
}
|
||||
|
||||
@Test
|
||||
void multiAuthorAscendingAndDescending() {
|
||||
Long a = book("Alpha", null, null, Instant.now(), List.of(), List.of("Ned McDodd", "Euchariah Who"), null).getId();
|
||||
Long b = book("Bravo", null, null, Instant.now(), List.of(), List.of("Sally O'Malley", "Axl"), null).getId();
|
||||
Long c = book("Charlie", null, null, Instant.now(), List.of(), List.of("Haily Yelp"), null).getId();
|
||||
em.flush();
|
||||
|
||||
// These are the same because of how the sorting works with multiple authors.
|
||||
// `b` has both the highest and lowest.
|
||||
assertThat(sortedIds("authorName", user.getId())).containsExactly(b, a, c);
|
||||
assertThat(sortedIds("-authorName", user.getId())).containsExactly(b, a, c);
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorSortNameAscendingAndDescending() {
|
||||
Long a = book("Alpha", null, null, Instant.now(), List.of(), List.of("Red McDodd"), null).getId();
|
||||
Long b = book("Bravo", null, null, Instant.now(), List.of(), List.of("Sally O'Malley"), null).getId();
|
||||
Long c = book("Charlie", null, null, Instant.now(), List.of(), List.of("Haily Yelp"), null).getId();
|
||||
em.flush();
|
||||
assertThat(sortedIds("authorSortName", user.getId())).containsExactly(a, b, c);
|
||||
assertThat(sortedIds("-authorSortName", user.getId())).containsExactly(c, b, a);
|
||||
}
|
||||
|
||||
@Test
|
||||
void idTiebreakerOrdersEqualKeysById() {
|
||||
Long first = book("Same").getId();
|
||||
|
||||
@@ -53,7 +53,7 @@ import static org.mockito.Mockito.when;
|
||||
"spring.jpa.hibernate.ddl-auto=create-drop",
|
||||
"spring.jpa.database-platform=org.hibernate.dialect.H2Dialect",
|
||||
"spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect",
|
||||
"spring.datasource.url=jdbc:h2:mem:browseservicetest;DB_CLOSE_DELAY=-1;NON_KEYWORDS=VALUE",
|
||||
"spring.datasource.url=jdbc:h2:mem:browseservicetest;DB_CLOSE_DELAY=-1;NON_KEYWORDS=VALUE;MODE=MYSQL",
|
||||
"spring.datasource.driver-class-name=org.h2.Driver",
|
||||
"spring.datasource.username=sa",
|
||||
"spring.datasource.password=",
|
||||
|
||||
Reference in New Issue
Block a user