From 264db9d2545dcd4129bc354f8c8e73417b903e11 Mon Sep 17 00:00:00 2001
From: Rhys Nute <nuterd@cardiff.ac.uk>
Date: Thu, 30 Nov 2023 21:41:49 +0000
Subject: [PATCH] finished database

---
 .../SmartTowns/Data/DatabaseController.java   | 22 ++++++++-----------
 .../java/Team5/SmartTowns/Data/location.java  |  2 +-
 .../SmartTowns/Data/locationRepository.java   |  2 +-
 .../Data/locationRepositoryJDBC.java          |  6 ++---
 .../SmartTowns/Data/trailsRepositoryJDBC.java |  4 ++--
 src/main/resources/data.sql                   |  6 ++---
 src/main/resources/schema.sql                 |  8 +++----
 .../templates/towns/locationData.html         |  4 ++--
 8 files changed, 25 insertions(+), 29 deletions(-)

diff --git a/src/main/java/Team5/SmartTowns/Data/DatabaseController.java b/src/main/java/Team5/SmartTowns/Data/DatabaseController.java
index 55fa1a54..b91d7d88 100644
--- a/src/main/java/Team5/SmartTowns/Data/DatabaseController.java
+++ b/src/main/java/Team5/SmartTowns/Data/DatabaseController.java
@@ -11,15 +11,11 @@ public class DatabaseController {
 
     @Autowired
     private UserRepository userRepository;
-//    @Autowired
-//    private locationRepository locationRepository;
+    @Autowired
+    private locationRepository locationRepository;
     @Autowired
     private trailsRepository trailsRepository;
 
-//    public DatabaseController() {
-//
-//    }
-
     @GetMapping("/userList")
     public ModelAndView userList() {
         ModelAndView mav = new ModelAndView("towns/userData");
@@ -34,11 +30,11 @@ public class DatabaseController {
         mav1.addObject("trails", trail);
         return mav1;
     }
-//    @GetMapping("locationList")
-//    public ModelAndView locationList(){
-//        ModelAndView mav2 = new ModelAndView("towns/locationData");
-//        List<location> location = locationRepository.getAllLocations();
-//        mav2.addObject("location", location);
-//        return mav2;
-//    }
+    @GetMapping("locationList")
+    public ModelAndView locationList(){
+        ModelAndView mav2 = new ModelAndView("towns/locationData");
+        List<location> locations = locationRepository.getAllLocation();
+        mav2.addObject("location", locations);
+        return mav2;
+    }
 }
diff --git a/src/main/java/Team5/SmartTowns/Data/location.java b/src/main/java/Team5/SmartTowns/Data/location.java
index 03fff518..827dabdb 100644
--- a/src/main/java/Team5/SmartTowns/Data/location.java
+++ b/src/main/java/Team5/SmartTowns/Data/location.java
@@ -6,6 +6,6 @@ import lombok.Data;
 @Data
 @AllArgsConstructor
 public class location {
-    private int locationId;
+    private int locationID;
     private String name;
 }
diff --git a/src/main/java/Team5/SmartTowns/Data/locationRepository.java b/src/main/java/Team5/SmartTowns/Data/locationRepository.java
index ce30cda9..cce73286 100644
--- a/src/main/java/Team5/SmartTowns/Data/locationRepository.java
+++ b/src/main/java/Team5/SmartTowns/Data/locationRepository.java
@@ -3,7 +3,7 @@ package Team5.SmartTowns.Data;
 import java.util.List;
 
 public interface locationRepository {
-    List<location> getAllLocations();
+    List<location> getAllLocation();
 
 
 }
diff --git a/src/main/java/Team5/SmartTowns/Data/locationRepositoryJDBC.java b/src/main/java/Team5/SmartTowns/Data/locationRepositoryJDBC.java
index 4743ea8b..f72787ed 100644
--- a/src/main/java/Team5/SmartTowns/Data/locationRepositoryJDBC.java
+++ b/src/main/java/Team5/SmartTowns/Data/locationRepositoryJDBC.java
@@ -7,7 +7,7 @@ import org.springframework.stereotype.Repository;
 import java.util.List;
 
 @Repository
-public abstract class locationRepositoryJDBC implements locationRepository {
+public class locationRepositoryJDBC implements locationRepository {
     private JdbcTemplate jdbc;
     private RowMapper<location> locationMapper;
 
@@ -17,12 +17,12 @@ public abstract class locationRepositoryJDBC implements locationRepository {
     }
     private void setlocationMapper(){
         locationMapper = (rs, i) -> new location(
-                rs.getInt("locationId"),
+                rs.getInt("locationID"),
                 rs.getString("name")
         );
     }
     public List<location> getAllLocation(){
-        String sql= "SELECT * FROM location";
+        String sql= "SELECT * FROM locations";
         return jdbc.query(sql, locationMapper);
     }
 }
diff --git a/src/main/java/Team5/SmartTowns/Data/trailsRepositoryJDBC.java b/src/main/java/Team5/SmartTowns/Data/trailsRepositoryJDBC.java
index 40ce5ff0..eb26156c 100644
--- a/src/main/java/Team5/SmartTowns/Data/trailsRepositoryJDBC.java
+++ b/src/main/java/Team5/SmartTowns/Data/trailsRepositoryJDBC.java
@@ -7,7 +7,7 @@ import org.springframework.stereotype.Repository;
 import java.util.List;
 
 @Repository
-public class trailsRepositoryJDBC {
+public class trailsRepositoryJDBC implements trailsRepository{
     private JdbcTemplate jdbc;
     private RowMapper<trail> trailMapper;
     public trailsRepositoryJDBC(JdbcTemplate aJdbc){
@@ -16,7 +16,7 @@ public class trailsRepositoryJDBC {
     }
     private void settrailsMapper(){
         trailMapper = (rs, i) -> new trail(
-                rs.getInt("trailsId"),
+                rs.getInt("trailID"),
                 rs.getString("name")
         );
     }
diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql
index 7f91a409..f7656769 100644
--- a/src/main/resources/data.sql
+++ b/src/main/resources/data.sql
@@ -6,6 +6,6 @@ delete from trails;
 insert into trails (trailID, Name) value ('1', 'Caerphilly Coffee Trail');
 insert into trails (trailID, Name) value ('2', 'Penarth Dragon Trail');
 
-delete from location;
-insert into location (locationID, Name) value ('1', 'Caerphilly');
-insert into location (locationID, Name) value ('2', 'Penarth');
\ No newline at end of file
+delete from locations;
+insert into locations (locationID, Name) value ('1', 'Caerphilly');
+insert into locations (locationID, Name) value ('2', 'Penarth');
\ No newline at end of file
diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql
index 0374c292..c10ffb81 100644
--- a/src/main/resources/schema.sql
+++ b/src/main/resources/schema.sql
@@ -1,10 +1,10 @@
-drop table if exists trails;
-create table if not exists trails(
+drop table if exists trail;
+create table if not exists trail(
     trailID bigint auto_increment primary key,
     name varchar(100)
 ) engine=InnoDB;
-drop table if exists location;
-create table if not exists location(
+drop table if exists locations;
+create table if not exists locations(
 locationID bigint auto_increment primary key,
 name varchar(100)
 ) engine=InnoDB;
diff --git a/src/main/resources/templates/towns/locationData.html b/src/main/resources/templates/towns/locationData.html
index 1ff28922..f7636f05 100644
--- a/src/main/resources/templates/towns/locationData.html
+++ b/src/main/resources/templates/towns/locationData.html
@@ -6,8 +6,8 @@
 </head>
 <body>
 <div>
-    <ul th:each="location:${locations}">
-        <li th:text="${location}"></li>
+    <ul th:each="locations:${location}">
+        <li th:text="${locations}"></li>
     </ul>
 </div>
 </body>
-- 
GitLab