diff --git a/src/main/java/Team5/SmartTowns/Organisation/organisationControllers.java b/src/main/java/Team5/SmartTowns/Organisation/organisationControllers.java index 76d93d7bca0ad409fc2177b28dd5b8f548983a4a..ba1c4ea7f7ca039d7df8376fa719f6d3cf1fa1d6 100644 --- a/src/main/java/Team5/SmartTowns/Organisation/organisationControllers.java +++ b/src/main/java/Team5/SmartTowns/Organisation/organisationControllers.java @@ -1,51 +1,65 @@ package Team5.SmartTowns.Organisation; import Team5.SmartTowns.Data.Location; -import Team5.SmartTowns.Data.addAddTownsRepository; -import Team5.SmartTowns.Landmarks.Landmarks; +import Team5.SmartTowns.addTowns.addAddTowns; +import Team5.SmartTowns.addTowns.addAddTownsRepository; import Team5.SmartTowns.Towns.Towns; 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; -import org.springframework.web.bind.annotation.*; +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 org.springframework.stereotype.Controller; + +import java.util.List; + @Controller public class organisationControllers { @GetMapping("/localauthorities") - public ModelAndView getLocalAuthoritiesPage(){ + public ModelAndView getLocalAuthoritiesPage() { ModelAndView modelAndView = new ModelAndView("WorkWith/local-authorities.html"); return modelAndView; } + @GetMapping("/businesses") - public ModelAndView getBusinessPage(){ + public ModelAndView getBusinessPage() { ModelAndView modelAndView = new ModelAndView("WorkWith/business.html"); return modelAndView; } + @GetMapping("/consumers") - public ModelAndView getConsumersPage(){ + public ModelAndView getConsumersPage() { ModelAndView modelAndView = new ModelAndView("WorkWith/consumers.html"); return modelAndView; } + @GetMapping("/towns") - public ModelAndView getTownsPage(){ + public ModelAndView getTownsPage() { ModelAndView modelAndView = new ModelAndView("WorkWith/towns.html"); + List<addAddTowns> Towns = addAddTownsRepository.getAllAddTowns(); + modelAndView.addObject("towns", Towns); return modelAndView; } @Autowired private addAddTownsRepository addAddTownsRepository; + @PostMapping("/townssub") - public ModelAndView townsSent(@Valid @ModelAttribute("towns-data")Towns towns, BindingResult bindingResult, Model model ) { + public ModelAndView townsSent(@Valid @ModelAttribute("towns-data") addAddTowns towns, BindingResult bindingResult, Model model) { + List<addAddTowns> addtowns = addAddTownsRepository.getAllAddTowns(); + if (bindingResult.hasErrors()) { ModelAndView modelAndView = new ModelAndView("towns-data", model.asMap()); return modelAndView; } else {// converts user input using the organisation constructor into a submittable format to the sql table - Towns tow= new Towns(towns.getTownName(), towns.getAddress1(), towns.getAddress2(), towns.getCity(), towns.getCounty(), towns.getPostcode()); - System.out.println(bus); - addAddTownsRepository.addAddTown(tow); //add new towns to Add Towns table + addAddTowns tow = new addAddTowns(towns.getTownName(), towns.getAddress1(), towns.getAddress2(), towns.getCity(), towns.getCounty(), towns.getPostcode()); + System.out.println(tow); + addAddTownsRepository.addTown(tow); //add new towns to Add Towns table ModelAndView modelAndView = new ModelAndView("redirect:/businesses"); return modelAndView; } + } } diff --git a/src/main/java/Team5/SmartTowns/addTowns/addAddTowns.java b/src/main/java/Team5/SmartTowns/addTowns/addAddTowns.java index a0a196c1b51126d539633eb3246e429f918568c6..302caf2999b73023f3cc068bf94ded8abb78cc3d 100644 --- a/src/main/java/Team5/SmartTowns/addTowns/addAddTowns.java +++ b/src/main/java/Team5/SmartTowns/addTowns/addAddTowns.java @@ -48,4 +48,5 @@ public class addAddTowns { public String getPostcode() { return postcode; } + } diff --git a/src/main/java/Team5/SmartTowns/addTowns/addAddTownsRepository.java b/src/main/java/Team5/SmartTowns/addTowns/addAddTownsRepository.java index 6eb350880db3ffc2148f7e7c034134a23859de69..c1c11ee303e25143a115a0c59ef1ac7c5d41f814 100644 --- a/src/main/java/Team5/SmartTowns/addTowns/addAddTownsRepository.java +++ b/src/main/java/Team5/SmartTowns/addTowns/addAddTownsRepository.java @@ -6,6 +6,7 @@ import Team5.SmartTowns.Towns.Towns; import java.util.List; public interface addAddTownsRepository { - List<Location> getAllAddTowns(); - void addAddTowns(Towns tow); + List<addAddTowns> getAllAddTowns(); + + void addTown(addAddTowns tow); } diff --git a/src/main/java/Team5/SmartTowns/addTowns/addAddTownsRepositoryJDBC.java b/src/main/java/Team5/SmartTowns/addTowns/addAddTownsRepositoryJDBC.java index 9be19b9e9af0d4672b6c03d9a6d3b86a5c6286ff..c1cf738ab3fbe8af414d701fc9a283856ff3c8fd 100644 --- a/src/main/java/Team5/SmartTowns/addTowns/addAddTownsRepositoryJDBC.java +++ b/src/main/java/Team5/SmartTowns/addTowns/addAddTownsRepositoryJDBC.java @@ -26,12 +26,13 @@ public class addAddTownsRepositoryJDBC implements addAddTownsRepository { rs.getString("postcode") ); } - + @Override public List<addAddTowns> getAllAddTowns() { String sql = "SELECT * FROM addTowns"; return jdbc.query(sql, addTownsMapper); } + @Override public void addTown(addAddTowns tow) { String sql = "insert into addTowns( townName, address1, address2, city, county, postcode) values (?, ?, ?, ?, ?, ?)";