Skip to content

Instantly share code, notes, and snippets.

View GayanM's full-sized avatar

Gayan GayanM

View GitHub Profile
@GayanM
GayanM / RecentOrders.java
Created September 25, 2022 10:10
Get the 3 most recent placed order
@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());
}
@GayanM
GayanM / CheapestProduct.java
Created September 25, 2022 04:28
Ex-5: Get the cheapest products by category
@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()));
}
@GayanM
GayanM / ProductsByCustomerTier.java
Created September 25, 2022 04:22
Ex-4: Obtain a list of products based on customer tier and date range
@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;
}
@GayanM
GayanM / ProductResourceDiscount.java
Created September 25, 2022 04:16
Ex-3:Obtain a list of product with given category and then apply 10% discount
@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 -> {
@GayanM
GayanM / OrderResource.java
Created September 25, 2022 04:09
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())).
@GayanM
GayanM / ProductResource.java
Created September 21, 2022 07:36
Filter products by category and price range
@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;