mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 13:05:49 +00:00
Added feature to count total owners and display on a new page
This commit is contained in:
parent
cefaf55dd1
commit
b7932cead7
3 changed files with 20 additions and 0 deletions
|
@ -169,5 +169,11 @@ class OwnerController {
|
||||||
mav.addObject(owner);
|
mav.addObject(owner);
|
||||||
return mav;
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,4 +62,7 @@ public interface OwnerRepository extends JpaRepository<Owner, Integer> {
|
||||||
*/
|
*/
|
||||||
Optional<Owner> findById(@Nonnull Integer id);
|
Optional<Owner> findById(@Nonnull Integer id);
|
||||||
|
|
||||||
|
@Query("SELECT COUNT(o) FROM Owner o")
|
||||||
|
int countOwners();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
11
src/main/resources/templates/owners/ownerCount.html
Normal file
11
src/main/resources/templates/owners/ownerCount.html
Normal 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>
|
||||||
|
|
Loading…
Reference in a new issue