Initial backend
21
backend/.gitignore
vendored
Normal file
|
@ -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
|
24
backend/build.gradle
Normal file
|
@ -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'
|
||||
}
|
19
backend/src/main/java/achievements/Application.java
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
75
backend/src/main/java/achievements/data/Achievements.java
Normal file
|
@ -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<Achievement> achievements;
|
||||
|
||||
public Achievements() {
|
||||
achievements = new ArrayList<Achievement>();
|
||||
}
|
||||
|
||||
public List<Achievement> getAchievements() {
|
||||
return achievements;
|
||||
}
|
||||
|
||||
public void setAchievements(List<Achievement> achievements) {
|
||||
this.achievements = achievements;
|
||||
}
|
||||
}
|
21
backend/src/main/java/achievements/data/InternalError.java
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
47
backend/src/main/java/achievements/services/DbService.java
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
2
backend/src/main/resources/application.properties
Normal file
|
@ -0,0 +1,2 @@
|
|||
server.port = 8000
|
||||
spring.application.name = Achievements Project
|
Before Width: | Height: | Size: 73 KiB |
Before Width: | Height: | Size: 91 KiB After Width: | Height: | Size: 91 KiB |
Before Width: | Height: | Size: 113 KiB After Width: | Height: | Size: 113 KiB |
Before Width: | Height: | Size: 88 KiB After Width: | Height: | Size: 88 KiB |
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 79 KiB |
Before Width: | Height: | Size: 41 KiB After Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 135 KiB After Width: | Height: | Size: 135 KiB |