diff --git a/src/main/java/Team5/SmartTowns/Data/location.java b/src/main/java/Team5/SmartTowns/Data/location.java index b882b5bfd1d642a69a9dd477933bcbf38f2755cf..268babdb19cd128482a9f2cea4a49bfe5123bcd1 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 a169497813c9286e94d65657f43bd8d5ee81e509..f09d1f64f49c06d488087d58192346b480696579 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 fd3c92b6b3de68d35a6822210263f14bbca9fa1c..5870dd6035161a9ddd8590f3cdea1b14c9930a7b 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 523a807a6323ffc65531baba2351af53efcbcd47..0efe043a724e2a38de7dec408b28b6c0a48e8f95 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 ecbbaf28daf1c47b257b29f544df7d1b43c01b04..ff2e51f3cfcf09e293eb0081cae7ae5615f2efd0 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;