From 496275eff8e99dec0303910b5c4e8ef1f59ba1bb Mon Sep 17 00:00:00 2001 From: Rhys Evans <EvansRM17@cardiff.ac.uk> Date: Wed, 13 Dec 2023 22:36:36 +0000 Subject: [PATCH] Added front-end verification to approval form --- .../landmarks/LandmarksController.java | 40 +++++++++---------- .../LocationsCoordinates.java | 9 +++++ .../PlacesCoordinatesRepository.java | 2 + .../PlacesCoordinatesRepositoryJDBC.java | 7 ++-- .../static/scripts/locationApprovalForm.js | 16 ++++++++ .../landmarks/locationApprovalFormTh.html | 21 ++++++---- 6 files changed, 65 insertions(+), 30 deletions(-) create mode 100644 src/main/resources/static/scripts/locationApprovalForm.js diff --git a/src/main/java/Team5/SmartTowns/landmarks/LandmarksController.java b/src/main/java/Team5/SmartTowns/landmarks/LandmarksController.java index aa044463..e3f28c48 100644 --- a/src/main/java/Team5/SmartTowns/landmarks/LandmarksController.java +++ b/src/main/java/Team5/SmartTowns/landmarks/LandmarksController.java @@ -20,9 +20,9 @@ import java.util.List; @Controller public class LandmarksController { -// Controllers for LandmarkFormTh.html landmark submission form + // Controllers for LandmarkFormTh.html landmark submission form @GetMapping("/landmarkSubmission") - public ModelAndView landmarkSubmission(){ + public ModelAndView landmarkSubmission() { ModelAndView modelAndView1 = new ModelAndView("Landmarks/LandmarkFormTh.html"); modelAndView1.addObject("landmarkData", new Landmarks()); return modelAndView1; @@ -31,32 +31,33 @@ public class LandmarksController { @Autowired private LocationRepository locationRepository; + @PostMapping("/landmarkSub") - public ModelAndView landmarkSent(@Valid @ModelAttribute("landmarkData") Landmarks landmarks, BindingResult bindingResult, Model model ) { + public ModelAndView landmarkSent(@Valid @ModelAttribute("landmarkData") Landmarks landmarks, BindingResult bindingResult, Model model) { if (bindingResult.hasErrors()) { ModelAndView modelAndView = new ModelAndView("Landmarks/LandmarkFormTh.html", model.asMap()); return modelAndView; - } else{ + } else { // 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(), false); + Location loc = new Location(landmarks.getLandmarkName(), landmarks.getLandmarkEmail(), landmarks.getLandmarkDescription(), landmarks.getLandmarkLocation(), landmarks.getTrailID(), false); locationRepository.addLocation(loc); // adds valid landmark to locations table ModelAndView modelAndView = new ModelAndView("redirect:/home"); return modelAndView; } - + } @Autowired private PlacesCoordinatesRepository placesCoordinatesRepo; - + // For form that allows an administrator to add an unapproved location to a trail @GetMapping("/checkpointApproval") - public ModelAndView adminCheckpointApproval(){ + public ModelAndView adminCheckpointApproval() { List<Location> unapprovedLocations = locationRepository.getAllUnapprovedLocations(); //change to unauthorised once merger 68 accepted!! todo ModelAndView modelAndView = new ModelAndView("Landmarks/locationApprovalFormTh.html"); @@ -66,21 +67,19 @@ public class LandmarksController { return modelAndView; } -// @ModelAttribute("locationCoord") + @PostMapping("/checkpointSubmitted") - public ModelAndView checkpointSent(@Valid LocationsCoordinates locCoord, Location location, BindingResult bindingResult, Model model ) { - System.out.println(111); + public ModelAndView checkpointSent(@Valid LocationsCoordinates locCoord, Location location, BindingResult bindingResult, Model model) { + + if (bindingResult.hasErrors()) { + ModelAndView modelAndView = new ModelAndView("Landmarks/locationApprovalFormTh.html", model.asMap()); + return modelAndView; -// if (bindingResult.hasErrors()) { -// ModelAndView modelAndView = new ModelAndView("Landmarks/locationApprovalFormTh.html", model.asMap()); -// return modelAndView; -// -// } else{ + } else { - int locationID= locationRepository.nametoLocationID(location.getLocationName()); - System.out.println( locationID); + int locationID = locationRepository.nametoLocationID(location.getLocationName()); // converts valid response using Location constructor into a submittable format to the sql table - LocationsCoordinates ALocCoord = new LocationsCoordinates(locationID,locCoord.getLocationCoordsLat(),locCoord.getLocationCoordsLong()); + LocationsCoordinates ALocCoord = new LocationsCoordinates(locationID, locCoord.getLocationCoordsLat(), locCoord.getLocationCoordsLong()); placesCoordinatesRepo.addLocationCoord(ALocCoord); // adds valid landmark to locations table locationRepository.updateApprovalStatus(locationID); // updates approval status accordingly System.out.println(placesCoordinatesRepo.getAllLocationCoords()); @@ -89,6 +88,7 @@ public class LandmarksController { // } + } } - } + diff --git a/src/main/java/Team5/SmartTowns/placeswithcoordinates/LocationsCoordinates.java b/src/main/java/Team5/SmartTowns/placeswithcoordinates/LocationsCoordinates.java index 3e809c8a..57031825 100644 --- a/src/main/java/Team5/SmartTowns/placeswithcoordinates/LocationsCoordinates.java +++ b/src/main/java/Team5/SmartTowns/placeswithcoordinates/LocationsCoordinates.java @@ -4,6 +4,10 @@ package Team5.SmartTowns.placeswithcoordinates; import Team5.SmartTowns.data.Location; import Team5.SmartTowns.data.LocationRepositoryJDBC; +import jakarta.validation.constraints.Digits; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.Size; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -18,7 +22,12 @@ import java.util.List; public class LocationsCoordinates { /// 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; +// @NotEmpty(message = "You must enter a Latitude.") + @Min(value=0,message = "You must enter a Latitude.") private Double locationCoordsLat; + +// @NotEmpty(message = "You must type in a Longitude.") + @Min(value=0,message = "You must type in a Longitude.") private Double locationCoordsLong; private JdbcTemplate jdbc; diff --git a/src/main/java/Team5/SmartTowns/placeswithcoordinates/PlacesCoordinatesRepository.java b/src/main/java/Team5/SmartTowns/placeswithcoordinates/PlacesCoordinatesRepository.java index deea3687..df83bb81 100644 --- a/src/main/java/Team5/SmartTowns/placeswithcoordinates/PlacesCoordinatesRepository.java +++ b/src/main/java/Team5/SmartTowns/placeswithcoordinates/PlacesCoordinatesRepository.java @@ -13,6 +13,8 @@ public interface PlacesCoordinatesRepository { List<TownWithTrails> getAllTownCoords(); void addTownWithCoords(TownWithTrails town); + int getLocationTableIDValue(List<Location> locations, String locationName); + // List<Location> getFullApprovedLocations(JdbcTemplate aJdbc); diff --git a/src/main/java/Team5/SmartTowns/placeswithcoordinates/PlacesCoordinatesRepositoryJDBC.java b/src/main/java/Team5/SmartTowns/placeswithcoordinates/PlacesCoordinatesRepositoryJDBC.java index aba6ac86..391f2f9d 100644 --- a/src/main/java/Team5/SmartTowns/placeswithcoordinates/PlacesCoordinatesRepositoryJDBC.java +++ b/src/main/java/Team5/SmartTowns/placeswithcoordinates/PlacesCoordinatesRepositoryJDBC.java @@ -1,8 +1,6 @@ package Team5.SmartTowns.placeswithcoordinates; import Team5.SmartTowns.data.Location; -import Team5.SmartTowns.data.LocationRepositoryJDBC; -import org.springframework.boot.autoconfigure.integration.IntegrationProperties; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; @@ -121,7 +119,9 @@ public class PlacesCoordinatesRepositoryJDBC implements PlacesCoordinatesReposit } } return true;} - int getLocationTableIDValue(List<Location> locations, String locationName){ + + @Override + public int getLocationTableIDValue(List<Location> locations, String locationName){ int index; for(int i=0;i<locations.size();i++){ if (Objects.equals(locations.get(i).getLocationName(), locationName)){ @@ -135,6 +135,7 @@ public class PlacesCoordinatesRepositoryJDBC implements PlacesCoordinatesReposit + // Method used to approve and add locations with associated coordinates. List<Location> unapprovedLocations // public void approveLocationAndAddCoords(String locationsName, Double latCoords, Double longCoords,JdbcTemplate jdbc) { // // need list too diff --git a/src/main/resources/static/scripts/locationApprovalForm.js b/src/main/resources/static/scripts/locationApprovalForm.js new file mode 100644 index 00000000..10778be2 --- /dev/null +++ b/src/main/resources/static/scripts/locationApprovalForm.js @@ -0,0 +1,16 @@ + +//script for basic long/lat validation + +function acceptanceValidation(){ + var pass=true; + var lat = document.forms["adminCheckpointApproval"]["locationCoordsLat"].value + var long = document.forms["adminCheckpointApproval"]["locationCoordsLong"].value + if (lat=="") { + alert('Invalid location inputted. \nPlease input a valid latitude.'); + pass = false; + } + if (long==""){ + alert('Invalid location inputted. \nPlease input a valid longitude.'); + pass = false; + } return pass; +} \ No newline at end of file diff --git a/src/main/resources/templates/landmarks/locationApprovalFormTh.html b/src/main/resources/templates/landmarks/locationApprovalFormTh.html index 93a24ad4..81eea9f6 100644 --- a/src/main/resources/templates/landmarks/locationApprovalFormTh.html +++ b/src/main/resources/templates/landmarks/locationApprovalFormTh.html @@ -3,32 +3,39 @@ <head> <meta charset="UTF-8"> <title>Title</title> + + <link rel="stylesheet" th:href="@{css/templatingstyle.css}"> + <script src="/scripts/locationApprovalForm.js"></script> </head> <body> +<header th:insert="~{/fragments/Templating.html :: header}"></header> <main> - <form action="/checkpointSubmitted" method="post" th:object="${locationCoord}"> + <form action="/checkpointSubmitted" method="post" name="adminCheckpointApproval" th:object="${locationCoord}" onsubmit="return acceptanceValidation()"> <label> Location To Be Approved: <select th:object="${location}" th:field="*{locationName}"}> <!-- <select th:field="*{locationName}">--> <option value="" hidden="true">Select Location</option> <option value="" selected disabled>Select Location</option> - <option value="Caerphilly">Caerphilly</option> - <option th:each="uloc:${uLocs}" th:value="${uloc.getLocationName()}" th:text="${uloc.getLocationName()}"> + <option th:each="uloc:${uLocs}" th:value="${uloc.getLocationName()}" th:text="${uloc.getLocationName()}+' ('+${uloc.getLocationTrailID()}+')'"> </option> </select> </label> - <br><label> Location Latitude: + <br><br> + <label> Location Latitude: <input type="text" th:field="*{locationCoordsLat}" placeholder="Latitude Here"> - <br><label> Location Longitude: + <div th:errors="*{locationCoordsLat}" th:if="${#fields.hasErrors('locationCoordsLat')}">ErrorlocationCoordsLat</div> + <br><br> + <label> Location Longitude: <input type="text" th:field="*{locationCoordsLong}" placeholder="Latitude Here"> - </label><br> + </label><br><br> + <div th:errors="*{locationCoordsLong}" th:if="${#fields.hasErrors('locationCoordsLong')}">ErrorlocationCoordsLat</div> <input type="submit"> </form> </main> - +<footer th:insert="~{/fragments/Templating.html :: footer}"></footer> </body> </html> \ No newline at end of file -- GitLab