diff --git a/backend/.gitignore b/backend/.gitignore new file mode 100644 index 0000000..ec60747 --- /dev/null +++ b/backend/.gitignore @@ -0,0 +1,21 @@ +# Intellij project files +.idea/ + +# Build files +build/ + +# Gradle files +.gradle/ +gradle/ + +gradlew +gradlew.bat + +# Eclipse project files +.classpath +.project + +.settings/ + +# Local property file +src/main/resources/application-db.properties diff --git a/backend/build.gradle b/backend/build.gradle new file mode 100644 index 0000000..d2148e1 --- /dev/null +++ b/backend/build.gradle @@ -0,0 +1,24 @@ +plugins { + id 'org.springframework.boot' version '2.3.3.RELEASE' + id 'io.spring.dependency-management' version '1.0.8.RELEASE' + id 'java' +} + +group = 'edu.rh.achievements' +version = '0.0.1-SNAPSHOT' + +sourceCompatibility = 1.15 +targetCompatibility = 1.15 + +repositories { + mavenCentral() +} + +dependencies { + compile 'org.springframework.boot:spring-boot-starter-web' + compile 'com.microsoft.sqlserver:mssql-jdbc' +} + +tasks.getByName('bootRun') { + systemProperty 'spring.profiles.active', 'db' +} diff --git a/backend/src/main/java/achievements/Application.java b/backend/src/main/java/achievements/Application.java new file mode 100644 index 0000000..44b5d01 --- /dev/null +++ b/backend/src/main/java/achievements/Application.java @@ -0,0 +1,19 @@ +package achievements; + +import achievements.services.DbConnectionService; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + var context = SpringApplication.run(Application.class, args); + + // Verify the database connection succeeded + var db = context.getBean(DbConnectionService.class); + if (db.getConnection() == null) { + SpringApplication.exit(context, () -> 0); + } + } +} \ No newline at end of file diff --git a/backend/src/main/java/achievements/controllers/Controller.java b/backend/src/main/java/achievements/controllers/Controller.java new file mode 100644 index 0000000..723001a --- /dev/null +++ b/backend/src/main/java/achievements/controllers/Controller.java @@ -0,0 +1,37 @@ +package achievements.controllers; + +import achievements.data.InternalError; +import achievements.services.DbService; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import static org.springframework.web.bind.annotation.RequestMethod.GET; + +@RestController +public class Controller { + + @Autowired + private DbService db; + + public Controller() {} + + @RequestMapping(value = "/achievements", method = GET, produces = "application/json") + public ResponseEntity index() { + 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 { + return new ResponseEntity(mapper.writeValueAsString(achievements), 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 new file mode 100644 index 0000000..8ca8a96 --- /dev/null +++ b/backend/src/main/java/achievements/data/Achievements.java @@ -0,0 +1,75 @@ +package achievements.data; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.ArrayList; +import java.util.List; + +public class Achievements { + + public static class Achievement { + + @JsonProperty("GameID") + private int gameID; + @JsonProperty("Name") + private String name; + @JsonProperty("Description") + private String description; + @JsonProperty("Stages") + private int stages; + + public Achievement(int gameID, String name, String description, int stages) { + this.gameID = gameID; + this.name = name; + this.description = description; + this.stages = stages; + } + + public int getGameID() { + return gameID; + } + + public void setGameID(int gameID) { + this.gameID = gameID; + } + + 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; + } + } + + @JsonProperty("achievements") + List achievements; + + public Achievements() { + achievements = new ArrayList(); + } + + public List getAchievements() { + return achievements; + } + + public void setAchievements(List achievements) { + this.achievements = achievements; + } +} diff --git a/backend/src/main/java/achievements/data/InternalError.java b/backend/src/main/java/achievements/data/InternalError.java new file mode 100644 index 0000000..16486a5 --- /dev/null +++ b/backend/src/main/java/achievements/data/InternalError.java @@ -0,0 +1,21 @@ +package achievements.data; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class InternalError { + + @JsonProperty + private String message; + + public InternalError(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/backend/src/main/java/achievements/services/DbConnectionService.java b/backend/src/main/java/achievements/services/DbConnectionService.java new file mode 100644 index 0000000..bb09ea4 --- /dev/null +++ b/backend/src/main/java/achievements/services/DbConnectionService.java @@ -0,0 +1,57 @@ +package achievements.services; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import com.microsoft.sqlserver.jdbc.SQLServerDataSource; + +@Component +public class DbConnectionService { + + private Connection connection; + + @Value("${database.server}") + private String serverName; + @Value("${database.name}") + private String databaseName; + @Value("${database.user.name}") + private String username; + @Value("${database.user.password}") + private String password; + + public DbConnectionService() {} + + @PostConstruct + public void connect() { + try { + var dataSource = new SQLServerDataSource(); + dataSource.setServerName (serverName ); + dataSource.setDatabaseName(databaseName); + dataSource.setUser (username ); + dataSource.setPassword (password ); + connection = dataSource.getConnection(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public Connection getConnection() { + return this.connection; + } + + @PreDestroy + public void disconnect() { + try { + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } +} diff --git a/backend/src/main/java/achievements/services/DbService.java b/backend/src/main/java/achievements/services/DbService.java new file mode 100644 index 0000000..8043075 --- /dev/null +++ b/backend/src/main/java/achievements/services/DbService.java @@ -0,0 +1,47 @@ +package achievements.services; + +import achievements.data.Achievements; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.sql.Connection; +import java.sql.SQLException; + +@Service +public class DbService { + + @Autowired + private DbConnectionService dbs; + private Connection db; + + @PostConstruct + private void init() { + db = dbs.getConnection(); + } + + public Achievements getAchievements() { + final String QUERY = "SELECT * FROM [dbo].[Achievement]"; + + try { + var statement = db.createStatement(); + 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") + )); + } + + statement.close(); + return achievements; + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties new file mode 100644 index 0000000..004fec9 --- /dev/null +++ b/backend/src/main/resources/application.properties @@ -0,0 +1,2 @@ +server.port = 8000 +spring.application.name = Achievements Project diff --git a/front_end/res/dummy_game.png~ b/front_end/res/dummy_game.png~ deleted file mode 100644 index 518adad..0000000 Binary files a/front_end/res/dummy_game.png~ and /dev/null differ diff --git a/front_end/index.html b/frontend/index.html similarity index 100% rename from front_end/index.html rename to frontend/index.html diff --git a/front_end/res/dummy_achievement.png b/frontend/res/dummy_achievement.png similarity index 100% rename from front_end/res/dummy_achievement.png rename to frontend/res/dummy_achievement.png diff --git a/front_end/res/dummy_game.png b/frontend/res/dummy_game.png similarity index 100% rename from front_end/res/dummy_game.png rename to frontend/res/dummy_game.png diff --git a/front_end/res/psn.png b/frontend/res/psn.png similarity index 100% rename from front_end/res/psn.png rename to frontend/res/psn.png diff --git a/front_end/res/steam.png b/frontend/res/steam.png similarity index 100% rename from front_end/res/steam.png rename to frontend/res/steam.png diff --git a/front_end/res/temp_pfp.png b/frontend/res/temp_pfp.png similarity index 100% rename from front_end/res/temp_pfp.png rename to frontend/res/temp_pfp.png diff --git a/front_end/res/xbox.png b/frontend/res/xbox.png similarity index 100% rename from front_end/res/xbox.png rename to frontend/res/xbox.png diff --git a/front_end/scripts/index.js b/frontend/scripts/index.js similarity index 100% rename from front_end/scripts/index.js rename to frontend/scripts/index.js diff --git a/front_end/styles/index.css b/frontend/styles/index.css similarity index 100% rename from front_end/styles/index.css rename to frontend/styles/index.css