Newer
Older
//Implements the locations repository using JDBC
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
public class LocationRepositoryJDBC implements LocationRepository {
Rhys Evans
committed
private RowMapper<Location> locationMapper;
Rhys Evans
committed
// public LocationRepositoryJDBC() {
// JdbcTemplate ajdbc = new JdbcTemplate();
// this.jdbc =ajdbc;
// setlocationMapper();
//
// }
Rhys Evans
committed
locationMapper = (rs, i) -> new Location(
rs.getString("locationName"),
rs.getString("locationEmail"),
rs.getString("locationDescription"),
rs.getString("locationPlace"),
rs.getString("locationTrailID"),
rs.getBoolean("locationApproved")
Rhys Evans
committed
public List<Location> getAllLocation(){
String sql= "SELECT * FROM locations";
Rhys Evans
committed
// public LocationRepositoryJDBC() {
// JdbcTemplate ajdbc = new JdbcTemplate();
// this.jdbc =ajdbc;
// setlocationMapper();
//
// }
// public LocationRepositoryJDBC(JdbcTemplate jdbc, RowMapper<Location> locationMapper) {
// this.jdbc = jdbc;
// this.locationMapper = locationMapper;
// }
@Override
public List<Location> getAllApprovedLocations(){
String sql= "SELECT * FROM locations";
List<Location> lis = jdbc.query(sql, locationMapper);
List<Location> lisFiltered = new ArrayList<>();
for (Location li : lis){
if (li.isLocationApproved()){
lisFiltered.add(li);
}
}
return lisFiltered;
}
@Override
public List<Location> getAllUnapprovedLocations(){
String sql= "SELECT * FROM locations";
List<Location> lis = jdbc.query(sql, locationMapper);
List<Location> lisFiltered = new ArrayList<>();
for (Location li : lis){
if (!li.isLocationApproved()){
lisFiltered.add(li);
}
}
return lisFiltered;
}
Rhys Evans
committed
@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, locationApproved) values (?,?,?,?,?,?)";
jdbc.update(sql,loc.getLocationName(),loc.getLocationEmail(),loc.getLocationDescription(),loc.getLocationPlace(),loc.getLocationTrailID(), false);
// @Override
// public List<Location> getApprovedLocations(){
// JdbcTemplate jdbc = new JdbcTemplate();
// List<Location> locations = new LocationRepositoryJDBC(jdbc).getAllLocation();
// List<Location> locationApprovalList= new ArrayList<Location>();
// for (Location loc :locations){
// if (loc.isLocationApproved()) {
// locationApprovalList.add(loc);
// }
// } return locationApprovalList;
// }
Rhys Evans
committed
//
// @Override
// public List<Location> getApprovedLocations2(List<Location> list){
//
// List<Location> locationApprovalList= new ArrayList<Location>();
// for (Location loc :list){
// if (loc.isLocationApproved()) {
// locationApprovalList.add(loc);
// }
// } return locationApprovalList;
// }
//
// @Override
// public List<Location> getUnapprovedLocations(){
// List<Location> locations = getAllLocation();
// List<Location> locationUnapprovedList= new ArrayList<Location>();
// for (Location loc :locations){
// if (!loc.isLocationApproved()) {
// locationUnapprovedList.add(loc);
// }
// } return locationUnapprovedList;
// }
@Override
return jdbc.queryForObject("SELECT locationID FROM locations WHERE locationName=?", Integer.class, name);
@Override
public void updateApprovalStatus(int locID){
String updateSql = "update locations set locationApproved = true where locationID = ?";
jdbc.update(updateSql, locID);
}
// return jdbc.queryForObject("SELECT locationApproval FROM locations WHERE locationName=?", locationID);
// public JdbcTemplate getJdbc() {
// return jdbc;
// }