From c16070304827434d61d387ef56c427a294043f43 Mon Sep 17 00:00:00 2001 From: Grant Duchars Date: Thu, 28 Jan 2021 22:50:07 -0500 Subject: [PATCH] Created and updated methods to pull data from db. --- .../achievements/controllers/Controller.java | 36 ++++++++- .../java/achievements/data/Achievements.java | 71 +++++++--------- .../main/java/achievements/data/Games.java | 57 +++++++++++++ .../java/achievements/services/DbService.java | 80 ++++++++++++++----- 4 files changed, 179 insertions(+), 65 deletions(-) create mode 100644 backend/src/main/java/achievements/data/Games.java diff --git a/backend/src/main/java/achievements/controllers/Controller.java b/backend/src/main/java/achievements/controllers/Controller.java index 82ca001..4cc455f 100644 --- a/backend/src/main/java/achievements/controllers/Controller.java +++ b/backend/src/main/java/achievements/controllers/Controller.java @@ -1,5 +1,7 @@ package achievements.controllers; +import achievements.data.Achievements; +import achievements.data.Games; import achievements.data.InternalError; import achievements.services.DbService; import com.fasterxml.jackson.core.JsonProcessingException; @@ -19,11 +21,16 @@ public class Controller { public Controller() {} - @RequestMapping(value = "/achievements", method = GET, produces = "application/json") - public ResponseEntity index() { + @RequestMapping(value = "/achievements/{Name}", method = GET, produces = "application/json") + public ResponseEntity fetchAchievements(@PathVariable("Name") String getName) { + var achievements = (Achievements) null; + if (getName == null) { + achievements = db.getAchievements("%"); + } else { + achievements = db.getAchievements(getName); + } + var mapper = new ObjectMapper(); try { - var achievements = db.getAchievements(); - var mapper = new ObjectMapper(); if (achievements == null) { return new ResponseEntity(mapper.writeValueAsString(new InternalError("Could not get achievements from database")), HttpStatus.INTERNAL_SERVER_ERROR); } else { @@ -34,4 +41,25 @@ public class Controller { return new ResponseEntity("{}", HttpStatus.INTERNAL_SERVER_ERROR); } } + + @RequestMapping(value = "/games/{Name}", method = GET, produces = "text/html") + public ResponseEntity fetchGames(@PathVariable("Name") String getName) { + var games = (Games) null; + if (getName == null) { + games = db.getGames("%"); + } else { + games = db.getGames(getName); + } + var mapper = new ObjectMapper(); + try { + if (games == null) { + return new ResponseEntity(mapper.writeValueAsString(new InternalError("Could not get games from database")), HttpStatus.INTERNAL_SERVER_ERROR); + } else { + return new ResponseEntity(mapper.writeValueAsString(games), HttpStatus.OK); + } + } catch (JsonProcessingException e) { + e.printStackTrace(); + return new ResponseEntity("{}", HttpStatus.INTERNAL_SERVER_ERROR); + } + } } diff --git a/backend/src/main/java/achievements/data/Achievements.java b/backend/src/main/java/achievements/data/Achievements.java index 8ca8a96..7283925 100644 --- a/backend/src/main/java/achievements/data/Achievements.java +++ b/backend/src/main/java/achievements/data/Achievements.java @@ -9,8 +9,6 @@ public class Achievements { public static class Achievement { - @JsonProperty("GameID") - private int gameID; @JsonProperty("Name") private String name; @JsonProperty("Description") @@ -18,58 +16,49 @@ public class Achievements { @JsonProperty("Stages") private int stages; - public Achievement(int gameID, String name, String description, int stages) { - this.gameID = gameID; + public Achievement(String name, String description, int stages) { this.name = name; this.description = description; this.stages = stages; } - public int getGameID() { - return gameID; - } + // Start Getters/Setters + public String getName() { return name; } - public void setGameID(int gameID) { - this.gameID = gameID; - } + public void setName(String name) { this.name = name; } - public String getName() { - return name; - } + public String getDescription() { return description; } - public void setName(String name) { - this.name = name; - } + public void setDescription(String description) { this.description = description; } - public String getDescription() { - return description; - } + public int getStages() { return stages; } - public void setDescription(String description) { - this.description = description; - } - - public int getStages() { - return stages; - } - - public void setStages(int stages) { - this.stages = stages; - } + public void setStages(int stages) { this.stages = stages; } + // End Getters/Setters } - @JsonProperty("achievements") - List achievements; + @JsonProperty("GameID") + private int gameID; + @JsonProperty("GameName") + private String gameName; + @JsonProperty("Achievements") + private List achievements; - public Achievements() { - achievements = new ArrayList(); - } + public Achievements() { achievements = new ArrayList(); } - public List getAchievements() { - return achievements; - } + // Start Getters/Setters + public int getGameID() { return gameID; } - public void setAchievements(List achievements) { - this.achievements = achievements; - } + public void setGameID(int gameID) { this.gameID = gameID; } + + public String getGameName() { return gameName; } + + public void setGameName(String gameName) { this.gameName = gameName; } + + public List getAchievements() { return achievements; } + + public void setAchievements(List achievements) { this.achievements = achievements; } + // End Getters/Setters + + public void addAchievement(Achievement achievement) { this.achievements.add(achievement); }; } diff --git a/backend/src/main/java/achievements/data/Games.java b/backend/src/main/java/achievements/data/Games.java new file mode 100644 index 0000000..df0b743 --- /dev/null +++ b/backend/src/main/java/achievements/data/Games.java @@ -0,0 +1,57 @@ +package achievements.data; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.ArrayList; +import java.util.List; + +public class Games { + + public static class Game { + + @JsonProperty("ID") + private int id; + @JsonProperty("Name") + private String name; + @JsonProperty("Platforms") + private List platforms; + + public Game(int id, String name, String platform) { + this.id = id; + this.name = name; + this.platforms = new ArrayList<>(); + this.platforms.add(platform); + } + + // Start Getters/Setters + public int getId() { return id; } + + public void setId(int id) { this.id = id; } + + public String getName() { return name; } + + public void setName(String name) { this.name = name; } + + public List getPlatforms() { return platforms; } + + public void setPlatforms(List platforms) { this.platforms = platforms; } + + public void addToPlatforms(String platform) { this.platforms.add(platform); } + // End Getters/Setters + + } + + @JsonProperty("Games") + private List games; + + public Games() { games = new ArrayList(); } + + // Start Getters/Setters + public List getGames() { return games; } + + public void setGames(List games) { this.games = games; } + // End Getters/Setters + + public void addGame(Game game) { this.games.add(game); } + +} diff --git a/backend/src/main/java/achievements/services/DbService.java b/backend/src/main/java/achievements/services/DbService.java index 8043075..20eca43 100644 --- a/backend/src/main/java/achievements/services/DbService.java +++ b/backend/src/main/java/achievements/services/DbService.java @@ -1,12 +1,12 @@ package achievements.services; import achievements.data.Achievements; +import achievements.data.Games; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; -import java.sql.Connection; -import java.sql.SQLException; +import java.sql.*; @Service public class DbService { @@ -16,32 +16,72 @@ public class DbService { private Connection db; @PostConstruct - private void init() { - db = dbs.getConnection(); - } - - public Achievements getAchievements() { - final String QUERY = "SELECT * FROM [dbo].[Achievement]"; + private void init() { db = dbs.getConnection(); } + public Achievements getAchievements(String gameName) { try { - var statement = db.createStatement(); + // Create Query + CallableStatement stmt = db.prepareCall("{? = call GetAchievements(?)}"); + stmt.registerOutParameter(1, Types.INTEGER); + stmt.setString(2, gameName); + + // Read Result(s) + ResultSet results = stmt.executeQuery(); var achievements = new Achievements(); - var queryResults = statement.executeQuery(QUERY); - - while (queryResults.next()) { - achievements.getAchievements().add(new Achievements.Achievement( - queryResults.getInt("GameID"), - queryResults.getString("Name"), - queryResults.getString("Description"), - queryResults.getInt("Stages") - )); + while (results.next()) { + // Add Result(s) to data class + int achievementGameID = results.getInt("GameID"); + String achievementGameName = results.getString("GameName"); + String achievementName = results.getString("Name"); + String achievementDescription = results.getString("Description"); + int achievementStages = results.getInt("Stages"); + // Checks if getting from specific game or all achievements + if (!gameName.equals("%")) { + achievements.setGameID(achievementGameID); + achievements.setGameName(achievementGameName); + } + achievements.addAchievement(new Achievements.Achievement(achievementName, achievementDescription, achievementStages)); } - - statement.close(); + stmt.close(); return achievements; } catch (SQLException e) { e.printStackTrace(); return null; } } + + public Games getGames(String name) { + try { + // Create Query + CallableStatement stmt = db.prepareCall("{? = call GetGame(?)}"); + stmt.registerOutParameter(1, Types.INTEGER); + stmt.setString(2, name); + + // Read Result(s) + ResultSet results = stmt.executeQuery(); + var games = new Games(); + while (results.next()) { + // Add Result(s) to data class + int gameID = results.getInt("ID"); + String gameName = results.getString("Name"); + String gamePlatform = results.getString("PlatformName"); + if (!games.getGames().isEmpty()) { + var lastGame = games.getGames().get(games.getGames().size()-1); + if (lastGame.getId() == gameID) { + lastGame.addToPlatforms(gamePlatform); + } else { + games.addGame(new Games.Game(gameID,gameName,gamePlatform)); + } + } else { + games.addGame(new Games.Game(gameID,gameName,gamePlatform)); + } + + } + stmt.close(); + return games; + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } }