From d484bab66fb40d1dfc15922276afb2da0f55379a Mon Sep 17 00:00:00 2001
From: Rhys Evans <EvansRM17@cardiff.ac.uk>
Date: Fri, 8 Dec 2023 17:47:02 +0000
Subject: [PATCH]  Further work on creating repositories for landamrk/location
 coordinates

---
 .../SmartTowns/data/LocationRepository.java   |  8 ++-
 .../data/LocationRepositoryJDBC.java          | 19 ++++++
 .../SmartTowns/data/LocationsCoordinates.java | 56 +++++++++++++++--
 .../data/LocationsCoordinatesRepository.java  | 12 ++++
 .../LocationsCoordinatesRepositoryJDBC.java   | 60 +++++++++++++++++++
 .../webpages/WebpageController.java           | 15 +++--
 src/main/resources/data.sql                   | 53 ++++++++--------
 src/main/resources/schema.sql                 | 28 ++++-----
 .../SmartTownsApplicationTests.java           | 10 ++--
 9 files changed, 197 insertions(+), 64 deletions(-)
 create mode 100644 src/main/java/Team5/SmartTowns/data/LocationsCoordinatesRepository.java
 create mode 100644 src/main/java/Team5/SmartTowns/data/LocationsCoordinatesRepositoryJDBC.java

diff --git a/src/main/java/Team5/SmartTowns/data/LocationRepository.java b/src/main/java/Team5/SmartTowns/data/LocationRepository.java
index 2a14910c..903db17c 100644
--- a/src/main/java/Team5/SmartTowns/data/LocationRepository.java
+++ b/src/main/java/Team5/SmartTowns/data/LocationRepository.java
@@ -8,10 +8,12 @@ public interface LocationRepository {
     List<Location> getAllLocation();
     void addLocation(Location loc);
 
-<<<<<<< HEAD:src/main/java/Team5/SmartTowns/Data/locationRepository.java
+
     List<Location> approvedLocations();
+    List<Location> unapprovedLocations();
+
+
 //     List<Location> approvedLocations();
 
-=======
->>>>>>> main:src/main/java/Team5/SmartTowns/data/LocationRepository.java
+
 }
diff --git a/src/main/java/Team5/SmartTowns/data/LocationRepositoryJDBC.java b/src/main/java/Team5/SmartTowns/data/LocationRepositoryJDBC.java
index f3e6c4a8..fcfaa676 100644
--- a/src/main/java/Team5/SmartTowns/data/LocationRepositoryJDBC.java
+++ b/src/main/java/Team5/SmartTowns/data/LocationRepositoryJDBC.java
@@ -51,5 +51,24 @@ public class LocationRepositoryJDBC implements LocationRepository {
         } return locationApprovalList;
  }
 
+    @Override
+    public List<Location> unapprovedLocations(){
+        List<Location> locations = getAllLocation();
+        List<Location> locationUnapprovedList= new ArrayList<Location>();
+        for (Location loc :locations){
+            if (!loc.isLocationApproved()) {
+                locationUnapprovedList.add(loc);
+            }
+        } return locationUnapprovedList;
+    }
+
+
 
+
+
+
+
+
+    public LocationRepositoryJDBC() {
+    }
 }
diff --git a/src/main/java/Team5/SmartTowns/data/LocationsCoordinates.java b/src/main/java/Team5/SmartTowns/data/LocationsCoordinates.java
index 1ed28ae7..ca12bf82 100644
--- a/src/main/java/Team5/SmartTowns/data/LocationsCoordinates.java
+++ b/src/main/java/Team5/SmartTowns/data/LocationsCoordinates.java
@@ -1,13 +1,61 @@
-package Team5.SmartTowns.Data;
+package Team5.SmartTowns.data;
 
 
 
 import lombok.AllArgsConstructor;
 import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.springframework.jdbc.core.JdbcTemplate;
+
 @Data
 @AllArgsConstructor
-public class LocationsCoordinates {
+@NoArgsConstructor
+public class LocationsCoordinates  extends LocationRepositoryJDBC  {
+    /// separate class to location to cover all locations within trails that have been approved and have long/lat coords attached for mapping.
     private int locationID;
-    private double locationCoordsLong;
-    private double locationCoordsLat;
+    private String locationCoordsLong;
+    private String locationCoordsLat;
+
+
+    public int getLocationID() {
+        return locationID;
+    }
+
+    public String getLocationCoordsLong() {
+        return locationCoordsLong;
+    }
+
+    public String getLocationCoordsLat() {
+        return locationCoordsLat;
+    }
+
+    public void setLocationCoordsLong(String locationCoordsLong) {
+        this.locationCoordsLong = locationCoordsLong;
+    }
+
+    public void setLocationCoordsLat(String locationCoordsLat) {
+        this.locationCoordsLat = locationCoordsLat;
+    }
+
+    public LocationsCoordinates(JdbcTemplate aJdbc, int locationID, String locationCoordsLong, String locationCoordsLat) {
+        super(aJdbc);
+        this.locationID = locationID;
+        this.locationCoordsLong = locationCoordsLong;
+        this.locationCoordsLat = locationCoordsLat;
+    }
+
+    public LocationsCoordinates(int locationID, String locationCoordsLong, String locationCoordsLat) {
+        this.locationID = locationID;
+        this.locationCoordsLong = locationCoordsLong;
+        this.locationCoordsLat = locationCoordsLat;
+    }
+
+
+ /// Need a constructor to create a lcoations list, approved lcoatiosn lsit, unapproved lcoations list.
+
+
+
+
+
+
 }
diff --git a/src/main/java/Team5/SmartTowns/data/LocationsCoordinatesRepository.java b/src/main/java/Team5/SmartTowns/data/LocationsCoordinatesRepository.java
new file mode 100644
index 00000000..acb4a503
--- /dev/null
+++ b/src/main/java/Team5/SmartTowns/data/LocationsCoordinatesRepository.java
@@ -0,0 +1,12 @@
+package Team5.SmartTowns.data;
+
+import java.util.List;
+
+public interface LocationsCoordinatesRepository {
+
+    List<LocationsCoordinates> getAllLocationCoords();
+    void addLocationCoord(LocationsCoordinates locCoord);
+
+
+
+}
diff --git a/src/main/java/Team5/SmartTowns/data/LocationsCoordinatesRepositoryJDBC.java b/src/main/java/Team5/SmartTowns/data/LocationsCoordinatesRepositoryJDBC.java
new file mode 100644
index 00000000..f05827bd
--- /dev/null
+++ b/src/main/java/Team5/SmartTowns/data/LocationsCoordinatesRepositoryJDBC.java
@@ -0,0 +1,60 @@
+package Team5.SmartTowns.data;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.RowMapper;
+
+import java.util.List;
+
+public class LocationsCoordinatesRepositoryJDBC extends LocationsCoordinatesRepository{
+
+
+    private JdbcTemplate jdbc;
+    private RowMapper<LocationsCoordinates> locationCoordMapper;
+
+    public LocationsCoordinatesRepositoryJDBC(JdbcTemplate aJdbc) {
+        this.jdbc = aJdbc;
+        setlocationCoordsMapper();
+    }
+    private void setlocationCoordsMapper(){
+        locationCoordMapper = (rs, i) -> new LocationsCoordinates(
+
+                rs.getInt("locationID"),
+                rs.getString("locationCoordsLong"),
+                rs.getString("locationCoordsLat")
+
+        );
+    }
+    public List<LocationsCoordinates> getAllLocation(){
+        String sql= "SELECT * FROM locationCoordinates";
+        return jdbc.query(sql, locationCoordMapper);
+    }
+
+    @Override
+    public void addLocation(LocationsCoordinates locCoords) {
+        String sql = "insert into locationCoordinates(locationID, locationCoordsLong,locationCoordsLat) values (?,?,?)";
+
+        jdbc.update(sql,locCoords.getLocationID(), locCoords.getLocationCoordsLong(),locCoords.getLocationCoordsLat());
+
+    }
+
+
+    //	private static JdbcTemplate jdbc;
+//	@BeforeAll
+//	public static void before() {
+//		location = new Location();
+//		locationRepo = new locationRepositoryJDBC(jdbc);}
+//	@BeforeAll
+//	public static void before2() {
+//		locationRepo = new locationRepositoryJDBC(jdbc); }
+	@Autowired
+	private locationRepository locationRepository;
+
+    public void approveLocationAndAddCoords(String town, String longCoords, String latCoords){ // need list too
+        List<Location> needApprovalList = new LocationsCoordinatesRepositoryJDBC.unapprovedLocations();
+        // unapproved list
+        // if matches name, approve
+        // add long/lat coords
+        //use to update table
+    }
+}
diff --git a/src/main/java/Team5/SmartTowns/webpages/WebpageController.java b/src/main/java/Team5/SmartTowns/webpages/WebpageController.java
index 069ed68e..dcc9a14a 100644
--- a/src/main/java/Team5/SmartTowns/webpages/WebpageController.java
+++ b/src/main/java/Team5/SmartTowns/webpages/WebpageController.java
@@ -1,7 +1,10 @@
 package Team5.SmartTowns.webpages;
 
 
+import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.servlet.ModelAndView;
 import org.springframework.stereotype.Controller;
 @Controller
@@ -23,7 +26,7 @@ public class WebpageController {
         ModelAndView modelAndView = new ModelAndView("Towns/risca");
         return modelAndView;
     }
-<<<<<<< HEAD:src/main/java/Team5/SmartTowns/Webpages/WebpageController.java
+
 
     @GetMapping("/maps")
     public ModelAndView getMapTestAPI(){
@@ -38,14 +41,10 @@ public class WebpageController {
 //    }
 
 
-    @RequestMapping(value="/test_ajax_frag", method=RequestMethod.POST)
-    public String sendHtmlFragment(Model map) {
-        //map.addAttribute("foo", "bar");
-        return "fragments/temp_frags.html :: trailInfo2";
+
     }
 
 
 
-=======
->>>>>>> main:src/main/java/Team5/SmartTowns/webpages/WebpageController.java
-}
+
+
diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql
index b94dc524..1cf13ce5 100644
--- a/src/main/resources/data.sql
+++ b/src/main/resources/data.sql
@@ -7,7 +7,7 @@ insert into trails ( Name,tru) value ( 'Caerphilly Coffee Trail',false);
 insert into trails ( Name,tru) value ( 'Penarth Dragon Trail',true);
 
 delete from locations;
-insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved)value ( 'St Cenydd','','Location description here','Caerphilly',0101, true);
+insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'St Cenydd','','Location description here','Caerphilly',0101, true);
 insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'The Castle','','Location description here','Caerphilly',0101, true);
 insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'Medieval Trades','','Location description here','Caerphilly',0101, true);
 insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'The Queen''s War','','Location description here','Caerphilly',0101, true);
@@ -25,30 +25,26 @@ insert into locations ( locationName , locationEmail,locationDescription,locatio
 insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'The Medieval Courthouse','','Location description here','Caerphilly',0103, true);
 insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ('Caerphilly Castle','','Location description here','Caerphilly',0103, true);
 insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'Ty Vaughan House','','Location description here','Caerphilly',0103, true);
-
 insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'Risca Colliery','','Location description here','Risca',0201, true);
 insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'Black Vein Colliery Disaster','','Location description here','Risca',0201, true);
+insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'The Esplanade','','Location description here','Penarth',0301, true);
+insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'The Old Swimming Baths','','Location description here','Penarth',0301, true);
+
 
+# delete from badges;
+# insert into badges (name, description, difficulty) value ('TownConnoisseur', 'You know the town very well!', '2');
+# insert into badges (name, description, difficulty) value ('TownRegular', 'You visited the town 3 days in a row!', '1');
+# insert into badges (name, description, difficulty) value ('TownMaster', 'You visited the town 7 days in a row!', '1');
+# insert into badges (name, description, difficulty) value ('TownRegular', 'You visited the town 3 days in a row!', '1');
+# insert into badges (name, description, difficulty) value ('TownRegular', 'You visited the town 3 days in a row!', '1');
 
-<<<<<<< HEAD
-insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value (19, 'The Esplanade','','Location description here','Penarth',0301, true);
-insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value (20, 'The Old Swimming Baths','','Location description here','Penarth',0301, true);
-delete from badges;
-insert into badges (name, description, difficulty) value ('TownConnoisseur', 'You know the town very well!', '2');
-insert into badges (name, description, difficulty) value ('TownRegular', 'You visited the town 3 days in a row!', '1');
-insert into badges (name, description, difficulty) value ('TownMaster', 'You visited the town 7 days in a row!', '1');
-insert into badges (name, description, difficulty) value ('TownRegular', 'You visited the town 3 days in a row!', '1');
-insert into badges (name, description, difficulty) value ('TownRegular', 'You visited the town 3 days in a row!', '1');
-=======
-insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (19, 'The Esplanade','','Location description here','Penarth',0301);
-insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (20, 'The Old Swimming Baths','','Location description here','Penarth',0301);
 
 
 delete from packs;
 insert into packs (name, description) value ('Wales Football Team', 'Pack of Welsh Football Players in the National Team');
 insert into packs (name, description) value ('Wales Rugby Team', 'Pack of Welsh Rugby Players in the National Team');
 insert into packs (name, description) value ('Welsh Heritage', 'Pack About Welsh Heritage');
->>>>>>> main
+
 
 delete from stickers;
 insert into stickers (packID, stickerID, name, description, rarity) value (1, 1, 'wayne_hennessey', 'Wales Football Team Player', '2');
@@ -65,21 +61,20 @@ insert into stickers (packID, stickerID, name, description, rarity) value (3, 1,
 insert into stickers (packID, stickerID, name, description, rarity) value (3, 2, 'Welsh Outline', 'Welsh Heritage', '1');
 insert into stickers (packID, stickerID, name, description, rarity) value (3, 3, 'Welsh Spoon', 'Welsh Heritage', '1');
 
-delete from stickerprogress;
-<<<<<<< HEAD
+# delete from stickerprogress;
 # insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '1', true);
 # insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '3', true);
 # insert into stickerprogress (userID, stickerID, hasSticker) value ('2', '2', true);
 
-delete from locationsCoordinates;
-insert into locationsCoordinates(locationID, locationCoordsLong, locationCoordsLat) value (2,51.57623,-3.21910 );
-insert into locationsCoordinates(locationID, locationCoordsLong, locationCoordsLat) value (3,51.575372, -3.219186 );
-insert into locationsCoordinates(locationID, locationCoordsLong, locationCoordsLat) value (4,51.576363, -3.220712 );
-=======
-insert into stickerprogress (userID, packID, stickerID) value (1, 1, 1);
-insert into stickerprogress (userID, packID, stickerID) value (1, 1, 2);
-insert into stickerprogress (userID, packID, stickerID) value (1, 1, 3);
-insert into stickerprogress (userID, packID, stickerID) value (1, 1, 5);
-insert into stickerprogress (userID, packID, stickerID) value (1, 2, 1);
-insert into stickerprogress (userID, packID, stickerID) value (1, 2, 3);
->>>>>>> main
+delete from locationCoordinates;
+insert into locationCoordinates(locationID, locationCoordsLong, locationCoordsLat) value (2,'51.57623','-3.21910' );
+insert into locationCoordinates(locationID, locationCoordsLong, locationCoordsLat) value (3,'51.575372','-3.219186');
+insert into locationCoordinates(locationID, locationCoordsLong, locationCoordsLat) value (4, '51.576363','-3.220712' );
+#
+# insert into stickerprogress (userID, packID, stickerID) value (1, 1, 1);
+# insert into stickerprogress (userID, packID, stickerID) value (1, 1, 2);
+# insert into stickerprogress (userID, packID, stickerID) value (1, 1, 3);
+# insert into stickerprogress (userID, packID, stickerID) value (1, 1, 5);
+# insert into stickerprogress (userID, packID, stickerID) value (1, 2, 1);
+# insert into stickerprogress (userID, packID, stickerID) value (1, 2, 3);
+
diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql
index 0efcdd4c..854e13dc 100644
--- a/src/main/resources/schema.sql
+++ b/src/main/resources/schema.sql
@@ -6,6 +6,7 @@ create table if not exists trails
     tru boolean
 )   engine=InnoDB;
 
+drop table if exists locationCoordinates;
 drop table if exists locations;
 create table if not exists locations
 (
@@ -59,27 +60,24 @@ create table if not exists stickerProgress
 (
     id bigint auto_increment primary key,
     userID bigint,
-<<<<<<< HEAD
-    stickerID bigint,
-    hasSticker boolean /*Has sticker or not*/
+    stickerID bigint
 ) engine=InnoDB;
 
 
 
 
-drop table if exists locationsCoordinates;
-create table if not exists locationsCoordinates
-(locationCoordID bigint auto_increment primary key,
- locationID bigint,
- locationCoordsLong double,
- locationCoordsLat double,
- Foreign Key (locationID) REFERENCES locations(locationID)
+
+create table if not exists locationCoordinates
+(
+    locationCoordID bigint auto_increment primary key,
+    locationID bigint,
+    Foreign Key (locationID) REFERENCES locations(locationID)
+        ON DELETE CASCADE
+        ON UPDATE RESTRICT,
+    locationCoordsLong varchar(128),
+    locationCoordsLat varchar(128)
 
 
 )engine=InnoDB;
-=======
-    packID bigint,
-    stickerID bigint
 
-) engine=InnoDB;
->>>>>>> main
+
diff --git a/src/test/java/Team5/SmartTowns/SmartTownsApplicationTests.java b/src/test/java/Team5/SmartTowns/SmartTownsApplicationTests.java
index 3589f884..816e5bea 100644
--- a/src/test/java/Team5/SmartTowns/SmartTownsApplicationTests.java
+++ b/src/test/java/Team5/SmartTowns/SmartTownsApplicationTests.java
@@ -1,10 +1,10 @@
 package Team5.SmartTowns;
 
-import Team5.SmartTowns.Data.DatabaseController;
-import Team5.SmartTowns.Data.Location;
-import Team5.SmartTowns.Data.locationRepository;
-import Team5.SmartTowns.Data.locationRepositoryJDBC;
-import Team5.SmartTowns.Landmarks.Landmarks;
+import Team5.SmartTowns.data.DatabaseController;
+import Team5.SmartTowns.data.Location;
+import Team5.SmartTowns.data.LocationRepository;
+import Team5.SmartTowns.data.LocationRepositoryJDBC;
+import Team5.SmartTowns.landmarks.Landmarks;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 import org.mockito.InjectMocks;
-- 
GitLab