Skip to content
Snippets Groups Projects
Commit 62cf05fc authored by Rhys Nute's avatar Rhys Nute
Browse files

updated databases

parent 94253d55
No related branches found
No related tags found
2 merge requests!38Draft: Businesses,!31Resolve "As a user, I want to see a page of local authorities so that I can easily source contact details for a variety of different local authorities."
package Team5.SmartTowns.Organisation; package Team5.SmartTowns.Organisation;
import Team5.SmartTowns.business.business;
import Team5.SmartTowns.business.businessRepository;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@Controller @Controller
public class organisationControllers { public class organisationControllers {
@GetMapping("/localauthorities") @GetMapping("/localauthorities")
...@@ -18,7 +25,23 @@ public class organisationControllers { ...@@ -18,7 +25,23 @@ public class organisationControllers {
ModelAndView modelAndView = new ModelAndView("WorkWith/business.html"); ModelAndView modelAndView = new ModelAndView("WorkWith/business.html");
return modelAndView; return modelAndView;
} }
@GetMapping("/consumers") @Autowired
private businessRepository businessRepository;
@PostMapping("/businesssub")
public ModelAndView localAuthSent(@Valid @ModelAttribute("business-data")business business, BindingResult bindingResult, Model model ) {
if (bindingResult.hasErrors()) {
ModelAndView modelAndView = new ModelAndView("business-data", model.asMap());
return modelAndView;
} else {// converts user input using the organisation constructor into a submittable format to the sql table
business bus = new business(business.getBusinessName(), business.getAddress1(), business.getAddress2(), business.getCity(), business.getCounty(), business.getPostcode(), business.getWebsite());
System.out.println(bus);
businessRepository.addBusiness(bus); //add local authority to local authority table
ModelAndView modelAndView = new ModelAndView("redirect:/businesses");
return modelAndView;
}
}
@GetMapping("/consumers")
public ModelAndView getConsumersPage(){ public ModelAndView getConsumersPage(){
ModelAndView modelAndView = new ModelAndView("WorkWith/consumers.html"); ModelAndView modelAndView = new ModelAndView("WorkWith/consumers.html");
return modelAndView; return modelAndView;
......
package Team5.SmartTowns.business;
import lombok.AllArgsConstructor;
import lombok.Data;
@AllArgsConstructor
@Data
public class business {
private String businessName;
private String address1;
private String address2;
private String city;
private String county;
private String postcode;
private String website;
@Override
public String toString(){
return "business{" +
businessName + '\'' +
address1 + '\'' +
address2 + '\'' +
city + '\'' +
county + '\'' +
postcode + '\'' +
website +
'}';
}
public String getBusinessName() {
return businessName;
}
public String getAddress1() {
return address1;
}
public String getAddress2() {
return address2;
}
public String getCity() {
return city;
}
public String getCounty() {
return county;
}
public String getPostcode() {
return postcode;
}
public String getWebsite() {
return website;
}
}
package Team5.SmartTowns.business;
import java.util.List;
public interface businessRepository {
List<business> getAllBusinesses();
void addBusiness(business bus);
void addLocalAuthority(business bus);
}
package Team5.SmartTowns.business;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class businessRepositoryJDBC implements businessRepository{
private JdbcTemplate jdbc;
private RowMapper<business> businessMapper;
public businessRepositoryJDBC(JdbcTemplate ajdbc){
this.jdbc = ajdbc;
setbusinessMapper();
}
private void setbusinessMapper(){
businessMapper = (rs, i) -> new business(
rs.getString("businessName"),
rs.getString("address1"),
rs.getString("address2"),
rs.getString("city"),
rs.getString("county"),
rs.getString("postcode"),
rs.getString("website")
);
}
public List<business> getAllBusinesses(){
String sql = "SELECT * FROM localAuthority";
return jdbc.query(sql, businessMapper);
}
@Override
public void addBusiness(business bus) {
}
@Override
public void addLocalAuthority(business bus){
String sql = "INSERT INTO business( businessName, address1, address2, city, county, postcode, website) values (?, ?, ?, ?, ?, ?, ?)";
jdbc.update(sql, bus.getBusinessName(),bus.getAddress1(),bus.getAddress2(),bus.getCity(),bus.getCounty(),bus.getPostcode(),bus.getWebsite());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment