Initial backend
This commit is contained in:
parent
f10ca87343
commit
a06a558d7b
19 changed files with 303 additions and 0 deletions
backend/src/main/java/achievements/services
|
@ -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
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue