Revamped UI...ish
This commit is contained in:
parent
13736d0ef9
commit
40a0e4046a
26 changed files with 2053 additions and 621 deletions
|
@ -0,0 +1,106 @@
|
|||
package achievements.services;
|
||||
|
||||
import achievements.data.User;
|
||||
import achievements.misc.DbConnectionService;
|
||||
import achievements.misc.Password;
|
||||
import achievements.misc.SessionManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.sql.*;
|
||||
|
||||
@Service
|
||||
public class AuthenticationService {
|
||||
|
||||
public static class LoginResponse {
|
||||
public int status;
|
||||
public Integer id;
|
||||
|
||||
public LoginResponse() {
|
||||
this.status = 0;
|
||||
this.id = null;
|
||||
}
|
||||
|
||||
public LoginResponse(int status) {
|
||||
this.status = status;
|
||||
this.id = null;
|
||||
}
|
||||
|
||||
public LoginResponse(int status, int id) {
|
||||
this.status = status;
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
public static final LoginResponse GUEST = new LoginResponse();
|
||||
|
||||
@Autowired
|
||||
private DbConnectionService dbs;
|
||||
private Connection db;
|
||||
|
||||
private SessionManager session;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
db = dbs.getConnection();
|
||||
session = new SessionManager();
|
||||
}
|
||||
|
||||
public LoginResponse createUser(User user) {
|
||||
if (!user.getEmail().matches(".+@\\w+\\.\\w+")) {
|
||||
return new LoginResponse(2);
|
||||
}
|
||||
|
||||
try {
|
||||
var statement = db.prepareCall("{? = call CreateUser(?, ?, ?, ?, ?)}");
|
||||
statement.registerOutParameter(1, Types.INTEGER);
|
||||
statement.setString(2, user.getEmail());
|
||||
statement.setString(3, user.getUsername());
|
||||
|
||||
var password = Password.generate(user.getPassword());
|
||||
statement.setString(4, password.salt);
|
||||
statement.setString(5, password.hash);
|
||||
|
||||
statement.registerOutParameter(6, Types.INTEGER);
|
||||
|
||||
statement.execute();
|
||||
var response = new LoginResponse(statement.getInt(1), statement.getInt(6));
|
||||
statement.close();
|
||||
|
||||
return response;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new LoginResponse(-1);
|
||||
}
|
||||
|
||||
public LoginResponse login(User user) {
|
||||
var response = new LoginResponse(-1);
|
||||
try {
|
||||
var statement = db.prepareCall("{call GetUserLogin(?)}");
|
||||
statement.setString(1, user.email);
|
||||
|
||||
var result = statement.executeQuery();
|
||||
if (result.next()) {
|
||||
var salt = result.getString("Salt");
|
||||
var hash = result.getString("Password");
|
||||
if (Password.validate(salt, user.getPassword(), hash)) {
|
||||
response = new LoginResponse(0, result.getInt("ID"));
|
||||
} else {
|
||||
response = new LoginResponse(2);
|
||||
}
|
||||
} else {
|
||||
response = new LoginResponse(1);
|
||||
}
|
||||
statement.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
public SessionManager session() {
|
||||
return session;
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
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.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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,134 +1,92 @@
|
|||
package achievements.services;
|
||||
|
||||
import achievements.data.Achievements;
|
||||
import achievements.data.Games;
|
||||
import achievements.data.User;
|
||||
import achievements.misc.Password;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.sql.*;
|
||||
|
||||
@Service
|
||||
public class DbService {
|
||||
|
||||
@Autowired
|
||||
private DbConnectionService dbs;
|
||||
private Connection db;
|
||||
|
||||
@PostConstruct
|
||||
private void init() { db = dbs.getConnection(); }
|
||||
|
||||
public Achievements getAchievements(String gameName) {
|
||||
try {
|
||||
// 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();
|
||||
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);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public int createUser(User user) {
|
||||
try {
|
||||
var statement = db.prepareCall("{? = call CreateUser(?, ?, ?, ?)}");
|
||||
statement.registerOutParameter(1, Types.INTEGER);
|
||||
statement.setString(2, user.getEmail());
|
||||
statement.setString(3, user.getUsername());
|
||||
|
||||
var password = Password.generate(user.getPassword());
|
||||
statement.setString(4, password.salt);
|
||||
statement.setString(5, password.hash);
|
||||
|
||||
statement.execute();
|
||||
var code = statement.getInt(1);
|
||||
statement.close();
|
||||
|
||||
return code;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int login(User user) {
|
||||
try {
|
||||
var statement = db.prepareStatement("SELECT Salt, Password FROM [dbo].[User] WHERE Email = ?");
|
||||
statement.setString(1, user.email);
|
||||
|
||||
var result = statement.executeQuery();
|
||||
if (result.next()) {
|
||||
var salt = result.getString("Salt");
|
||||
var hash = result.getString("Password");
|
||||
if (Password.validate(salt, user.getPassword(), hash)) {
|
||||
return 0;
|
||||
} else {
|
||||
return 2;
|
||||
}
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
package achievements.services;
|
||||
|
||||
import achievements.data.Achievements;
|
||||
import achievements.data.Games;
|
||||
import achievements.misc.DbConnectionService;
|
||||
import achievements.misc.SessionManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.sql.*;
|
||||
|
||||
@Service
|
||||
public class DbService {
|
||||
|
||||
@Autowired
|
||||
private DbConnectionService dbs;
|
||||
private Connection db;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
db = dbs.getConnection();
|
||||
}
|
||||
|
||||
public Achievements getAchievements(String gameName) {
|
||||
try {
|
||||
// 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();
|
||||
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);
|
||||
}
|
||||
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…
Add table
Add a link
Reference in a new issue