Time for action – showing products based on category
Let's add a category view to the products page using the path variable:
Open the
ProductRepository
interface and add one more method declaration on itsgetProductsByCategory
method:List<Product> getProductsByCategory(String category);
Open the implementation class
InMemoryProductRepository
and add an implementation for the previously declared method as follows:public List<Product> getProductsByCategory(String category) { List<Product> productsByCategory = new ArrayList<Product>(); for(Product product: listOfProducts) { if(category.equalsIgnoreCase(product.getCategory())){ productsByCategory.add(product); } } return productsByCategory; }
Similarly, open the
ProductService
interface and add one more method declaration on itsgetProductsByCategory
method:List<Product> getProductsByCategory(String category);
Open the service implementation class
ProductServiceImpl
and add an implementation...