Repository layer:
I have two methods that extract ID lists from 2 different collections.
From the first collection - ID of all documents (entity = lecturer).
From the second collection - ID of all lecturers with the "status" field set to "ACTIVE".
Service layer:
The third method is in the service layer and its purpose is to check the differences between two ID lists and return a list of differences.
Repository methods code :
List<String> getAllActiveLecturerIDsList() {
Query query = new Query();
query.fields().include("_id");
query.addCriteria(Criteria.where("status").is("ACTIVE"));
ArrayList<String> listOfIDs = new ArrayList<>();
mongoTemplate.executeQuery(query, "brlnlecturers", document -> listOfIDs.add(document.getObjectId("_id").toString()));
return listOfIDs;
}
List<String> getAllCurLecturerIDsList() {
Query query = new Query();
query.fields().include("_id");
ArrayList<String> listOfIDs = new ArrayList<>();
mongoTemplate.executeQuery(query, "lndlecturers", document -> listOfIDs.add(document.getObjectId("_id").toString()));
return listOfIDs;
}
Service method code:
List<String> getListOfDifferences() {
List<String> lndLecturerIdList = lecturerRepository.getAllCurLecturerIDsList();
List<String> lecturerActiveList = lecturerRepository.getAllActiveLecturerIDsList();
if (lndLecturerIdList.size() > lecturerActiveList.size()) {
return lndLecturerIdList.stream()
.filter(element -> !lecturerActiveList.contains(element))
.collect(Collectors.toList());
}
if (lecturerActiveList.size() > lndLecturerIdList.size()) {
return lecturerActiveList.stream()
.filter(element -> !lndLecturerIdList.contains(element))
.collect(Collectors.toList());
}
return Collections.emptyList();
}
Goal:
I'm looking for a way to combine all these methods into one at the repository level that will do all the job at the mongodb query level.
So create a method with a query that will return a list of ID differences from two collections, in one of which, in addition to the ID, we also verify the status of the record.
Unfortunately, I cannot find a solution to this problem.
My approach seems to me inefficient and not very elegant.