From 3dc55645e75554bf51d345575296042092f90f19 Mon Sep 17 00:00:00 2001 From: tenzin metok Date: Thu, 6 Apr 2023 13:39:32 +0530 Subject: [PATCH] dashboard: number of owners and number of cities --- .../petclinic/ DashboardController.java | 29 +++++++++++++++++++ .../fragments/dashboard/dashboard.html | 18 ++++++++++++ .../resources/templates/fragments/layout.html | 7 ++++- .../resources/templates/owners_by_city.html | 17 +++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/ DashboardController.java create mode 100644 src/main/resources/templates/fragments/dashboard/dashboard.html create mode 100644 src/main/resources/templates/owners_by_city.html diff --git a/src/main/java/org/springframework/samples/petclinic/ DashboardController.java b/src/main/java/org/springframework/samples/petclinic/ DashboardController.java new file mode 100644 index 000000000..245f1ec99 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/ DashboardController.java @@ -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 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 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; + } + +} \ No newline at end of file diff --git a/src/main/resources/templates/fragments/dashboard/dashboard.html b/src/main/resources/templates/fragments/dashboard/dashboard.html new file mode 100644 index 000000000..ad01f087b --- /dev/null +++ b/src/main/resources/templates/fragments/dashboard/dashboard.html @@ -0,0 +1,18 @@ + + + + + Dashboard + + +

Dashboard

+

Number of cities in the database:

+

Cities:

+
    +
  • + +
  • +
+ + + diff --git a/src/main/resources/templates/fragments/layout.html b/src/main/resources/templates/fragments/layout.html index f58a18c5d..bbd8e036c 100755 --- a/src/main/resources/templates/fragments/layout.html +++ b/src/main/resources/templates/fragments/layout.html @@ -49,7 +49,7 @@ Home - +
  • Find owners @@ -60,6 +60,11 @@ Veterinarians
  • +
  • + + Dashboard + +
  • diff --git a/src/main/resources/templates/owners_by_city.html b/src/main/resources/templates/owners_by_city.html new file mode 100644 index 000000000..96e5cb6ab --- /dev/null +++ b/src/main/resources/templates/owners_by_city.html @@ -0,0 +1,17 @@ + + + + + Owners in [[${city}]] + + +

    Owners in [[${city}]]

    +

    Number of owners:

    +

    Owners:

    +
      +
    • +

      +
    • +
    + +