diff --git a/src/main/java/Team5/SmartTowns/Data/DatabaseController.java b/src/main/java/Team5/SmartTowns/Data/DatabaseController.java index b91d7d880368f6cb405e3ed71c38242f612bfae2..0eed9aca6601fa11eab58684cbf54bae225d5a35 100644 --- a/src/main/java/Team5/SmartTowns/Data/DatabaseController.java +++ b/src/main/java/Team5/SmartTowns/Data/DatabaseController.java @@ -33,8 +33,8 @@ public class DatabaseController { @GetMapping("locationList") public ModelAndView locationList(){ ModelAndView mav2 = new ModelAndView("towns/locationData"); - List<location> locations = locationRepository.getAllLocation(); - mav2.addObject("location", locations); + 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 new file mode 100644 index 0000000000000000000000000000000000000000..01ba2932c6b7d0a6201aa8128161bf7ad8b27526 --- /dev/null +++ b/src/main/java/Team5/SmartTowns/Data/Location.java @@ -0,0 +1,48 @@ +//Holds variable data for the locations table (landmarks) +package Team5.SmartTowns.Data; + +import lombok.AllArgsConstructor; +import lombok.Data; +@Data +@AllArgsConstructor +public class Location { +// private int locationID; + private String locationName; + private String locationEmail; + private String locationDescription; + private String locationPlace; + private int locationTrailID; + + @Override + public String toString() { + return "Location{" + + locationName + '\'' + + locationEmail + '\'' + + locationDescription + '\'' + + locationPlace + '\'' + + locationTrailID + + '}'; + } + + 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; + } + + +} diff --git a/src/main/java/Team5/SmartTowns/Data/location.java b/src/main/java/Team5/SmartTowns/Data/location.java deleted file mode 100644 index f74adb71039eb1a254e379e56a16ca16d7e3cf32..0000000000000000000000000000000000000000 --- a/src/main/java/Team5/SmartTowns/Data/location.java +++ /dev/null @@ -1,12 +0,0 @@ -//Holds variable data for the locations table -package Team5.SmartTowns.Data; - -import lombok.AllArgsConstructor; -import lombok.Data; - -@Data -@AllArgsConstructor -public class location { - 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 772f335edeb43376b96a4a2045ceddbbb8bf9690..822a18cb860654694734c7f591cbb5b216eb8a39 100644 --- a/src/main/java/Team5/SmartTowns/Data/locationRepository.java +++ b/src/main/java/Team5/SmartTowns/Data/locationRepository.java @@ -1,10 +1,12 @@ -//Holds locations data repository +//Holds locations data repository (landmarks) package Team5.SmartTowns.Data; import java.util.List; + public interface locationRepository { - List<location> getAllLocation(); + List<Location> getAllLocation(); + void addLocation(Location loc); } diff --git a/src/main/java/Team5/SmartTowns/Data/locationRepositoryJDBC.java b/src/main/java/Team5/SmartTowns/Data/locationRepositoryJDBC.java index b11671ee54d102354431f6917b297ba179f388d9..a5e79d4442d540f5dba67ee7f2b6217984044ebb 100644 --- a/src/main/java/Team5/SmartTowns/Data/locationRepositoryJDBC.java +++ b/src/main/java/Team5/SmartTowns/Data/locationRepositoryJDBC.java @@ -10,20 +10,33 @@ import java.util.List; @Repository public class locationRepositoryJDBC implements locationRepository { private JdbcTemplate jdbc; - private RowMapper<location> locationMapper; + private RowMapper<Location> locationMapper; public locationRepositoryJDBC(JdbcTemplate aJdbc) { this.jdbc = aJdbc; setlocationMapper(); } private void setlocationMapper(){ - locationMapper = (rs, i) -> new location( - rs.getInt("locationID"), - rs.getString("name") + locationMapper = (rs, i) -> new Location( + + rs.getString("locationName"), + rs.getString("locationEmail"), + rs.getString("locationDescription"), + rs.getString("locationPlace"), + rs.getInt("locationTrailID") ); } - public List<location> getAllLocation(){ + public List<Location> getAllLocation(){ String sql= "SELECT * FROM locations"; return jdbc.query(sql, locationMapper); } + + @Override // intended implementation at current: user data from templates/Landmarks/LandmarkFormTh.html is added to the Location table + public void addLocation(Location loc) { + String sql = "insert into locations( locationName , locationEmail,locationDescription,locationPlace, locationTrailID) values (?,?,?,?,?)"; + + jdbc.update(sql,loc.getLocationName(),loc.getLocationEmail(),loc.getLocationDescription(),loc.getLocationPlace(),loc.getLocationTrailID()); + } + + } diff --git a/src/main/java/Team5/SmartTowns/Landmarks/Landmarks.java b/src/main/java/Team5/SmartTowns/Landmarks/Landmarks.java index 94fec3e5f18cf082f58ca3dbca4223b1c50429b7..381ccf04293b752f03e2eb743ba820c186b277dd 100644 --- a/src/main/java/Team5/SmartTowns/Landmarks/Landmarks.java +++ b/src/main/java/Team5/SmartTowns/Landmarks/Landmarks.java @@ -15,18 +15,19 @@ import java.util.List; @NoArgsConstructor public class Landmarks { - // Intialised object to getID from trail. + // Initialized object to getID from trail. //Predefined Landmark for Dragons Tale. public static List<Landmarks> landmarksDragonstrail = List.of( new Landmarks( 1, "A scent of...Dragon", "The Dragon has been spotted near by, find the QR code to continue" , "Start your discovery, at the sweet shop."), - new Landmarks( 2, "They've been found!", "Don't let them escape, find the next QR code to continue!", "location test") + new Landmarks( 2, "They've been found!", "Don't let them escape, find the next QR code to continue!", "Location test") ); private Integer trailID; private int landmarkID; @NotEmpty(message = "You must type in a username.") private String landmarkName; + @NotEmpty(message = "You must attach a contact address.") // Requires @NotEmpty for form validation @Email(message = "You must attach a contact address.") private String landmarkEmail; private String landmarkDescription; diff --git a/src/main/java/Team5/SmartTowns/Landmarks/LandmarksController.java b/src/main/java/Team5/SmartTowns/Landmarks/LandmarksController.java index 98c35594da947d261f6980b10a516a75b4583431..a94feef88935a8d3ca71f6326bdb8f44cda6dd0e 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; @@ -22,7 +25,8 @@ public class LandmarksController { } - + @Autowired + private locationRepository locationRepository; @PostMapping("/landmarkSub") public ModelAndView landmarkSent(@Valid @ModelAttribute("landmarkData") Landmarks landmarks, BindingResult bindingResult, Model model ) { @@ -32,11 +36,11 @@ public class LandmarksController { return modelAndView; } else{ - System.out.println(landmarks); - // current functionality only prints successful Landmarks, (todo )database integration is necessary here - - - ModelAndView modelAndView = new ModelAndView("redirect:/test.html"); + // converts valid response using Location constructor into a submittable format to the sql table + Location loc= new Location(landmarks.getLandmarkName(), landmarks.getLandmarkEmail(), landmarks.getLandmarkDescription(), landmarks.getLandmarkLocation(), landmarks.getTrailID()); + System.out.println(loc); + locationRepository.addLocation(loc); // adds valid landmark to locations table + ModelAndView modelAndView = new ModelAndView("redirect:/home"); return modelAndView; } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index f7656769c7d1b2afd5e78e3a93c3cd3ed44abe3b..c7b960defab205639d46c66fcad52df8ffb3fc18 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -7,5 +7,28 @@ insert into trails (trailID, Name) value ('1', 'Caerphilly Coffee Trail'); insert into trails (trailID, Name) value ('2', 'Penarth Dragon Trail'); 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 +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (1, 'St Cenydd','','Location description here','Caerphilly',0101); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (2, 'The Castle','','Location description here','Caerphilly',0101); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (3, 'Medieval Trades','','Location description here','Caerphilly',0101); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (4, 'The Queen''s War','','Location description here','Caerphilly',0101); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (5, 'The Green Lady','','Location description here','Caerphilly',0101); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (6, 'Armoury','','Location description here','Caerphilly',0101); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (7, 'Architecture','','Location description here','Caerphilly',0101); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (8, '21st Century Landmark','','Location description here','Caerphilly',0101); + +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (9, 'JD Wetherspoons-Malcolm Uphill','','Location description here','Caerphilly',0102); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (10, 'Caerphilly Cwtch','','Location description here','Caerphilly',0102); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (11, 'Caerphilly Conservative Club','','Location description here','Caerphilly',0102); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (12, 'The King''s Arms','','Location description here','Caerphilly',0102); + +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (13, 'Caerphilly Bus Station','','Location description here','Caerphilly',0103); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (14, 'The Medieval Courthouse','','Location description here','Caerphilly',0103); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (15 ,'Caerphilly Castle','','Location description here','Caerphilly',0103); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (16, 'Ty Vaughan House','','Location description here','Caerphilly',0103); + +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (17, 'Risca Colliery','','Location description here','Risca',0201); +insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (18, 'Black Vein Colliery Disaster','','Location description here','Risca',0201); + + +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); diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index 8d9e102d1f6b02b1f864e396f591a4a61aad8b41..abb296cb3ddc14856259de8efe5819cabb19f505 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -5,10 +5,15 @@ create table if not exists trails name varchar(128) ) engine=InnoDB; drop table if exists locations; + create table if not exists locations ( locationID bigint auto_increment primary key, - name varchar(128) + locationName varchar(128), + locationEmail varchar(128), + locationDescription longtext, + locationPlace varchar(255), + locationTrailID varchar(128) ) engine=InnoDB; drop table if exists users; create table if not exists users diff --git a/src/main/resources/static/scripts/landmarkFormThScript.js b/src/main/resources/static/scripts/landmarkFormThScript.js new file mode 100644 index 0000000000000000000000000000000000000000..dc8ca6d96e55ff68b95da302e566db992a5eb409 --- /dev/null +++ b/src/main/resources/static/scripts/landmarkFormThScript.js @@ -0,0 +1,35 @@ + + + // verification function of the above form + function landmarkFormValidation(){ + var pass=true; + var trail = document.forms["landmarkSubmission"]["trailID"].value + var location = document.forms["landmarkSubmission"]["landmarkLocation"].value + var description = document.forms["landmarkSubmission"]["landmarkDescription"].value; + var descriptionWrdCount=description.split(" ").length + if (descriptionWrdCount>200){ + alert('Please keep your description to a maximum of 200 words.'); + pass = false; +} + + if (trail==0){ // values of 0 and "" used to ensure default drop down option isn't submittable + alert('Invalid trail selected. \nPlease select the trail you wish to join.'); + pass = false; +} + + if (location==""){ + alert('Invalid location selected. \nPlease select the location you wish to join.'); + + pass = false; // ensure correct trail is selected in accordance to the town it takes place in + +} if ( (location=="Caerphilly" & (parseInt(trail/100)!==1)) || + (location=="Risca" & (parseInt(trail/100)!==2)) || + (location=="Penarth" & (parseInt(trail/100)!==3)) ){ + alert('Trail unavailable in your current location. \nPlease choose an available trail'); + pass = false; +} + + return pass; +} + + diff --git a/src/main/resources/templates/Landmarks/LandmarkFormTh.html b/src/main/resources/templates/Landmarks/LandmarkFormTh.html index 9690356f54031ce873453d68aa97a248ac89f687..b54a9143e3a41726670ad9acdb98c82b1cef3aca 100644 --- a/src/main/resources/templates/Landmarks/LandmarkFormTh.html +++ b/src/main/resources/templates/Landmarks/LandmarkFormTh.html @@ -5,6 +5,9 @@ <title>Landmark Sign Up</title> <link rel="stylesheet" th:href="@{css/landmarkFormStyle.css}"> <link rel="stylesheet" th:href="@{css/templatingstyle.css}"> +<!-- <link rel="script" th:href="@{scripts/landmarkFormThScript.js}">--> + <script src="scripts/landmarkFormThScript.js"></script> +<!-- <style in="scripts/landmarkFormThScript.js"></style>--> </head> @@ -30,7 +33,8 @@ </label><br><br> <label>Your Location: <select th:field="*{landmarkLocation}"> - <option value="" disabled selected>Select Location</option> + <option value="" hidden="true">Select Location</option> + <option value="" selected disabled>Select Location</option> <option value="Caerphilly">Caerphilly</option> <option value="Risca">Risca</option> <option value="Penarth">Penarth</option> @@ -38,6 +42,7 @@ </label><br><br> <label>Trail: <select th:field="*{trailID}"> + <option value=0 hidden="true">Select Trail</option> <option value=0 disabled selected>Select Trail</option> <option value=0101>(Caerphilly) Castle Trail</option> <option value=0102>(Caerphilly) Pub Trail</option> @@ -56,45 +61,7 @@ <hr style="height:40px; visibility:hidden;" /> </main> -<script> - - - -// verification function of the above form - function landmarkFormValidation(){ - var pass=true; - var trail = document.forms["landmarkSubmission"]["trailID"].value - var location = document.forms["landmarkSubmission"]["landmarkLocation"].value - var description = document.forms["landmarkSubmission"]["landmarkDescription"].value; - var descriptionWrdCount=description.split(" ").length - if (descriptionWrdCount>200){ - alert('Please keep your description to a maximum of 200 words.'); - pass = false; - } - - if (trail==0){ // values of 0 and "" used to ensure default drop down option isn't submittable - alert('Invalid trail selected. \nPlease select the trail you wish to join.'); - pass = false; - } - - if (location==""){ - alert('Invalid location selected. \nPlease select the location you wish to join.'); - - pass = false; // ensure correct trail is selected in accordance to the town it takes place in - - } if ( (location=="Caerphilly" & (parseInt(trail/100)!==1)) || - (location=="Risca" & (parseInt(trail/100)!==2)) || - (location=="Penarth" & (parseInt(trail/100)!==3)) ){ - alert('Trail unavailable in your current location. \nPlease choose an available trail'); - pass = false; - } - - return pass; - } - - -</script> <footer th:insert="~{/towns/Templating.html :: footer}"></footer> </body> </html> \ No newline at end of file