diff --git a/src/main/java/Team5/SmartTowns/rewards/StickersRepository.java b/src/main/java/Team5/SmartTowns/rewards/StickersRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..869c2d07353a7aa090fb7fee79b2d44feacd9b12 --- /dev/null +++ b/src/main/java/Team5/SmartTowns/rewards/StickersRepository.java @@ -0,0 +1,9 @@ +//Holds locations data repository +package Team5.SmartTowns.rewards; + +import java.util.List; + +public interface StickersRepository { + List<Sticker> getAllStickers(); +} + diff --git a/src/main/java/Team5/SmartTowns/rewards/StickersRepositoryJDBC.java b/src/main/java/Team5/SmartTowns/rewards/StickersRepositoryJDBC.java new file mode 100644 index 0000000000000000000000000000000000000000..64df71bc3da3eff327377798185111aee3be7867 --- /dev/null +++ b/src/main/java/Team5/SmartTowns/rewards/StickersRepositoryJDBC.java @@ -0,0 +1,33 @@ +//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 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") + ); + } + + @Override + public List<Sticker> getAllStickers(){ + String sql= "SELECT * FROM stickers"; + return jdbc.query(sql, stickerMapper); + } +} diff --git a/src/main/java/Team5/SmartTowns/users/UserController.java b/src/main/java/Team5/SmartTowns/users/UserController.java index d2cf3b318522ae65fb456ca4a03dd3bbe10376ed..e9ba754370320a24c9414c961edca6904b3bc938 100644 --- a/src/main/java/Team5/SmartTowns/users/UserController.java +++ b/src/main/java/Team5/SmartTowns/users/UserController.java @@ -4,6 +4,7 @@ package Team5.SmartTowns.users; import Team5.SmartTowns.rewards.Badge; import Team5.SmartTowns.rewards.BadgesRepository; 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; @@ -19,19 +20,10 @@ public class UserController { private UserRepository userRepository; @Autowired private BadgesRepository badgesRepository; + @Autowired + private StickersRepository stickersRepository; - /*TODO REPLACE IT WITH DATABASE LIST*/ - static List<Sticker> stickers = List.of( - new Sticker(1, "Sticker", "Sticker", 1), - new Sticker(2, "Sticker", "Sticker", 4), - new Sticker(3, "Sticker", "Sticker One is This", 4), - new Sticker(4, "Sticker", "Sticker One is This", 5), - new Sticker(5, "Sticker", "Sticker One is This", 5), - new Sticker(46, "Sticker", "Sticker One is This", 5), - new Sticker(7, "Sticker", "Sticker One is This", 2) - ); - @GetMapping("/userList") public ModelAndView userList() { ModelAndView mav = new ModelAndView("towns/userData"); @@ -43,6 +35,7 @@ public class UserController { @GetMapping("/user/{id}") public ModelAndView getUserPage(@PathVariable int id) { List<Badge> badges = badgesRepository.getAllBadges(); + List<Sticker> stickers = stickersRepository.getAllStickers(); List<User> users = userRepository.getAllUsers(); ModelAndView mav = new ModelAndView("rewards/userProfile"); users.stream() diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index d06aaf78f54db096f2f3da78687cbd0676652421..2c9ea94b36cf320299af1853a10472bd62f5bbc6 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -11,8 +11,15 @@ insert into locations (locationID, Name) value ('1', 'Caerphilly'); insert into locations (locationID, Name) value ('2', 'Penarth'); delete from badges; -insert into badges (badgeID, name, description, difficulty) value ('1', 'TownConnoisseur', 'You know the town very well!', '2'); -insert into badges (badgeID, name, description, difficulty) value ('2', 'TownRegular', 'You visited the town 3 days in a row!', '1'); -insert into badges (badgeID, name, description, difficulty) value ('3', 'TownMaster', 'You visited the town 7 days in a row!', '1'); -insert into badges (badgeID, name, description, difficulty) value ('4', 'TownRegular', 'You visited the town 3 days in a row!', '1'); -insert into badges (badgeID, name, description, difficulty) value ('5', 'TownRegular', 'You visited the town 3 days in a row!', '1'); \ No newline at end of file +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'); +insert into badges (name, description, difficulty) value ('TownMaster', 'You visited the town 7 days in a row!', '1'); +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 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'); +insert into stickers (name, description, rarity) value ('TownMaster', 'You visited the town 7 days in a row!', '1'); +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'); \ No newline at end of file diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index 4eadc115d284f559d296a7743cf852606a507418..ab83b929297cb5930a4c127550f2b30422c83f7e 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -24,4 +24,12 @@ create table if not exists badges name varchar(128), description varchar(128), difficulty bigint +) engine=InnoDB; +drop table if exists stickers; +create table if not exists stickers +( + stickerID bigint auto_increment primary key, + name varchar(128), + description varchar(128), + rarity bigint ) engine=InnoDB; \ No newline at end of file