Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How do I display the number of clients that placed an order for a particular product?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 320
    Answer it

    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.

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

    MY CONTROLLER

     @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

    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

    <!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>
    

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: