Skip to content
Snippets Groups Projects
Commit 2531a0af authored by Gabriel Copat's avatar Gabriel Copat
Browse files

Changed database Repositories for Badges/Stickers to a singular Repository for Rewards

Updated UserController.java to reflect this change
parent e8c1365f
Branches
No related tags found
1 merge request!33Resolve "As a QR-scanning connoisseur , I want to unlock stickers after scanning a QR code to feel a sense of reward."
//Holds locations data repository
package team5.smartTowns.rewards.data;
package team5.smartTowns.rewards;
import team5.smartTowns.rewards.Badge;
import team5.smartTowns.rewards.Sticker;
import java.util.List;
public interface StickersRepository {
public interface RewardsRepository {
List<Badge> getAllBadges();
List<Sticker> getAllStickers();
}
//Implements the locations repository using JDBC
package team5.smartTowns.rewards.data;
package team5.smartTowns.rewards;
import team5.smartTowns.rewards.Badge;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
......@@ -9,13 +8,15 @@ import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class BadgesRepositoryJDBC implements BadgesRepository {
private JdbcTemplate jdbc;
public class RewardsRepositoryJDBC implements RewardsRepository {
private final JdbcTemplate jdbc;
private RowMapper<Badge> badgeMapper;
private RowMapper<Sticker> stickerMapper;
public BadgesRepositoryJDBC(JdbcTemplate aJdbc) {
public RewardsRepositoryJDBC(JdbcTemplate aJdbc) {
this.jdbc = aJdbc;
setBadgeMapper();
setStickerMapper();
}
private void setBadgeMapper(){
badgeMapper = (rs, i) -> new Badge(
......@@ -26,6 +27,21 @@ public class BadgesRepositoryJDBC implements BadgesRepository {
);
}
private void setStickerMapper(){
stickerMapper = (rs, i) -> new Sticker(
rs.getInt("stickerID"),
rs.getString("name"),
rs.getString("description"),
rs.getInt("rarity")
);
}
@Override
public List<Sticker> getAllStickers(){
String sql= "SELECT * FROM stickers";
return jdbc.query(sql, stickerMapper);
}
@Override
public List<Badge> getAllBadges(){
String sql= "SELECT * FROM badges";
......
//Holds locations data repository
package team5.smartTowns.rewards.data;
import team5.smartTowns.rewards.Badge;
import java.util.List;
public interface BadgesRepository {
List<Badge> getAllBadges();
}
//Implements the locations repository using JDBC
package team5.smartTowns.rewards.data;
import team5.smartTowns.rewards.Sticker;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class StickersRepositoryJDBC implements StickersRepository {
private JdbcTemplate jdbc;
private RowMapper<Sticker> stickerMapper;
public StickersRepositoryJDBC(JdbcTemplate aJdbc) {
this.jdbc = aJdbc;
setStickerMapper();
}
private void setStickerMapper(){
stickerMapper = (rs, i) -> new Sticker(
rs.getInt("stickerID"),
rs.getString("name"),
rs.getString("description"),
rs.getInt("rarity"),
rs.getString("pack")
);
}
@Override
public List<Sticker> getAllStickers(){
String sql= "SELECT * FROM stickers";
return jdbc.query(sql, stickerMapper);
}
}
......@@ -2,9 +2,8 @@ package team5.smartTowns.users;
import team5.smartTowns.rewards.Badge;
import team5.smartTowns.rewards.BadgesRepository;
import team5.smartTowns.rewards.RewardsRepository;
import team5.smartTowns.rewards.Sticker;
import team5.smartTowns.rewards.StickersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -20,9 +19,7 @@ public class UserController {
@Autowired
private UserRepository userRepository;
@Autowired
private BadgesRepository badgesRepository;
@Autowired
private StickersRepository stickersRepository;
private RewardsRepository rewardsRepository;
@GetMapping("/login")
public ModelAndView getLoginPage() {
......@@ -43,20 +40,22 @@ public class UserController {
@GetMapping("/user/{id}")
public ModelAndView getUserPage(@PathVariable int id) {
ModelAndView mav = new ModelAndView("users/userProfile");
List<Badge> badges = badgesRepository.getAllBadges(); /*DEPRECATED FOR THE MOMENT*/
mav.addObject("badges", badges);
List<Sticker> allStickers = stickersRepository.getAllStickers();
List<Badge> badges = rewardsRepository.getAllBadges(); /*DEPRECATED FOR THE MOMENT*/
List<Sticker> allStickers = rewardsRepository.getAllStickers();
List<User> users = userRepository.getAllUsers();
Map<Long, Boolean> userStickers = userRepository.getStickers(id);
for (Long stickerID : userStickers.keySet()) { //Finds and updates visibility of stickers based on what the user has
allStickers.stream()
.filter(sticker -> sticker.getId()==stickerID)
.findFirst().ifPresent(sticker -> sticker.setVisibility(userStickers.get(stickerID)));
}
mav.addObject("user", userRepository.getUser(id));
mav.addObject("packs", badges);
mav.addObject("stickers", allStickers);
return mav;
}
}
......@@ -32,6 +32,7 @@ insert into locations ( locationName , locationEmail,locationDescription,locatio
insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (19, 'The Esplanade','','Location description here','Penarth',0301);
insert into locations (locationID, locationName , locationEmail,locationDescription,locationPlace, locationTrailID) value (20, 'The Old Swimming Baths','','Location description here','Penarth',0301);
delete from badges;
insert into badges (name, description, difficulty) value ('TownConnoisseur', 'You know the town very well!', '2');
insert into badges (name, description, difficulty) value ('TownRegular', 'You visited the town 3 days in a row!', '1');
......@@ -39,6 +40,11 @@ insert into badges (name, description, difficulty) value ('TownMaster', 'You vis
insert into badges (name, description, difficulty) value ('TownRegular', 'You visited the town 3 days in a row!', '1');
insert into badges (name, description, difficulty) value ('TownRegular', 'You visited the town 3 days in a row!', '1');
delete from badgeprogress;
insert into badgeprogress (userID, badgeID, progress) value ('1', '1', '40');
insert into badgeprogress (userID, badgeID, progress) value ('1', '2', '70');
insert into badgeprogress (userID, badgeID, progress) value ('2', '2', '70');
delete from stickers;
insert into stickers (name, description, rarity) value ('TownConnoisseur', 'You know the town very well!', '2');
insert into stickers (name, description, rarity) value ('TownRegular', 'You visited the town 3 days in a row!', '1');
......@@ -46,10 +52,10 @@ insert into stickers (name, description, rarity) value ('TownMaster', 'You visit
insert into stickers (name, description, rarity) value ('TownRegular', 'You visited the town 3 days in a row!', '1');
insert into stickers (name, description, rarity) value ('TownRegular', 'You visited the town 3 days in a row!', '1');
delete from badgeprogress;
insert into badgeprogress (userID, badgeID, progress) value ('1', '1', '40');
insert into badgeprogress (userID, badgeID, progress) value ('1', '2', '70');
insert into badgeprogress (userID, badgeID, progress) value ('2', '2', '70');
delete from stickerprogress;
insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '1', true);
insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '3', true);
insert into stickerprogress (userID, stickerID, hasSticker) value ('2', '2', true);
delete from stickerprogress;
insert into stickerprogress (userID, stickerID, hasSticker) value ('1', '1', true);
......
......@@ -39,6 +39,7 @@ drop table if exists stickers;
create table if not exists stickers
(
stickerID bigint auto_increment primary key,
packID bigint,
name varchar(128),
description varchar(128),
rarity bigint
......@@ -57,4 +58,11 @@ create table if not exists stickerProgress
userID bigint,
stickerID bigint,
hasSticker boolean /*Has sticker or not*/
) engine=InnoDB;
create table if not exists packs
(
packID bigint auto_increment primary key,
name varchar(128),
description varchar(128)
) engine=InnoDB;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment