Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I display the number of clients that placed an order for a particular product?

I'm a beginner in java and I want to display the number of clients that placed an order for a particular product on my MVC WebApp. I came up with a logical way of doing this but I stumbled upon a dead end.

What I have tried:

Here's what I tried to do. Any help would be greatly appreciated.

MY CONTROLLER

Java
@GetMapping("/produtos-crud/produtos-da-categoria-admin/{id}")
 public String Home15(Model model,  @PathVariable("id") int id, HttpServletRequest request, @Valid User user) {
            
    HttpSession session = request.getSession(false);
    if (session != null) {
        for(User user_:userRepository.verify(session.getAttribute("email"), 
        session.getAttribute("password"))) {
                
      model.addAttribute("num_clients",orderRepository.countClientById(?)); //'?' <- All I need is this value
    





                //These codes are not relevant. Please ignore.
                //For originality purpose
                model.addAttribute("num", productRepository.countProductById(id) + " Produtos");
                model.addAttribute("users_cat", categoryRepository.findById(id));
                model.addAttribute("name", user_.getName());
                model.addAttribute("users", productRepository.findProductByCategoryId(id));
                
                }
            }
            else {
                
                return "redirect:/index";
                
            }
            
            
            return "/produtos-crud/produtos-da-categoria-admin";

MY REPOSITORY

Java
public interface OrderRepository extends CrudRepository<Pedido, Integer> {

@Query(value = "SELECT COUNT(client) FROM order WHERE product = ?;", nativeQuery = true)
    int countClientById(int ?); //Feel free to set a variable

}

MY HTML FILE

HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Category Items</title>
<meta content="Dashboard" property="og:title">
<meta content="Dashboard" property="twitter:title">
<meta content="width=device-width, initial-scale=1" name="viewport">
<meta content="Webflow" name="generator">
<link href="css/normalize.css" rel="stylesheet" type="text/css">
<link href="css/webflow.css" rel="stylesheet" type="text/css">
<link href="css/ui-login-teste.webflow.css" rel="stylesheet" type="text/css">
</head>

<body>

   <!-- Irrelevant codes have been removed for to shorten code--> 

  <div class="valida-table-field estatuto">
     <div th:text="${num_clients}">14</div> <!-- Main focus -->
  </div>

</body>
</html>
Posted
Updated 9-Feb-21 22:13pm
Comments
CHill60 9-Feb-21 7:49am    
Define "a dead end" - what actually happened (or didn't happen that you wanted to)
El,sha Destino 9-Feb-21 8:55am    
well, I cant seem to find a logical way to set the "value" required in My Controller. @CHill60

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900