Facebook
From Kingslayer, 4 Years ago, written in Java.
This paste is a reply to Irrelevant to the body from Kingslayer - go back
Embed
Viewing differences between Irrelevant to the body and Re: Irrelevant to the body
MongoTemplate mongoTemplate = new MongoTemplate(new MongoClient(new MongoClientURI(mongoURI)), "news-archive");
Query query = new Query();

List tempList = new ArrayList<>();

if(keyword != null) {
  String[] strings = keyword.toString().split(" ");
  for (int idx = 0; idx < strings.length; idx++) {
    List now = this.newsRepository.findAllByNewsContainingOrTitleContaining(pageable, strings[idx]);
    tempList.addAll(now);
  }
}

if (category != null) {
  query.addCriteria(Criteria.where("category").regex(category.toString()));
}

if(date != null) {
  String dateString = date.toString().substring(0, date.toString().length()-1);
  LocalDateTime ldt = LocalDateTime.parse(dateString);
  LocalDateTime lower, upper;
  lower = LocalDateTime.of(ldt.getYear(), ldt.getMonth(), ldt.getDayOfMonth(), 0, 0, 0);
  upper = LocalDateTime.of(ldt.getYear(), ldt.getMonth(), ldt.getDayOfMonth(), 23, 59, 59);
  query.addCriteria(Criteria.where("date").gte(lower).lte(upper));
}

List list = mongoTemplate.find(query, News.class);
List finalList = new ArrayList<>();

if(tempList.size() > 0) {
  finalList.addAll(tempList);
  finalList.retainAll(list);
} else {
  finalList.addAll(list);
}

finalList = finalList.stream().distinct().collect(Collectors.toList());

return finalList;