Spring MVC: Adding multiple filters to list

1.9k Views Asked by At

I'm doing an exercise on my own from Spring MVC just for the sake of learning it, and I'm stuck on a task where I have an url address by which I am asked to filter products in a imaginary web-store.

I want to apply multiple criteria to view a desired product; for example a product that falls under the tablet category, is within the price range of $200 to $400, and has been manufactured by Google.

The url is: .../webstore/products/tablet/price;low=200;high=400?manufacturer="Google"

The task is to create a request mapping method called filterProducts in the productController class to map this URL. The URL contains the matrix variables low an high to represent the price range, the GET parameter manufacturer to identify the manufacturer and finally a URI template path variable tablet to represent the category.

There is a hint that I might use java.util.Set to combine the results to avoid duplication.

Here is my incomplete code: (I do not know how to solve the problem)

@RequestMapping("/{category}/{byCriteria}")
public String getProductsByManufacturer(
        @PathVariable("category") String productCategory,
        @MatrixVariable(pathVar = "byCriteria") Map<String, List<String>> filterParams,
        @RequestParam("manufactuer") String manufacturer, Model model) {


    List<Product> productsCategory = productService.getProductsByCategory(productCategory);

// ???

    model.addAttribute("product", ??);

    return "products";
}
1

There are 1 best solutions below

0
Eduardo Ortiz On

I know that exercise. Can you try:

@RequestMapping("/{category}/{price}")
public String filterProducts(@PathVariable("category") String category,
                             @MatrixVariable(pathVar = "price") Map<String, List<String>> priceParams,
                             @RequestParam("manufacturer") String manufacturer,
                             Model model) {
    Set<Product> filteredProducts = new HashSet<Product>();

    List<Product> productsByCategory = productService.getProductsByCategory(category);
    List<Product> productsByManufacturer = productService.getProductsByManufacturer(manufacturer);
    Set<Product> productsByPrice = new HashSet<Product>();

    BigDecimal low = new BigDecimal(priceParams.get("low").get(0));
    BigDecimal high = new BigDecimal(priceParams.get("high").get(0));
    productsByPrice.addAll(productService.getProductsByPrice(low, high));

    for(Product categoryProduct: productsByCategory) {
        for(Product manufacturerProduct: productsByManufacturer) {
            for(Product priceProduct: productsByPrice) {
                if(priceProduct.equals(manufacturerProduct) && manufacturerProduct.equals(categoryProduct)) {
                    filteredProducts.add(priceProduct);
                }
            }
        }
    }

    model.addAttribute("products", filteredProducts);

    return "products";
}

Don't forget to do the method getProductsByPrice(low, high)) on the ProductRepository and Impl. I hope you have served.