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);
|
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) {
|
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
|
Map<Long, BookEntity> booksById = bookRepository
|
||||||
.findAllWithMetadataByIds(page.getContent().stream().map(BookEntity::getId).collect(Collectors.toSet()))
|
.findAllWithMetadataByIds(page.getContent().stream().map(BookEntity::getId).collect(Collectors.toSet()))
|
||||||
.stream()
|
.stream()
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.booklore.service.browse;
|
|||||||
|
|
||||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||||
import jakarta.persistence.criteria.Expression;
|
import jakarta.persistence.criteria.Expression;
|
||||||
|
import jakarta.persistence.criteria.From;
|
||||||
import jakarta.persistence.criteria.Join;
|
import jakarta.persistence.criteria.Join;
|
||||||
import jakarta.persistence.criteria.JoinType;
|
import jakarta.persistence.criteria.JoinType;
|
||||||
import jakarta.persistence.criteria.Order;
|
import jakarta.persistence.criteria.Order;
|
||||||
@@ -45,6 +46,10 @@ public class BookSortRegistry {
|
|||||||
for (String field : List.of("personalRating", "lastReadTime", "readStatus", "dateFinished")) {
|
for (String field : List.of("personalRating", "lastReadTime", "readStatus", "dateFinished")) {
|
||||||
registry.register(field, progressField(field));
|
registry.register(field, progressField(field));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registry.register("authorName", authorField("name"));
|
||||||
|
registry.register("authorSortName", authorField("sortName"));
|
||||||
|
|
||||||
registry.register("readingProgress", readingProgress());
|
registry.register("readingProgress", readingProgress());
|
||||||
registry.register("random", random());
|
registry.register("random", random());
|
||||||
|
|
||||||
@@ -56,17 +61,21 @@ public class BookSortRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static SortOrderBuilder<BookEntity> metadataField(String field) {
|
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) {
|
private static SortOrderBuilder<BookEntity> progressField(String field) {
|
||||||
return ctx -> List.of(order(ctx, progressJoin(ctx).get(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() {
|
private static SortOrderBuilder<BookEntity> readingProgress() {
|
||||||
return ctx -> {
|
return ctx -> {
|
||||||
CriteriaBuilder cb = ctx.cb();
|
CriteriaBuilder cb = ctx.cb();
|
||||||
Join<BookEntity, ?> progress = progressJoin(ctx);
|
var progress = progressJoin(ctx);
|
||||||
Expression<Float> greatest = null;
|
Expression<Float> greatest = null;
|
||||||
for (String field : PROGRESS_PERCENT_FIELDS) {
|
for (String field : PROGRESS_PERCENT_FIELDS) {
|
||||||
Expression<Float> value = cb.coalesce(progress.get(field), cb.literal(0f));
|
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);
|
return ctx.descending() ? ctx.cb().desc(expression) : ctx.cb().asc(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
private static Join<?, ?> getSortJoin(From<?, ?> root, String name) {
|
||||||
private static <Y> Join<BookEntity, Y> metadataJoin(Root<BookEntity> root) {
|
String alias = "sort_" + name;
|
||||||
for (Join<BookEntity, ?> join : root.getJoins()) {
|
for (var join : root.getJoins()) {
|
||||||
if (join.getAttribute().getName().equals("metadata") && join.getJoinType() == JoinType.LEFT) {
|
if (!alias.equals(join.getAlias())) {
|
||||||
return (Join<BookEntity, Y>) join;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return root.join("metadata", JoinType.LEFT);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
if (!name.equals(join.getAttribute().getName())) {
|
||||||
private static Join<BookEntity, ?> progressJoin(SortContext<BookEntity> ctx) {
|
continue;
|
||||||
Root<BookEntity> root = ctx.root();
|
|
||||||
for (Join<BookEntity, ?> join : root.getJoins()) {
|
|
||||||
if (join.getAttribute().getName().equals("userBookProgress")) {
|
|
||||||
return join;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!JoinType.LEFT.equals(join.getJoinType())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return join;
|
||||||
}
|
}
|
||||||
Join<BookEntity, ?> join = root.join("userBookProgress", JoinType.LEFT);
|
|
||||||
CriteriaBuilder cb = ctx.cb();
|
Join<?, ?> join = root.join(name, JoinType.LEFT);
|
||||||
Path<Object> joinUserId = ((Join<BookEntity, Object>) join).get("user").get("id");
|
join.alias(alias);
|
||||||
join.on(ctx.userId() != null ? cb.equal(joinUserId, ctx.userId()) : cb.disjunction());
|
|
||||||
return join;
|
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.hibernate.ddl-auto=create-drop",
|
||||||
"spring.jpa.database-platform=org.hibernate.dialect.H2Dialect",
|
"spring.jpa.database-platform=org.hibernate.dialect.H2Dialect",
|
||||||
"spring.jpa.properties.hibernate.dialect=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.driver-class-name=org.h2.Driver",
|
||||||
"spring.datasource.username=sa",
|
"spring.datasource.username=sa",
|
||||||
"spring.datasource.password=",
|
"spring.datasource.password=",
|
||||||
@@ -144,6 +144,7 @@ class BookBrowseRegistryTest {
|
|||||||
private AuthorEntity author(String name) {
|
private AuthorEntity author(String name) {
|
||||||
return authors.computeIfAbsent(name, n -> {
|
return authors.computeIfAbsent(name, n -> {
|
||||||
AuthorEntity e = AuthorEntity.builder().name(n).build();
|
AuthorEntity e = AuthorEntity.builder().name(n).build();
|
||||||
|
e.computeSortName();
|
||||||
em.persist(e);
|
em.persist(e);
|
||||||
return e;
|
return e;
|
||||||
});
|
});
|
||||||
@@ -175,6 +176,49 @@ class BookBrowseRegistryTest {
|
|||||||
assertThat(sortedIds("-title", user.getId())).containsExactly(c, b, a);
|
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
|
@Test
|
||||||
void idTiebreakerOrdersEqualKeysById() {
|
void idTiebreakerOrdersEqualKeysById() {
|
||||||
Long first = book("Same").getId();
|
Long first = book("Same").getId();
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ import static org.mockito.Mockito.when;
|
|||||||
"spring.jpa.hibernate.ddl-auto=create-drop",
|
"spring.jpa.hibernate.ddl-auto=create-drop",
|
||||||
"spring.jpa.database-platform=org.hibernate.dialect.H2Dialect",
|
"spring.jpa.database-platform=org.hibernate.dialect.H2Dialect",
|
||||||
"spring.jpa.properties.hibernate.dialect=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.driver-class-name=org.h2.Driver",
|
||||||
"spring.datasource.username=sa",
|
"spring.datasource.username=sa",
|
||||||
"spring.datasource.password=",
|
"spring.datasource.password=",
|
||||||
|
|||||||
Reference in New Issue
Block a user