fix(api): improve sort feature parity of AppBookService (#828)
This commit is contained in:
@@ -53,6 +53,7 @@ public class AppBookService {
|
|||||||
|
|
||||||
private static final int DEFAULT_PAGE_SIZE = 50;
|
private static final int DEFAULT_PAGE_SIZE = 50;
|
||||||
private static final int MAX_PAGE_SIZE = 50;
|
private static final int MAX_PAGE_SIZE = 50;
|
||||||
|
private static final String DEFAULT_SORT = "addedon";
|
||||||
|
|
||||||
private final BookRepository bookRepository;
|
private final BookRepository bookRepository;
|
||||||
private final UserBookProgressRepository userBookProgressRepository;
|
private final UserBookProgressRepository userBookProgressRepository;
|
||||||
@@ -1044,32 +1045,47 @@ public class AppBookService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For `lastReadTime` sorting we need to filter to only the current user's progress.
|
// Any of the user book progress sorting we need to filter to only the current user's progress.
|
||||||
if ("lastreadtime".equalsIgnoreCase(req.sort())) {
|
String field = getSortField(req.sort());
|
||||||
|
if (field.startsWith("userBookProgress.")) {
|
||||||
specs.add(AppBookSpecification.withProgress(userId, true));
|
specs.add(AppBookSpecification.withProgress(userId, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
return AppBookSpecification.combine(specs.toArray(new Specification[0]));
|
return AppBookSpecification.combine(specs.toArray(new Specification[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getSortField(String sortBy) {
|
||||||
|
return switch (sortBy != null ? sortBy.toLowerCase() : DEFAULT_SORT) {
|
||||||
|
case "addedon" -> "addedOn";
|
||||||
|
case "title" -> "metadata.title";
|
||||||
|
case "seriesname", "series" -> "metadata.seriesName";
|
||||||
|
case "seriesnumber" -> "metadata.seriesNumber";
|
||||||
|
case "pagecount" -> "metadata.pageCount";
|
||||||
|
case "language" -> "metadata.language";
|
||||||
|
case "publisher" -> "metadata.publisher";
|
||||||
|
case "publisheddate" -> "metadata.publishedDate";
|
||||||
|
case "amazonrating" -> "metadata.amazonRating";
|
||||||
|
case "amazonreviewcount" -> "metadata.amazonReviewCount";
|
||||||
|
case "goodreadsrating" -> "metadata.goodreadsRating";
|
||||||
|
case "goodreadsreviewcount" -> "metadata.goodreadsReviewCount";
|
||||||
|
case "hardcoverrating" -> "metadata.hardcoverRating";
|
||||||
|
case "hardcoverreviewcount" -> "metadata.hardcoverReviewCount";
|
||||||
|
case "ranobedbrating" -> "metadata.ranobedbRating";
|
||||||
|
case "narrator" -> "metadata.narrator";
|
||||||
|
case "lastreadtime" -> "userBookProgress.lastReadTime";
|
||||||
|
case "readstatus" -> "userBookProgress.readStatus";
|
||||||
|
case "datefinished" -> "userBookProgress.dateFinished";
|
||||||
|
case "personalrating" -> "userBookProgress.personalRating";
|
||||||
|
default -> "addedOn";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private Sort buildSort(String sortBy, String sortDir) {
|
private Sort buildSort(String sortBy, String sortDir) {
|
||||||
Sort.Direction direction = "asc".equalsIgnoreCase(sortDir)
|
Sort.Direction direction = "asc".equalsIgnoreCase(sortDir)
|
||||||
? Sort.Direction.ASC
|
? Sort.Direction.ASC
|
||||||
: Sort.Direction.DESC;
|
: Sort.Direction.DESC;
|
||||||
|
|
||||||
// "author" needs a join to the authors collection, which can't be expressed
|
String field = getSortField(sortBy);
|
||||||
// as a simple property path, fall through to the default (addedOn) for now.
|
|
||||||
String field = switch (sortBy != null ? sortBy.toLowerCase() : "") {
|
|
||||||
case "title" -> "metadata.title";
|
|
||||||
case "seriesname", "series" -> "metadata.seriesName";
|
|
||||||
case "seriesnumber" -> "metadata.seriesNumber";
|
|
||||||
case "publisher" -> "metadata.publisher";
|
|
||||||
case "language" -> "metadata.language";
|
|
||||||
case "publisheddate" -> "metadata.publishedDate";
|
|
||||||
case "lastreadtime" -> "userBookProgress.lastReadTime";
|
|
||||||
case "pagecount" -> "metadata.pageCount";
|
|
||||||
default -> "addedOn";
|
|
||||||
};
|
|
||||||
|
|
||||||
return Sort.by(direction, field);
|
return Sort.by(direction, field);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user