From 8d75ddbadf6d488740da67fdfc7af357ac46e147 Mon Sep 17 00:00:00 2001 From: Rhys Evans <EvansRM17@cardiff.ac.uk> Date: Fri, 1 Dec 2023 22:19:02 +0000 Subject: [PATCH] integrated location/landamrk table with landamrk signup form todo: lcoationDEescription constricted by varchar(250), find better way to implement large text --- .../java/Team5/SmartTowns/Data/location.java | 52 +++++++++++++++++-- .../SmartTowns/Data/locationRepository.java | 3 ++ .../Data/locationRepositoryJDBC.java | 31 ++++++++--- .../Landmarks/LandmarksController.java | 12 ++++- src/main/resources/schema.sql | 2 +- 5 files changed, 88 insertions(+), 12 deletions(-) diff --git a/src/main/java/Team5/SmartTowns/Data/location.java b/src/main/java/Team5/SmartTowns/Data/location.java index b882b5bf..268babdb 100644 --- a/src/main/java/Team5/SmartTowns/Data/location.java +++ b/src/main/java/Team5/SmartTowns/Data/location.java @@ -11,10 +11,54 @@ import lombok.Data; public class location { private int locationID; private String locationName; -// private String locationEmail; -// private String locationDescription; -// private String locationPlace; //todo revert this -// private int locationTrailID; + private String locationEmail; + private String locationDescription; + private String locationPlace; + private int locationTrailID; + @Override + public String toString() { + return "location{" + + "" + locationID + + ", '" + locationName + '\'' + + ", '" + locationEmail + '\'' + + ", '" + locationDescription + '\'' + + ", '" + locationPlace + '\'' + + ", " + locationTrailID + + '}'; + } + + public int getLocationID() { + return locationID; + } + + public String getLocationName() { + return locationName; + } + + public String getLocationEmail() { + return locationEmail; + } + + public String getLocationDescription() { + return locationDescription; + } + + public String getLocationPlace() { + return locationPlace; + } + + public int getLocationTrailID() { + return locationTrailID; + } + +// public location(int locationID, String locationName, String locationEmail, String locationDescription, String locationPlace, int locationTrailID) { +// this.locationID = locationID; +// this.locationName = locationName; +// this.locationEmail = locationEmail; +// this.locationDescription = locationDescription; +// this.locationPlace = locationPlace; +// this.locationTrailID = locationTrailID; +// } } diff --git a/src/main/java/Team5/SmartTowns/Data/locationRepository.java b/src/main/java/Team5/SmartTowns/Data/locationRepository.java index a1694978..f09d1f64 100644 --- a/src/main/java/Team5/SmartTowns/Data/locationRepository.java +++ b/src/main/java/Team5/SmartTowns/Data/locationRepository.java @@ -1,11 +1,14 @@ //Holds locations data repository (landmarks) package Team5.SmartTowns.Data; +import Team5.SmartTowns.Landmarks.Landmarks; + import java.util.List; public interface locationRepository { List<location> getAllLocation(); + void addLocation(location llocation); } diff --git a/src/main/java/Team5/SmartTowns/Data/locationRepositoryJDBC.java b/src/main/java/Team5/SmartTowns/Data/locationRepositoryJDBC.java index fd3c92b6..5870dd60 100644 --- a/src/main/java/Team5/SmartTowns/Data/locationRepositoryJDBC.java +++ b/src/main/java/Team5/SmartTowns/Data/locationRepositoryJDBC.java @@ -1,10 +1,12 @@ //Implements the locations repository using JDBC package Team5.SmartTowns.Data; +import Team5.SmartTowns.Landmarks.Landmarks; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; +import java.sql.SQLException; import java.util.List; @Repository @@ -19,15 +21,32 @@ public class locationRepositoryJDBC implements locationRepository { private void setlocationMapper(){ locationMapper = (rs, i) -> new location( rs.getInt("locationID"), - rs.getString("locationName") -// rs.getString("locationEmail"), -// rs.getString("locationDescription"), -// rs.getString("locationPlace"), //todo revert this -// rs.getInt("locationTrailID") + rs.getString("locationName"), + rs.getString("locationEmail"), + rs.getString("locationDescription"), + rs.getString("locationPlace"), + rs.getInt("locationTrailID") ); } public List<location> getAllLocation(){ - String sql= "SELECT locationID,locationName FROM locations"; + String sql= "SELECT * FROM locations"; return jdbc.query(sql, locationMapper); } + + @Override + public void addLocation(location llocation) { + String sql = "insert into locations(locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) values (?,?,?,?,?,?)"; + + jdbc.update(sql, llocation.getLocationID(),llocation.getLocationName(),llocation.getLocationEmail(),llocation.getLocationDescription(),llocation.getLocationPlace(),llocation.getLocationTrailID()); + } + +// public void insertLocation(Landmarks landmark){ +// String sql = "INSERT INTO locations(locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID)"+"VALUES("+ landmark.getLandmarkID()+","+","+landmark.getLandmarkName()+","+landmark.getLandmarkEmail()+","+landmark.getLandmarkDescription()+","+landmark.getLandmarkLocation()+","+landmark.getTrailID()+")"; +// try{ +// executeUpdate(sql); +// } +// catch(SQLException e){ +// +// } +// } } diff --git a/src/main/java/Team5/SmartTowns/Landmarks/LandmarksController.java b/src/main/java/Team5/SmartTowns/Landmarks/LandmarksController.java index 523a807a..0efe043a 100644 --- a/src/main/java/Team5/SmartTowns/Landmarks/LandmarksController.java +++ b/src/main/java/Team5/SmartTowns/Landmarks/LandmarksController.java @@ -1,6 +1,9 @@ package Team5.SmartTowns.Landmarks; +import Team5.SmartTowns.Data.location; +import Team5.SmartTowns.Data.locationRepository; import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; @@ -8,6 +11,8 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.servlet.ModelAndView; + +import java.sql.PreparedStatement; //import jakarta.validation.Valid; @Controller @@ -22,7 +27,8 @@ public class LandmarksController { } - + @Autowired + private locationRepository locationRepository; @PostMapping("/landmarkSub") public ModelAndView landmarkSent(@Valid @ModelAttribute("landmarkData") Landmarks landmarks, BindingResult bindingResult, Model model ) { @@ -33,6 +39,10 @@ public class LandmarksController { } else{ System.out.println(landmarks); + location loc= new location(landmarks.getLandmarkID(),landmarks.getLandmarkName(), landmarks.getLandmarkEmail(), landmarks.getLandmarkDescription(), landmarks.getLandmarkLocation(), landmarks.getTrailID()); + locationRepository.addLocation(loc); + + diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index ecbbaf28..ff2e51f3 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -11,7 +11,7 @@ create table if not exists locations locationID bigint auto_increment primary key, locationName varchar(128), locationEmail varchar(128), - locationDescription varchar(255), + locationDescription varchar(max), locationPlace varchar(255), locationTrailID varchar(128) ) engine=InnoDB; -- GitLab