mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-26 01:15:50 +00:00
dashboard: number of owners and number of cities
This commit is contained in:
parent
6f4c49a95e
commit
3dc55645e7
4 changed files with 70 additions and 1 deletions
|
@ -0,0 +1,29 @@
|
|||
@Controller
|
||||
public class DashboardController {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public DashboardController(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
@GetMapping("/dashboard")
|
||||
public ModelAndView showDashboard() {
|
||||
ModelAndView mav = new ModelAndView("dashboard");
|
||||
|
||||
// Get list of all cities
|
||||
List<String> cities = jdbcTemplate.queryForList("SELECT DISTINCT city FROM owners ORDER BY city ASC", String.class);
|
||||
mav.addObject("cities", cities);
|
||||
|
||||
// Get number of owners in each city
|
||||
Map<String, Integer> ownersByCity = new HashMap<>();
|
||||
for (String city : cities) {
|
||||
int count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM owners WHERE city=?", Integer.class, city);
|
||||
ownersByCity.put(city, count);
|
||||
}
|
||||
mav.addObject("ownersByCity", ownersByCity);
|
||||
|
||||
return mav;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<!DOCTYPE html>
|
||||
<html xmlns:th="https://www.thymeleaf.org" th:replace="~{fragments/layout :: layout (~{::body},'dashboard')}">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Dashboard</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Dashboard</h1>
|
||||
<p>Number of cities in the database: <b th:text="${numCities}"></b></p>
|
||||
<h2>Cities:</h2>
|
||||
<ul>
|
||||
<li th:each="city : ${cities}">
|
||||
<a th:href="@{/dashboard/owners(city=${city.name})}" th:text="${city.name}"></a>
|
||||
</li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -49,7 +49,7 @@
|
|||
<span class="fa fa-home" aria-hidden="true"></span>
|
||||
<span>Home</span>
|
||||
</li>
|
||||
|
||||
|
||||
<li th:replace="~{::menuItem ('/owners/find','owners','find owners','search','Find owners')}">
|
||||
<span class="fa fa-search" aria-hidden="true"></span>
|
||||
<span>Find owners</span>
|
||||
|
@ -60,6 +60,11 @@
|
|||
<span>Veterinarians</span>
|
||||
</li>
|
||||
|
||||
<li th:replace="~{::menuItem ('/dashboard/dashboard.html','dashboard','dashboard','dashboard','DashBoard')}">
|
||||
<span aria-hidden="true"></span>
|
||||
<span>Dashboard</span>
|
||||
|
||||
</li>
|
||||
<li
|
||||
th:replace="~{::menuItem ('/oups','error','trigger a RuntimeException to see how it is handled','exclamation-triangle','Error')}">
|
||||
<span class="fa exclamation-triangle" aria-hidden="true"></span>
|
||||
|
|
17
src/main/resources/templates/owners_by_city.html
Normal file
17
src/main/resources/templates/owners_by_city.html
Normal file
|
@ -0,0 +1,17 @@
|
|||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Owners in [[${city}]]</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Owners in [[${city}]]</h1>
|
||||
<p>Number of owners: <b th:text="${numOwners}"></b></p>
|
||||
<h2>Owners:</h2>
|
||||
<ul>
|
||||
<li th:each="owner : ${owners}">
|
||||
<p th:text="${owner.firstName} + ' ' + ${owner.lastName}"></p>
|
||||
</li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue