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(); config.load();
Class.forName("com.mysql.cj.jdbc.Driver"); 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(); 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); ServerSocket server = new ServerSocket(80);

View file

@ -1,11 +1,13 @@
package be.jeffcheasey88.peeratcode.repository; package be.jeffcheasey88.peeratcode.repository;
import be.jeffcheasey88.peeratcode.Configuration;
import be.jeffcheasey88.peeratcode.model.Chapter; import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.Puzzle; import be.jeffcheasey88.peeratcode.model.Puzzle;
import com.password4j.Hash; import com.password4j.Hash;
import com.password4j.Password; import com.password4j.Password;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; 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_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 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 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) { public DatabaseRepository(Configuration config){
this.con = con; 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 { private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException {
@ -38,6 +47,7 @@ public class DatabaseRepository {
private List<Puzzle> getPuzzlesInChapter(int id) throws SQLException { private List<Puzzle> getPuzzlesInChapter(int id) throws SQLException {
List<Puzzle> puzzles = new ArrayList<>(); List<Puzzle> puzzles = new ArrayList<>();
ensureConnection();
PreparedStatement puzzleStmt = con.prepareStatement(PUZZLES_IN_CHAPTER_QUERY); PreparedStatement puzzleStmt = con.prepareStatement(PUZZLES_IN_CHAPTER_QUERY);
puzzleStmt.setInt(1, id); puzzleStmt.setInt(1, id);
ResultSet puzzleResult = puzzleStmt.executeQuery(); ResultSet puzzleResult = puzzleStmt.executeQuery();
@ -55,6 +65,7 @@ public class DatabaseRepository {
*/ */
public Puzzle getPuzzle(int id) { public Puzzle getPuzzle(int id) {
try { try {
ensureConnection();
PreparedStatement puzzleStmt = con.prepareStatement(SPECIFIC_PUZZLE_QUERY); PreparedStatement puzzleStmt = con.prepareStatement(SPECIFIC_PUZZLE_QUERY);
puzzleStmt.setInt(1, id); puzzleStmt.setInt(1, id);
ResultSet puzzleResult = puzzleStmt.executeQuery(); ResultSet puzzleResult = puzzleStmt.executeQuery();
@ -75,6 +86,7 @@ public class DatabaseRepository {
*/ */
public Chapter getChapter(int id) { public Chapter getChapter(int id) {
try { try {
ensureConnection();
PreparedStatement chapterStmt = con.prepareStatement(SPECIFIC_CHAPTER_QUERY); PreparedStatement chapterStmt = con.prepareStatement(SPECIFIC_CHAPTER_QUERY);
chapterStmt.setInt(1, id); chapterStmt.setInt(1, id);
ResultSet chapterResult = chapterStmt.executeQuery(); ResultSet chapterResult = chapterStmt.executeQuery();
@ -98,6 +110,7 @@ public class DatabaseRepository {
public List<Chapter> getAllChapters() { public List<Chapter> getAllChapters() {
try { try {
List<Chapter> chapterList = new ArrayList<>(); List<Chapter> chapterList = new ArrayList<>();
ensureConnection();
PreparedStatement chapterStmt = con.prepareStatement(ALL_CHAPTERS_QUERY); PreparedStatement chapterStmt = con.prepareStatement(ALL_CHAPTERS_QUERY);
ResultSet chapterResult = chapterStmt.executeQuery(); ResultSet chapterResult = chapterStmt.executeQuery();
while (chapterResult.next()) { while (chapterResult.next()) {
@ -134,6 +147,7 @@ public class DatabaseRepository {
private boolean checkAvailability(String queriedString, String correspondingQuery) { private boolean checkAvailability(String queriedString, String correspondingQuery) {
try { try {
ensureConnection();
PreparedStatement statement = con.prepareStatement(correspondingQuery); PreparedStatement statement = con.prepareStatement(correspondingQuery);
statement.setString(1, queriedString); statement.setString(1, queriedString);
ResultSet result = statement.executeQuery(); 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) { 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(); Hash hash = Password.hash(password).withArgon2();
try { try {
ensureConnection();
PreparedStatement statement = con.prepareStatement(REGISTER_QUERY); PreparedStatement statement = con.prepareStatement(REGISTER_QUERY);
statement.setString(1, pseudo); statement.setString(1, pseudo);
statement.setString(2, email); statement.setString(2, email);
@ -179,15 +194,15 @@ public class DatabaseRepository {
/** /**
* Login a user * Login a user
* *
* @param email The email of the user * @param username The username of the user
* @param password The password 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) * @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 { try {
PreparedStatement statement = con.prepareStatement(PASSWORD_FOR_EMAIL_QUERY); ensureConnection();
statement.setString(1, email); PreparedStatement statement = con.prepareStatement(CHECK_PASSWORD);
statement.setString(2, password); statement.setString(1, username);
ResultSet result = statement.executeQuery(); ResultSet result = statement.executeQuery();
if (result.next()) { if (result.next()) {
String hashedPassword = result.getString("passwd"); String hashedPassword = result.getString("passwd");