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
}
0 Answer(s)