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
database = postgres # Determines which of PostgreSQL or Cassandra | |
# this node will use as its datastore. | |
# Accepted values are `postgres`, | |
# `cassandra`, and `off`. | |
pg_host = 127.0.0.1 # Host of the Postgres server. | |
pg_port = 5432 # Port of the Postgres server. | |
pg_timeout = 5000 # Defines the timeout (in ms), for connecting, | |
# reading and writing. |
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("/most-expensive") | |
Map<String, Optional<Product>> getMostExpensiveProductByCategory () { | |
Map<String, Optional<com.mycompany.functional.programming.exercises.models.Product>> mostExpensiveByCategory = productRepos.findAll().stream().collect(Collectors.groupingBy( | |
com.mycompany.functional.programming.exercises.models.Product::getCategory, | |
Collectors.maxBy(Comparator.comparing(com.mycompany.functional.programming.exercises.models. | |
Product::getPrice) | |
) | |
)); | |
Map<String, Optional<Product>> ordersByCustomerTransformed = new HashMap(); | |
mostExpensiveByCategory.entrySet().stream().forEach(e -> { |
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("/product-category") | |
Map<String, List<String>> getProductsByCategory(List<Product> productList) { | |
return productRepos.findAll().stream().collect(Collectors.groupingBy(com.mycompany.functional.programming. | |
exercises.models.Product::getCategory, Collectors.mapping(product -> product.getName(), | |
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("/product-total-sum") | |
public Map<Order, Double> getProductTotalSumByOrder() { | |
return orderRepo.findAll().stream().collect(Collectors.toMap(o -> new Order(o.getId(), o.getOrderDate(), | |
o.getDeliveryDate(), o.getStatus()), 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("/group-by-customer") | |
public Map<Customer, List<Order>> getOrdersByCustomer () { | |
Map<com.mycompany.functional.programming.exercises.models.Customer, | |
List<com.mycompany.functional.programming.exercises.models.Order>> ordersByCustomer = orderRepo. | |
findAll().stream().collect(Collectors.groupingBy(com.mycompany.functional.programming.exercises.models. | |
Order::getCustomer)); | |
Map<Customer, List<Order>> ordersByCustomerTransformed = new HashMap(); | |
ordersByCustomer.forEach((k,v) -> { | |
Customer customer = new Customer(k.getId(), k.getName(), k.getTier()); | |
List<Order> orders = v.stream().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("/products-count") | |
public Map<Long, Integer> getOrdersWithProductCount() { | |
return orderRepo.findAll().stream(). | |
collect(Collectors.toMap(o -> o.getId(), o -> o.getProducts().size())); | |
} |
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(); | |
} |
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("/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("/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()); |
NewerOlder