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

QR part 1

parent a9767edb
No related branches found
No related tags found
1 merge request!27Qr codes
Showing
with 438 additions and 16 deletions
...@@ -24,6 +24,10 @@ repositories { ...@@ -24,6 +24,10 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.mariadb.jdbc:mariadb-java-client:2.1.2'
testImplementation 'junit:junit:4.13.1'
compileOnly 'org.projectlombok:lombok'
testImplementation 'org.projectlombok:lombok:1.18.28' testImplementation 'org.projectlombok:lombok:1.18.28'
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools' developmentOnly 'org.springframework.boot:spring-boot-devtools'
......
File added
package Team5.SmartTowns.Data;
import Team5.SmartTowns.users.User;
import Team5.SmartTowns.users.UserRepository;
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;
import java.util.*;
@Controller
public class DatabaseController {
@Autowired
private locationRepository locationRepository;
@Autowired
private trailsRepository trailsRepository;
@GetMapping("/trailList")
public ModelAndView trailList() {
ModelAndView mav1 = new ModelAndView("towns/trailsData");
List<trail> trail = trailsRepository.getAllTrails();
mav1.addObject("trails", trail);
return mav1;
}
@GetMapping("locationList")
public ModelAndView locationList(){
ModelAndView mav2 = new ModelAndView("towns/locationData");
List<Location> Locations = locationRepository.getAllLocation();
mav2.addObject("location", Locations);
return mav2;
}
}
//Holds variable data for the locations table (landmarks)
package Team5.SmartTowns.Data;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Location {
// private int locationID;
private String locationName;
private String locationEmail;
private String locationDescription;
private String locationPlace;
private int locationTrailID;
@Override
public String toString() {
return "Location{" +
locationName + '\'' +
locationEmail + '\'' +
locationDescription + '\'' +
locationPlace + '\'' +
locationTrailID +
'}';
}
public String getLocationName() {
return locationName;
}
public String getLocationEmail() {
return locationEmail;
}
public String getLocationDescription() {
return locationDescription;
}
public String getLocationPlace() {
return locationPlace;
}
public int getLocationTrailID() {
return locationTrailID;
}
}
//Holds locations data repository (landmarks)
package Team5.SmartTowns.Data;
import java.util.List;
public interface locationRepository {
List<Location> getAllLocation();
void addLocation(Location loc);
}
//Implements the locations repository using JDBC
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 locationRepositoryJDBC implements locationRepository {
private JdbcTemplate jdbc;
private RowMapper<Location> locationMapper;
public locationRepositoryJDBC(JdbcTemplate aJdbc) {
this.jdbc = aJdbc;
setlocationMapper();
}
private void setlocationMapper(){
locationMapper = (rs, i) -> new Location(
rs.getString("locationName"),
rs.getString("locationEmail"),
rs.getString("locationDescription"),
rs.getString("locationPlace"),
rs.getInt("locationTrailID")
);
}
public List<Location> getAllLocation(){
String sql= "SELECT * FROM locations";
return jdbc.query(sql, locationMapper);
}
@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) values (?,?,?,?,?)";
jdbc.update(sql,loc.getLocationName(),loc.getLocationEmail(),loc.getLocationDescription(),loc.getLocationPlace(),loc.getLocationTrailID());
}
}
//Holds variable data for the trails table
package Team5.SmartTowns.Data;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class trail {
private int trailsId;
private String name;
}
//Holds trails data repository
package Team5.SmartTowns.Data;
import java.util.List;
public interface trailsRepository {
List<trail> getAllTrails();
}
//Implements the trails repository using JDBC
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 trailsRepositoryJDBC implements trailsRepository{
private JdbcTemplate jdbc;
private RowMapper<trail> trailMapper;
public trailsRepositoryJDBC(JdbcTemplate aJdbc){
this.jdbc = aJdbc;
settrailsMapper();
}
private void settrailsMapper(){
trailMapper = (rs, i) -> new trail(
rs.getInt("trailID"),
rs.getString("name")
);
}
public List<trail> getAllTrails(){
String sql= "SELECT * FROM trails";
return jdbc.query(sql, trailMapper);
}
}
...@@ -15,18 +15,19 @@ import java.util.List; ...@@ -15,18 +15,19 @@ import java.util.List;
@NoArgsConstructor @NoArgsConstructor
public class Landmarks { public class Landmarks {
// Intialised object to getID from trail. // Initialized object to getID from trail.
//Predefined Landmark for Dragons Tale. //Predefined Landmark for Dragons Tale.
public static List<Landmarks> landmarksDragonstrail = List.of( public static List<Landmarks> landmarksDragonstrail = List.of(
new Landmarks( 1, "A scent of...Dragon", "The Dragon has been spotted near by, find the QR code to continue" , "Start your discovery, at the sweet shop."), new Landmarks( 1, "A scent of...Dragon", "The Dragon has been spotted near by, find the QR code to continue" , "Start your discovery, at the sweet shop."),
new Landmarks( 2, "They've been found!", "Don't let them escape, find the next QR code to continue!", "location test") new Landmarks( 2, "They've been found!", "Don't let them escape, find the next QR code to continue!", "Location test")
); );
private Integer trailID; private Integer trailID;
private int landmarkID; private int landmarkID;
@NotEmpty(message = "You must type in a username.") @NotEmpty(message = "You must type in a username.")
private String landmarkName; private String landmarkName;
@NotEmpty(message = "You must attach a contact address.") // Requires @NotEmpty for form validation
@Email(message = "You must attach a contact address.") @Email(message = "You must attach a contact address.")
private String landmarkEmail; private String landmarkEmail;
private String landmarkDescription; private String landmarkDescription;
......
package Team5.SmartTowns.Landmarks; package Team5.SmartTowns.Landmarks;
import Team5.SmartTowns.Data.Location;
import Team5.SmartTowns.Data.locationRepository;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
...@@ -22,7 +25,8 @@ public class LandmarksController { ...@@ -22,7 +25,8 @@ public class LandmarksController {
} }
@Autowired
private locationRepository locationRepository;
@PostMapping("/landmarkSub") @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 ) {
...@@ -32,11 +36,11 @@ public class LandmarksController { ...@@ -32,11 +36,11 @@ public class LandmarksController {
return modelAndView; return modelAndView;
} else{ } else{
System.out.println(landmarks); // converts valid response using Location constructor into a submittable format to the sql table
// current functionality only prints successful Landmarks, (todo )database integration is necessary here Location loc= new Location(landmarks.getLandmarkName(), landmarks.getLandmarkEmail(), landmarks.getLandmarkDescription(), landmarks.getLandmarkLocation(), landmarks.getTrailID());
System.out.println(loc);
locationRepository.addLocation(loc); // adds valid landmark to locations table
ModelAndView modelAndView = new ModelAndView("redirect:/test.html"); ModelAndView modelAndView = new ModelAndView("redirect:/home");
return modelAndView; return modelAndView;
} }
......
package Team5.SmartTowns.Towns;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class TownController {
@GetMapping("/home")
public ModelAndView getTownList(){
ModelAndView modelAndView = new ModelAndView("Towns/home/homePage");
TownStorage townsCurrent= new TownStorage().getInstance();
List<Towns> towns = townsCurrent.getTownList();
modelAndView.addObject("towns",towns);
return modelAndView;
}
}
package Team5.SmartTowns.Towns;
import java.util.ArrayList;
import java.util.List;
public class TownStorage {
private List<Towns> townList;
private static TownStorage singleton;
TownStorage() {
townList = new ArrayList<>();
townList.addAll(
List.of(
new Towns("Caerphilly",01,3,70,"/images/CaerphillyCastle.jpg"),
new Towns("Risca",02,2,34,"/images/RiscaBanner.jpg"),
new Towns("Penarth",03,5,0,"/images/PenarthBanner.jpg"),
new Towns("Penarth",03,5,50,"/images/PenarthBanner.jpg"),
new Towns("Caerphilly",01,3,70,"/images/CaerphillyCastle.jpg"),
new Towns("Risca",02,2,90,"/images/RiscaBanner.jpg"),
new Towns("Penarth",03,5,100,"/images/PenarthBanner.jpg")
)
);
}
public static TownStorage getInstance() {
if (singleton == null) {
singleton = new TownStorage();
}
return singleton;
}
public List<Towns> getTownList() {
return townList;
}
}
package Team5.SmartTowns.Towns;
import lombok.Data;
@Data
public class Towns {
private String name;
private Integer id;
private int trailNumber;
private int trailProgress;
private String imageTown;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public int getTrailNumber() {
return trailNumber;
}
public void setTrailNumber(int trailNumber) {
this.trailNumber = trailNumber;
}
public int getTrailProgress() {
return trailProgress;
}
public void setTrailProgress(int trailProgress) {
this.trailProgress = trailProgress;
}
public String getImageTown() {
return imageTown;
}
public void setImageTown(String imageTown) {
this.imageTown = imageTown;
}
public Towns(String name, Integer id, int trailNumber, int trailProgress, String imageTown) {
this.name = name;
this.id = id;
this.trailNumber = trailNumber;
this.trailProgress = trailProgress;
this.imageTown = imageTown;
}
}
...@@ -11,27 +11,28 @@ import org.springframework.stereotype.Controller; ...@@ -11,27 +11,28 @@ import org.springframework.stereotype.Controller;
public class WebpageController { public class WebpageController {
@GetMapping("/Caerleon") @GetMapping("/Caerleon")
public ModelAndView getCaerleonPage(){ public ModelAndView getCaerleonPage(){
ModelAndView modelAndView = new ModelAndView("towns/caerleon"); ModelAndView modelAndView = new ModelAndView("Towns/caerleon");
return modelAndView; return modelAndView;
} }
@GetMapping("/Caerphilly") @GetMapping("/Caerphilly")
public ModelAndView getCaerphillyPage(){ public ModelAndView getCaerphillyPage(){
ModelAndView modelAndView = new ModelAndView("towns/caerphilly"); ModelAndView modelAndView = new ModelAndView("Towns/caerphilly");
return modelAndView; return modelAndView;
} }
@GetMapping("/Risca") @GetMapping("/Risca")
public ModelAndView getRiscaPage(){ public ModelAndView getRiscaPage(){
ModelAndView modelAndView = new ModelAndView("towns/risca"); ModelAndView modelAndView = new ModelAndView("Towns/risca");
return modelAndView; return modelAndView;
} }
@GetMapping("/home") // @GetMapping("/home")
public ModelAndView getHome(){ // public ModelAndView getHome(){
ModelAndView modelAndView = new ModelAndView("towns/home/homePage"); // ModelAndView modelAndView = new ModelAndView("Towns/home/homePage");
return modelAndView; // return modelAndView;
} // }
@RequestMapping(value="/test_ajax_frag", method=RequestMethod.POST) @RequestMapping(value="/test_ajax_frag", method=RequestMethod.POST)
public String sendHtmlFragment(Model map) { public String sendHtmlFragment(Model map) {
......
/*AUTHOR: Gabriel Copat*/
package Team5.SmartTowns.rewards;
import lombok.Data;
import java.io.File;
import java.util.Objects;
@Data
public class Badge {
/* Badges can be earned by completing certain goals.
* They are displayed in the user profile page
*
* For example, one might earn a badge after visiting 20 locations */
int id;
String name;
String description;
String imgPath;
int difficulty; //1-5
public Badge(int id, String name, String description, int difficulty) {
this.id = id;
this.name = name;
this.description = description;
this.difficulty = difficulty;
imgPath = findImagePath();
}
private String findImagePath(){
/* Finds the image in the Path folder, if image is not found assigns default image */
String imgPath = "images/rewards/badges/" + id + ".jpg";
String notFoundPath = "/images/rewards/badges/0.png";
File imgFile = new File("src/main/resources/static/" + imgPath);
return imgFile.exists() ? imgPath : notFoundPath;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Badge badge = (Badge) o;
return id == badge.id && Objects.equals(name, badge.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
}
//Holds locations data repository
package Team5.SmartTowns.rewards;
import java.util.List;
public interface BadgesRepository {
List<Badge> getAllBadges();
}
//Implements the locations repository using JDBC
package Team5.SmartTowns.rewards;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class BadgesRepositoryJDBC implements BadgesRepository {
private JdbcTemplate jdbc;
private RowMapper<Badge> badgeMapper;
public BadgesRepositoryJDBC(JdbcTemplate aJdbc) {
this.jdbc = aJdbc;
setBadgeMapper();
}
private void setBadgeMapper(){
badgeMapper = (rs, i) -> new Badge(
rs.getInt("badgeID"),
rs.getString("name"),
rs.getString("description"),
rs.getInt("difficulty")
);
}
@Override
public List<Badge> getAllBadges(){
String sql= "SELECT * FROM badges";
return jdbc.query(sql, badgeMapper);
}
}
package Team5.SmartTowns.rewards;
import org.springframework.stereotype.Controller;
@Controller
public class RewardsController {
}
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