Added feature to count total owners and display on a new page

This commit is contained in:
namaskruti pal 2025-06-10 12:46:13 +05:30
parent cefaf55dd1
commit b7932cead7
3 changed files with 20 additions and 0 deletions

View file

@ -169,5 +169,11 @@ class OwnerController {
mav.addObject(owner);
return mav;
}
@GetMapping("/owners/count")
public String countOwners(Model model) {
int count = owners.countOwners();
model.addAttribute("count", count);
return "owners/ownerCount"; // we'll create this HTML view next
}
}

View file

@ -62,4 +62,7 @@ public interface OwnerRepository extends JpaRepository<Owner, Integer> {
*/
Optional<Owner> findById(@Nonnull Integer id);
@Query("SELECT COUNT(o) FROM Owner o")
int countOwners();
}

View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Owner Count</title>
</head>
<body>
<h2>Total number of owners: <span th:text="${count}">0</span></h2>
<a href="/owners">Back to Owners List</a>
</body>
</html>