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;
|
||||
|
||||
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<String> index() {
|
||||
try {
|
||||
var achievements = db.getAchievements();
|
||||
@RequestMapping(value = "/achievements/{Name}", method = GET, produces = "application/json")
|
||||
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 {
|
||||
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<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 {
|
||||
|
||||
@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 setName(String name) { this.name = name; }
|
||||
|
||||
public String getDescription() { return description; }
|
||||
|
||||
public void setDescription(String description) { this.description = description; }
|
||||
|
||||
public int getStages() { return stages; }
|
||||
|
||||
public void setStages(int stages) { this.stages = stages; }
|
||||
// End Getters/Setters
|
||||
}
|
||||
|
||||
public void setGameID(int gameID) {
|
||||
this.gameID = gameID;
|
||||
}
|
||||
@JsonProperty("GameID")
|
||||
private int gameID;
|
||||
@JsonProperty("GameName")
|
||||
private String gameName;
|
||||
@JsonProperty("Achievements")
|
||||
private List<Achievement> achievements;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public Achievements() { achievements = new ArrayList<Achievement>(); }
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
// Start Getters/Setters
|
||||
public int getGameID() { return gameID; }
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
public void setGameID(int gameID) { this.gameID = gameID; }
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
public String getGameName() { return gameName; }
|
||||
|
||||
public int getStages() {
|
||||
return stages;
|
||||
}
|
||||
public void setGameName(String gameName) { this.gameName = gameName; }
|
||||
|
||||
public void setStages(int stages) {
|
||||
this.stages = stages;
|
||||
}
|
||||
}
|
||||
public List<Achievement> getAchievements() { return achievements; }
|
||||
|
||||
@JsonProperty("achievements")
|
||||
List<Achievement> achievements;
|
||||
public void setAchievements(List<Achievement> achievements) { this.achievements = achievements; }
|
||||
// End Getters/Setters
|
||||
|
||||
public Achievements() {
|
||||
achievements = new ArrayList<Achievement>();
|
||||
}
|
||||
|
||||
public List<Achievement> getAchievements() {
|
||||
return achievements;
|
||||
}
|
||||
|
||||
public void setAchievements(List<Achievement> achievements) {
|
||||
this.achievements = achievements;
|
||||
}
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
statement.close();
|
||||
achievements.addAchievement(new Achievements.Achievement(achievementName, achievementDescription, achievementStages));
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue