This file contains hidden or 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
| @RequestMapping("") | |
| public List<Product> filterProducts(@Param("category") final Optional<String> category, | |
| @Param("lower-price-limit") final Optional<Double> lowerLimitPrice, | |
| @RequestParam("upper-price-limit") final Optional<Double> upperLimitPrice ) { | |
| return productRepos.findAll().stream(). | |
| filter(p -> { | |
| if (category.isEmpty()) { | |
| return true; |
This file contains hidden or 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
| @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())). |
This file contains hidden or 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
| @RequestMapping("/discount") | |
| public List<Product> applyDiscountOnCategory(@RequestParam("category") final Optional<String> category, | |
| @RequestParam("discount") final Optional<Double> discount) { | |
| return productRepos.findAll().stream(). | |
| filter(p -> { | |
| if (category.isEmpty()) { | |
| return true; | |
| } | |
| return category.get().equalsIgnoreCase(p.getCategory()); | |
| }).map(p -> { |
This file contains hidden or 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
| @RequestMapping("/customer-tier") | |
| public List<Product> getProductsByCustomerTier (@RequestParam("tier") final Optional<Integer> tier, | |
| @RequestParam("start-date") final Optional<String> startDate, | |
| @RequestParam("end-date") final Optional<String> endDate) { | |
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | |
| return orderRepo.findAll().stream(). | |
| filter(o -> { | |
| if (tier.isEmpty()) { | |
| return true; | |
| } |
This file contains hidden or 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
| @RequestMapping("/cheapest") | |
| public Optional<Product> getCheapestProductByCategory(@RequestParam("category") final Optional<String> category) { | |
| return productRepos.findAll().stream().filter(p -> { | |
| if (category.isEmpty()) { | |
| return true; | |
| } | |
| return category.get().equalsIgnoreCase(p.getCategory()); | |
| }).min(Comparator.comparing(com.mycompany.functional.programming.exercises.models.Product::getPrice)).map(p -> | |
| new Product(p.getId(), p.getName(), p.getPrice(), p.getCategory())); | |
| } |
This file contains hidden or 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
| @RequestMapping("/recent") | |
| public List<Order> getRecentOrders (@RequestParam("limit") final Optional<Long> limit) { | |
| return orderRepo.findAll().stream(). | |
| sorted(Comparator. | |
| comparing(com.mycompany.functional.programming.exercises.models.Order::getOrderDate). | |
| reversed()).limit(limit.get()).map(o -> new Order(o.getId(), o.getOrderDate(), | |
| o.getDeliveryDate(), o.getStatus())).collect(Collectors.toList()); | |
| } |
This file contains hidden or 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
| @RequestMapping("/log-orders") | |
| public List<Product> logOrdersOnGivenDateAndReturnProducts(@RequestParam("date") final Optional<String> date) { | |
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | |
| return orderRepo.findAll().stream() | |
| .filter(o -> o.getOrderDate().isEqual(LocalDate.parse(date.get(), formatter))) | |
| .peek(o -> System.out.println(o.toString())) | |
| .flatMap(o -> o.getProducts().stream()) | |
| .distinct().map(p -> new Product(p.getId(), p.getName(), p.getPrice(), p.getCategory())) | |
| .collect(Collectors.toList()); |
This file contains hidden or 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
| @RequestMapping("/lump-sum") | |
| public Double lumpSumOfOrders (@RequestParam("month") final int month) { | |
| return orderRepo.findAll() | |
| .stream() | |
| .filter(o -> o.getOrderDate().compareTo(LocalDate.of(2021, month, 1)) >= 0) | |
| .filter(o -> o.getOrderDate().compareTo(LocalDate.of(2021, month + 1, 1)) < 0) | |
| .flatMap(o -> o.getProducts().stream()) | |
| .mapToDouble(p -> p.getPrice()) | |
| .sum(); |
This file contains hidden or 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
| @RequestMapping("/avg-payment") | |
| public Double getAvgPayment(@RequestParam("date") final Optional<String> date) { | |
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | |
| return orderRepo.findAll().stream().filter(o -> o.getOrderDate().isEqual(LocalDate. | |
| parse(date.get(), formatter))).flatMap(o -> o.getProducts().stream()).mapToDouble(p -> | |
| p.getPrice()).average().getAsDouble(); | |
| } |
This file contains hidden or 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
| @RequestMapping("/statistics") | |
| public DoubleSummaryStatistics getStatistics(@RequestParam("category") final Optional<String> category) { | |
| return productRepos.findAll().stream().filter(p -> category.get().equalsIgnoreCase(p.getCategory())). | |
| mapToDouble(p -> p.getPrice()).summaryStatistics(); | |
| } |
OlderNewer