Fixing Jules problems again & Ensure sql connection

This commit is contained in:
jeffcheasey88 2023-02-18 10:42:10 +01:00
parent c953971ef3
commit ee602af486
2 changed files with 25 additions and 11 deletions

View file

@ -29,7 +29,6 @@ public class Main {
config.load();
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://"+config.getDbHost()+":"+config.getDbPort()+"/"+config.getDbDatabase()+"",config.getDbUser(), config.getDbPassword());
Router router = new Router();
@ -47,7 +46,7 @@ public class Main {
}
});
initRoutes(router, new DatabaseRepository(con));
initRoutes(router, new DatabaseRepository(config));
ServerSocket server = new ServerSocket(80);

View file

@ -1,11 +1,13 @@
package be.jeffcheasey88.peeratcode.repository;
import be.jeffcheasey88.peeratcode.Configuration;
import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.Puzzle;
import com.password4j.Hash;
import com.password4j.Password;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -20,12 +22,19 @@ public class DatabaseRepository {
private static final String CHECK_PSEUDO_AVAILABLE_QUERY = "SELECT * FROM players WHERE pseudo = ?";
private static final String CHECK_EMAIL_AVAILABLE_QUERY = "SELECT * FROM players WHERE email = ?";
private static final String REGISTER_QUERY = "INSERT INTO players (pseudo, email, passwd, firstname, lastname, description, sgroup, avatar) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
private static final String PASSWORD_FOR_EMAIL_QUERY = "SELECT passwd FROM players WHERE pseudo = ?";
private static final String CHECK_PASSWORD = "SELECT passwd FROM players WHERE pseudo=?";
private final Connection con;
private Connection con;
private Configuration config;
public DatabaseRepository(Connection con) {
this.con = con;
public DatabaseRepository(Configuration config){
this.config = config;
}
private void ensureConnection() throws SQLException{
if(con == null || (!con.isValid(5))){
this.con = DriverManager.getConnection("jdbc:mysql://"+config.getDbHost()+":"+config.getDbPort()+"/"+config.getDbDatabase()+"",config.getDbUser(), config.getDbPassword());
}
}
private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException {
@ -38,6 +47,7 @@ public class DatabaseRepository {
private List<Puzzle> getPuzzlesInChapter(int id) throws SQLException {
List<Puzzle> puzzles = new ArrayList<>();
ensureConnection();
PreparedStatement puzzleStmt = con.prepareStatement(PUZZLES_IN_CHAPTER_QUERY);
puzzleStmt.setInt(1, id);
ResultSet puzzleResult = puzzleStmt.executeQuery();
@ -55,6 +65,7 @@ public class DatabaseRepository {
*/
public Puzzle getPuzzle(int id) {
try {
ensureConnection();
PreparedStatement puzzleStmt = con.prepareStatement(SPECIFIC_PUZZLE_QUERY);
puzzleStmt.setInt(1, id);
ResultSet puzzleResult = puzzleStmt.executeQuery();
@ -75,6 +86,7 @@ public class DatabaseRepository {
*/
public Chapter getChapter(int id) {
try {
ensureConnection();
PreparedStatement chapterStmt = con.prepareStatement(SPECIFIC_CHAPTER_QUERY);
chapterStmt.setInt(1, id);
ResultSet chapterResult = chapterStmt.executeQuery();
@ -98,6 +110,7 @@ public class DatabaseRepository {
public List<Chapter> getAllChapters() {
try {
List<Chapter> chapterList = new ArrayList<>();
ensureConnection();
PreparedStatement chapterStmt = con.prepareStatement(ALL_CHAPTERS_QUERY);
ResultSet chapterResult = chapterStmt.executeQuery();
while (chapterResult.next()) {
@ -134,6 +147,7 @@ public class DatabaseRepository {
private boolean checkAvailability(String queriedString, String correspondingQuery) {
try {
ensureConnection();
PreparedStatement statement = con.prepareStatement(correspondingQuery);
statement.setString(1, queriedString);
ResultSet result = statement.executeQuery();
@ -160,6 +174,7 @@ public class DatabaseRepository {
public boolean register(String pseudo, String email, String password, String firstname, String lastname, String description, String sgroup, String avatar) {
Hash hash = Password.hash(password).withArgon2();
try {
ensureConnection();
PreparedStatement statement = con.prepareStatement(REGISTER_QUERY);
statement.setString(1, pseudo);
statement.setString(2, email);
@ -179,15 +194,15 @@ public class DatabaseRepository {
/**
* Login a user
*
* @param email The email of the user
* @param username The username of the user
* @param password The password of the user
* @return True if the user's information are correct, false otherwise (or if an error occurred)
*/
public boolean login(String email, String password) {
public boolean login(String username, String password) {
try {
PreparedStatement statement = con.prepareStatement(PASSWORD_FOR_EMAIL_QUERY);
statement.setString(1, email);
statement.setString(2, password);
ensureConnection();
PreparedStatement statement = con.prepareStatement(CHECK_PASSWORD);
statement.setString(1, username);
ResultSet result = statement.executeQuery();
if (result.next()) {
String hashedPassword = result.getString("passwd");