Skip to content

Instantly share code, notes, and snippets.

View GayanM's full-sized avatar

Gayan GayanM

View GitHub Profile
@GayanM
GayanM / kong.conf
Created October 4, 2022 06:15
Kong gateway configuration
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.
@GayanM
GayanM / MostExpensiveProduct.java
Created September 25, 2022 11:12
Get the most expensive product by category
@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 -> {
@GayanM
GayanM / ProductCategory.java
Created September 25, 2022 11:09
Obtain a data map with list of product name by category
@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())));
}
@GayanM
GayanM / ProductTotalSum.java
Created September 25, 2022 11:04
Produce a data map with order record and product total sum
@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()));
}
@GayanM
GayanM / GroupByCustomer.java
Created September 25, 2022 11:01
Produce a data map with order records grouped by customer
@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())).
@GayanM
GayanM / ProductsCount.java
Created September 25, 2022 10:59
Obtain a data map with order id and order’s product count
@RequestMapping("/products-count")
public Map<Long, Integer> getOrdersWithProductCount() {
return orderRepo.findAll().stream().
collect(Collectors.toMap(o -> o.getId(), o -> o.getProducts().size()));
}
@GayanM
GayanM / Statistics.java
Created September 25, 2022 10:28
Obtain a collection of statistic figures (i.e. sum, average, max, min, count) for all products of given category
@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();
}
@GayanM
GayanM / AvgPayment.java
Created September 25, 2022 10:25
Calculate order average payment placed on 14-Mar-2021:Answ=287.604
@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();
}
@GayanM
GayanM / LumpSumOfOrders.java
Created September 25, 2022 10:21
Calculate total lump sum of all orders placed in Feb 2021:Answ=11995.36
@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();
@GayanM
GayanM / LogOrders.java
Created September 25, 2022 10:16
Get a list of orders which were ordered on 15-Mar-2021, log the order records to the console and then return its product list
@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());