I'm making the chat application. When I'm retrieving the chat history of the user, It is bringing all the records which are present in the database. I just need only the chat history of the last 3 days of that user. Need to apply pagination for this with the date.
I'm sharing my sample code here
This is controller:
@GetMapping("/getAllGroupChatHistory/{userId}")
ArrayList<ResultGroupChatHistoryDTO> getAllGroupChatHistory(@PathVariable long userId){
return messagingService.getAllGroupChatHistory(userId);
}
This is serviceImpl
public ArrayList<ResultGroupChatHistoryDTO> getAllGroupChatHistory(long userId) {
List<GroupEntity> groupMembers = groupRepository.findAll();
List<GroupEntity> listgroupMembers = new ArrayList<GroupEntity>();
groupMembers.forEach(groupMember -> {
if (groupMember.getParticipates().contains(userId)) {
listgroupMembers.add(groupMember);
}
});
System.out.println("list" + listgroupMembers);
List grpIds = listgroupMembers.stream().map(grpEntity -> grpEntity.getGroup_id()).collect(Collectors.toList());
ArrayList<ResultGroupChatHistoryDTO> finalresult = new ArrayList();
for (int val = 0; val < grpIds.size(); val++) {
ResultGroupChatHistoryDTO rchatDTO = new ResultGroupChatHistoryDTO();
int grpId = (int) grpIds.get(val);
List<GroupChatEntity> senderHistory = groupChatRepository.findChatByGroupId(grpId);
ArrayList<GroupChatHistoryDTO> message = new ArrayList();
for (int i = 0; i < senderHistory.size(); i++) {
GroupChatHistoryDTO chatDTO = new GroupChatHistoryDTO();
GroupChatEntity groupChatEntity = senderHistory.get(i);
chatDTO.setId(groupChatEntity.getSender_id());
chatDTO.setSenderName(groupChatEntity.getReciever_name());
chatDTO.setTime(groupChatEntity.getSent_time());
chatDTO.setLastMessage(groupChatEntity.getMessage());
message.add(chatDTO);
}
rchatDTO.setMessages(message);
rchatDTO.setId(val);
finalresult.add(rchatDTO);
}
return finalresult;
}
I'm using mongoRepository to connect. How I need to apply the pagination for this based on the date of last 3 days?