Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
fix: return the total number of elements in paged result
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc authored and marcambier committed Oct 25, 2021
1 parent 457a011 commit 4433e61
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ public Page<AlertEventMongo> search(AlertEventCriteria criteria, Pageable pageab
// set sort by updated at
query.with(Sort.by(Sort.Direction.DESC, "createdAt"));

long total = mongoTemplate.count(query, AlertEventMongo.class);

// set pageable
if (pageable != null) {
query.with(PageRequest.of(pageable.pageNumber(), pageable.pageSize()));
}

List<AlertEventMongo> events = mongoTemplate.find(query, AlertEventMongo.class);
long total = mongoTemplate.count(query, AlertEventMongo.class);

return new Page<>(events, (pageable != null) ? pageable.pageNumber() : 0, events.size(), total);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public Page<TicketMongo> search(TicketCriteria criteria, Sortable sortable, Page
}
}

long total = mongoTemplate.count(query, TicketMongo.class);

if (pageable != null) {
query.with(PageRequest.of(pageable.pageNumber(), pageable.pageSize()));
}
Expand All @@ -80,7 +82,6 @@ public Page<TicketMongo> search(TicketCriteria criteria, Sortable sortable, Page
query.collation(Collation.of(Locale.ENGLISH).strength(Collation.ComparisonLevel.secondary()));

List<TicketMongo> tickets = mongoTemplate.find(query, TicketMongo.class);
long total = mongoTemplate.count(query, TicketMongo.class);

return new Page<>(tickets, (pageable != null) ? pageable.pageNumber() : 0, tickets.size(), total);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ public Page<UserMongo> search(UserCriteria criteria, Pageable pageable) {
}
}
query.with(Sort.by(Sort.Direction.ASC, "lastname", "firstname"));

long total = mongoTemplate.count(query, UserMongo.class);

if (pageable != null) {
query.with(PageRequest.of(pageable.pageNumber(), pageable.pageSize()));
}

List<UserMongo> users = mongoTemplate.find(query, UserMongo.class);
long total = mongoTemplate.count(query, UserMongo.class);

return new Page<>(users, (pageable != null) ? pageable.pageNumber() : 0, users.size(), total);
}
Expand Down

0 comments on commit 4433e61

Please sign in to comment.