Created and updated methods to pull data from db.
This commit is contained in:
parent
e7e5168073
commit
c160703048
4 changed files with 179 additions and 65 deletions
|
@ -1,5 +1,7 @@
|
||||||
package achievements.controllers;
|
package achievements.controllers;
|
||||||
|
|
||||||
|
import achievements.data.Achievements;
|
||||||
|
import achievements.data.Games;
|
||||||
import achievements.data.InternalError;
|
import achievements.data.InternalError;
|
||||||
import achievements.services.DbService;
|
import achievements.services.DbService;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
@ -19,11 +21,16 @@ public class Controller {
|
||||||
|
|
||||||
public Controller() {}
|
public Controller() {}
|
||||||
|
|
||||||
@RequestMapping(value = "/achievements", method = GET, produces = "application/json")
|
@RequestMapping(value = "/achievements/{Name}", method = GET, produces = "application/json")
|
||||||
public ResponseEntity<String> index() {
|
public ResponseEntity<String> 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 {
|
try {
|
||||||
var achievements = db.getAchievements();
|
|
||||||
var mapper = new ObjectMapper();
|
|
||||||
if (achievements == null) {
|
if (achievements == null) {
|
||||||
return new ResponseEntity(mapper.writeValueAsString(new InternalError("Could not get achievements from database")), HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity(mapper.writeValueAsString(new InternalError("Could not get achievements from database")), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
} else {
|
} else {
|
||||||
|
@ -34,4 +41,25 @@ public class Controller {
|
||||||
return new ResponseEntity("{}", HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity("{}", HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/games/{Name}", method = GET, produces = "text/html")
|
||||||
|
public ResponseEntity<String> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,6 @@ public class Achievements {
|
||||||
|
|
||||||
public static class Achievement {
|
public static class Achievement {
|
||||||
|
|
||||||
@JsonProperty("GameID")
|
|
||||||
private int gameID;
|
|
||||||
@JsonProperty("Name")
|
@JsonProperty("Name")
|
||||||
private String name;
|
private String name;
|
||||||
@JsonProperty("Description")
|
@JsonProperty("Description")
|
||||||
|
@ -18,58 +16,49 @@ public class Achievements {
|
||||||
@JsonProperty("Stages")
|
@JsonProperty("Stages")
|
||||||
private int stages;
|
private int stages;
|
||||||
|
|
||||||
public Achievement(int gameID, String name, String description, int stages) {
|
public Achievement(String name, String description, int stages) {
|
||||||
this.gameID = gameID;
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.stages = stages;
|
this.stages = stages;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getGameID() {
|
// Start Getters/Setters
|
||||||
return gameID;
|
public String getName() { return name; }
|
||||||
}
|
|
||||||
|
|
||||||
public void setGameID(int gameID) {
|
public void setName(String name) { this.name = name; }
|
||||||
this.gameID = gameID;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
public String getDescription() { return description; }
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
public void setDescription(String description) { this.description = description; }
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDescription() {
|
public int getStages() { return stages; }
|
||||||
return description;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDescription(String description) {
|
public void setStages(int stages) { this.stages = stages; }
|
||||||
this.description = description;
|
// End Getters/Setters
|
||||||
}
|
|
||||||
|
|
||||||
public int getStages() {
|
|
||||||
return stages;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStages(int stages) {
|
|
||||||
this.stages = stages;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonProperty("achievements")
|
@JsonProperty("GameID")
|
||||||
List<Achievement> achievements;
|
private int gameID;
|
||||||
|
@JsonProperty("GameName")
|
||||||
|
private String gameName;
|
||||||
|
@JsonProperty("Achievements")
|
||||||
|
private List<Achievement> achievements;
|
||||||
|
|
||||||
public Achievements() {
|
public Achievements() { achievements = new ArrayList<Achievement>(); }
|
||||||
achievements = new ArrayList<Achievement>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Achievement> getAchievements() {
|
// Start Getters/Setters
|
||||||
return achievements;
|
public int getGameID() { return gameID; }
|
||||||
}
|
|
||||||
|
|
||||||
public void setAchievements(List<Achievement> achievements) {
|
public void setGameID(int gameID) { this.gameID = gameID; }
|
||||||
this.achievements = achievements;
|
|
||||||
}
|
public String getGameName() { return gameName; }
|
||||||
|
|
||||||
|
public void setGameName(String gameName) { this.gameName = gameName; }
|
||||||
|
|
||||||
|
public List<Achievement> getAchievements() { return achievements; }
|
||||||
|
|
||||||
|
public void setAchievements(List<Achievement> achievements) { this.achievements = achievements; }
|
||||||
|
// End Getters/Setters
|
||||||
|
|
||||||
|
public void addAchievement(Achievement achievement) { this.achievements.add(achievement); };
|
||||||
}
|
}
|
||||||
|
|
57
backend/src/main/java/achievements/data/Games.java
Normal file
57
backend/src/main/java/achievements/data/Games.java
Normal file
|
@ -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<String> 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<String> getPlatforms() { return platforms; }
|
||||||
|
|
||||||
|
public void setPlatforms(List<String> platforms) { this.platforms = platforms; }
|
||||||
|
|
||||||
|
public void addToPlatforms(String platform) { this.platforms.add(platform); }
|
||||||
|
// End Getters/Setters
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("Games")
|
||||||
|
private List<Game> games;
|
||||||
|
|
||||||
|
public Games() { games = new ArrayList<Game>(); }
|
||||||
|
|
||||||
|
// Start Getters/Setters
|
||||||
|
public List<Game> getGames() { return games; }
|
||||||
|
|
||||||
|
public void setGames(List<Game> games) { this.games = games; }
|
||||||
|
// End Getters/Setters
|
||||||
|
|
||||||
|
public void addGame(Game game) { this.games.add(game); }
|
||||||
|
|
||||||
|
}
|
|
@ -1,12 +1,12 @@
|
||||||
package achievements.services;
|
package achievements.services;
|
||||||
|
|
||||||
import achievements.data.Achievements;
|
import achievements.data.Achievements;
|
||||||
|
import achievements.data.Games;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import java.sql.Connection;
|
import java.sql.*;
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class DbService {
|
public class DbService {
|
||||||
|
@ -16,32 +16,72 @@ public class DbService {
|
||||||
private Connection db;
|
private Connection db;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
private void init() {
|
private void init() { db = dbs.getConnection(); }
|
||||||
db = dbs.getConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Achievements getAchievements() {
|
|
||||||
final String QUERY = "SELECT * FROM [dbo].[Achievement]";
|
|
||||||
|
|
||||||
|
public Achievements getAchievements(String gameName) {
|
||||||
try {
|
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 achievements = new Achievements();
|
||||||
var queryResults = statement.executeQuery(QUERY);
|
while (results.next()) {
|
||||||
|
// Add Result(s) to data class
|
||||||
while (queryResults.next()) {
|
int achievementGameID = results.getInt("GameID");
|
||||||
achievements.getAchievements().add(new Achievements.Achievement(
|
String achievementGameName = results.getString("GameName");
|
||||||
queryResults.getInt("GameID"),
|
String achievementName = results.getString("Name");
|
||||||
queryResults.getString("Name"),
|
String achievementDescription = results.getString("Description");
|
||||||
queryResults.getString("Description"),
|
int achievementStages = results.getInt("Stages");
|
||||||
queryResults.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));
|
||||||
}
|
}
|
||||||
|
stmt.close();
|
||||||
statement.close();
|
|
||||||
return achievements;
|
return achievements;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue