fix(api): use correct field for lastReadTime (#779)

This commit is contained in:
James Ward
2026-04-23 00:28:33 -04:00
committed by GitHub
parent 7c466ae17d
commit bfca35b03c
2 changed files with 28 additions and 1 deletions
@@ -1044,6 +1044,11 @@ public class AppBookService {
}
}
// For `lastReadTime` sorting we need to filter to only the current user's progress.
if ("lastreadtime".equalsIgnoreCase(req.sort())) {
specs.add(AppBookSpecification.withProgress(userId, true));
}
return AppBookSpecification.combine(specs.toArray(new Specification[0]));
}
@@ -1061,7 +1066,7 @@ public class AppBookService {
case "publisher" -> "metadata.publisher";
case "language" -> "metadata.language";
case "publisheddate" -> "metadata.publishedDate";
case "lastreadtime" -> "lastReadTime";
case "lastreadtime" -> "userBookProgress.lastReadTime";
case "pagecount" -> "metadata.pageCount";
default -> "addedOn";
};
@@ -131,6 +131,28 @@ public class AppBookSpecification {
};
}
/**
* Filter books that have any progress for a specific user.
* If `optional` is true, allow books without user progress to be included
* but omit other user's progress.
*/
public static Specification<BookEntity> withProgress(Long userId, boolean optional) {
return (root, query, cb) -> {
if (userId == null) {
return cb.conjunction();
}
Join<BookEntity, UserBookProgressEntity> progressJoin = root.join("userBookProgress", JoinType.LEFT);
progressJoin = progressJoin.on(cb.equal(progressJoin.get("user").get("id"), userId));
if (optional) {
return cb.conjunction();
}
return cb.equal(progressJoin.get("user").get("id"), userId);
};
}
public static Specification<BookEntity> addedWithinDays(int days) {
return (root, query, cb) -> {
Instant cutoff = Instant.now().minus(days, ChronoUnit.DAYS);