Skip to content
Snippets Groups Projects
Commit 2fa2e246 authored by Rhys Evans's avatar Rhys Evans
Browse files

Further work. Previous commit - attempting foreign keys

parent 4d792c5d
No related branches found
No related tags found
1 merge request!34Resolve "As a user, I would like a town specific page which shows all trails for that town so that I can easily see my progress"
package Team5.SmartTowns.Data;public class Town {
package Team5.SmartTowns.Data;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Town {
private String townName;
private Integer townTrailNumber;
public String getTownName() {
return townName;
}
public Integer getTownTrailNumber() {
return townTrailNumber;
}
}
package Team5.SmartTowns.Data;public class TownRepository {
package Team5.SmartTowns.Data;
import java.util.List;
public interface TownRepository {
List<Town> getAllTowns();
void addTown(Town town);
}
package Team5.SmartTowns.Data;public class TownRepositoryJDBC {
package Team5.SmartTowns.Data;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class TownRepositoryJDBC implements TownRepository{
private JdbcTemplate jdbc;
private RowMapper<Town> townMapper;
public TownRepositoryJDBC(JdbcTemplate aJdbc) {
this.jdbc = aJdbc;
setTownMapper();
}
private void setTownMapper(){
townMapper = (rs, i) -> new Town(
rs.getString("townName"),
rs.getInt("townTrailNumber")
);
}
public List<Town> getAllTowns(){
String sql= "SELECT * FROM towns";
return jdbc.query(sql, townMapper);
}
@Override // intended implementation at current: user data from templates/Landmarks/LandmarkFormTh.html is added to the Location table
public void addTown(Town town) {
String sql = "insert into towns( townName,townTrailNumber) values (?,?)";
jdbc.update(sql,town.getTownName(),town.getTownTrailNumber());
}
}
package Team5.SmartTowns.Towns;
import Team5.SmartTowns.Data.Location;
import Team5.SmartTowns.Data.Town;
import Team5.SmartTowns.Data.TownRepository;
import Team5.SmartTowns.Data.locationRepository;
import Team5.SmartTowns.Landmarks.Landmarks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
......@@ -9,6 +14,10 @@ import java.util.List;
@Controller
public class TownController {
@Autowired
private Team5.SmartTowns.Data.locationRepository locationRepository;
@Autowired
private Team5.SmartTowns.Data.TownRepository townRepository;
@GetMapping("/home")
public ModelAndView getTownList(){
......@@ -20,12 +29,19 @@ public class TownController {
}
// @GetMapping("/home/town")
// public ModelAndView getATownTrailsList(){
// ModelAndView modelAndView = new ModelAndView("userTrails/userTrailsTemplate");
// modelAndView.addObject("townStuff", a);
// return modelAndView;
// }
//
@GetMapping("/home/town")
public ModelAndView getATownTrailsList(){
ModelAndView modelAndView = new ModelAndView("userTrails/userTrailsTemplate");
TownStorage townsCurrent= new TownStorage().getInstance();
List<Towns> towns = townsCurrent.getTownList();
List<Town> Towns = townRepository.getAllTowns();
modelAndView.addObject("towns", Towns);
List<Location> Locations = locationRepository.getAllLocation();
modelAndView.addObject("locations", Locations);
return modelAndView;
}
public List<Location> filterByLocationForTrails(List<Location>)
}
......@@ -5,15 +5,9 @@ create table if not exists trails
name varchar(128)
) engine=InnoDB;
drop table if exists towns;
create table if not exists towns
(
townID bigint auto_increment primary key,
townName varchar(255),
trailNumber tinyint
) engine=InnoDB;
drop table if exists locations;
create table if not exists locations
......@@ -22,9 +16,8 @@ create table if not exists locations
locationName varchar(128),
locationEmail varchar(128),
locationDescription longtext,
locationPlace bigint,
locationTrailID varchar(128),
foreign key (locationPlace) references towns(townID)
locationPlace varchar(255),
locationTrailID varchar(128)
) engine=InnoDB;
drop table if exists users;
......@@ -69,3 +62,10 @@ create table if not exists stickerProgress
hasSticker boolean /*Has sticker or not*/
) engine=InnoDB;
create table if not exists towns
(
townID bigint auto_increment primary key,
townName varchar(255) ,
townTrailNumber tinyint
) engine=InnoDB;
......@@ -5,10 +5,16 @@
<title>Trails</title>
</head>
<body>
<main>
<div th:each="location:${locations}"></div>
</main>
</body>
......
package Team5.SmartTowns;
import Team5.SmartTowns.Towns.TownController;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
class SmartTownsApplicationTests {
private static TownController townController;
@Autowired
private Team5.SmartTowns.Data.locationRepository locationRepository;
// @BeforeAll
// public static void before(){}
// townController= new townController();
//}
@Test
void contextLoads() {
}
@Test
public void whenFilteringTownsByLocationsReturnOneTown() {
townController.filterByLocationForTrails()
assertEquals(, townController.filterByLocationForTrails);
}
}
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