Created
July 7, 2022 23:23
-
-
Save prestigegodson/e7bebab720b4f8d044ba929d8a2dab14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Page<GroupedCustomerTransaction> groupCustomerTransactionRecordByMonthOfYear(Page<CustomerTransactionDTO> customerTransactionPage) { | |
List<CustomerTransactionDTO> customerTransactions = customerTransactionPage.getContent(); | |
Map<String, List<CustomerTransactionDTO>> store = new HashMap<>(); | |
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MMMM-yyyy"); | |
customerTransactions.stream().forEach(customerTransaction -> { | |
String createdYearMonth = dateTimeFormatter.format(customerTransaction.getCreatedDate()); | |
if(store.containsKey(createdYearMonth)){ | |
store.get(createdYearMonth).add(customerTransaction); | |
}else{ | |
List<CustomerTransactionDTO> list = new ArrayList<CustomerTransactionDTO>(){}; | |
list.add(customerTransaction); | |
store.put(createdYearMonth, list); | |
} | |
}); | |
List<GroupedCustomerTransaction> groupedCustomerTransactions = buildCustomerTransactionGroup(store); | |
return new PageImpl<>(groupedCustomerTransactions, customerTransactionPage.getPageable(), customerTransactionPage.getTotalElements()); | |
} | |
private List<GroupedCustomerTransaction> buildCustomerTransactionGroup(Map<String, List<CustomerTransactionDTO>> store) { | |
return store.keySet().stream().map(key -> | |
GroupedCustomerTransaction.builder() | |
.date(key) | |
.customerTransactions(store.get(key)) | |
.build()).collect(Collectors.toList()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment