Skip to content

Commit

Permalink
Adding the sorting logic for v2 explore API (#485)
Browse files Browse the repository at this point in the history
* Adding the sorting logic for v2 explore API

* Adding the sorted Logic for v2
  • Loading branch information
Sahil-tarento authored Mar 7, 2024
1 parent e7783f2 commit 472c787
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ public SBApiResponse getExploreCourseListV2() {
List<Map<String, Object>> courseList = cassandraOperation.getRecordsByPropertiesWithoutFiltering(
Constants.SUNBIRD_KEY_SPACE_NAME, Constants.TABLE_EXPLORE_COURSE_LIST_V2, MapUtils.EMPTY_MAP,
ListUtils.EMPTY_LIST);
if (CollectionUtils.isNotEmpty(courseList)) {
Comparator<Map<String, Object>> customComparator = Comparator.comparing(entry -> {
Integer seqNo = (Integer) entry.get(Constants.SEQ_NO);
return (seqNo != null) ? seqNo : Integer.MAX_VALUE;
});
courseList = courseList.stream().sorted(customComparator).collect(Collectors.toList());
}
List<String> identifierList = new ArrayList<String>();
for (Map<String, Object> course : courseList) {
identifierList.add((String) course.get(Constants.IDENTIFIER));
Expand All @@ -150,6 +157,17 @@ public SBApiResponse getExploreCourseListV2() {
errMsg = "Failed to get contant details for Identifier List from DB.";
} else {
Map<String, Object> responseCourseList = (Map<String, Object>) searchResponse.get(Constants.RESULT);
List<Map<String, Object>> contentList = (List<Map<String, Object>>) responseCourseList.get(Constants.CONTENT);
if (CollectionUtils.isNotEmpty(contentList)) {
List<Map<String, Object>> sortedContentList = identifierList.stream()
.map(identifier -> contentList.stream()
.filter(content -> identifier.equals(content.get(Constants.IDENTIFIER)))
.findFirst().orElse(null))
.filter(Objects::nonNull)
.sorted(Comparator.comparing(content -> (String) content.get(Constants.PRIMARY_CATEGORY)))
.collect(Collectors.toList());
responseCourseList.put(Constants.CONTENT, sortedContentList);
}
response.setResult(responseCourseList);
}
}
Expand Down

0 comments on commit 472c787

Please sign in to comment.