Skip to content

Instantly share code, notes, and snippets.

@GayanM
Created September 25, 2022 04:09
Show Gist options
  • Select an option

  • Save GayanM/380ceb4f7e2c77f4b5ba733fcb37217a to your computer and use it in GitHub Desktop.

Select an option

Save GayanM/380ceb4f7e2c77f4b5ba733fcb37217a to your computer and use it in GitHub Desktop.
Get orders contains given product category
@RequestMapping("")
public List<Order> getOrdersByProductCategory (@RequestParam("category") final Optional<String> category) {
return orderRepo.findAll().stream().
filter(o -> o.getProducts().stream().anyMatch(p -> {
if (category.isEmpty()) {
return true;
}
return category.get().equalsIgnoreCase(p.getCategory());
})).
map(o -> new Order(o.getId(), o.getOrderDate(), o.getDeliveryDate(), o.getStatus())).
collect(Collectors.toList());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment