diff --git a/WEB-INF/src/dao/ThreadDAO.java b/WEB-INF/src/dao/ThreadDAO.java index 22140beae20180a423aedb2f5bde4c4ecea6f336..3c5ae83b48744ea65cf9106bc81258a21398a035 100644 --- a/WEB-INF/src/dao/ThreadDAO.java +++ b/WEB-INF/src/dao/ThreadDAO.java @@ -54,6 +54,22 @@ public class ThreadDAO { return thread; } + public MyThread getThreadByName(String threadName) throws SQLException { + MyThread thread = null; + PreparedStatement ps = this.con.prepareStatement("SELECT threadID, threadName FROM thread WHERE threadName = ?"); + try { + ps.setString(1, threadName); + ResultSet rs = ps.executeQuery(); + while (rs.next()) { + thread = new MyThread(rs.getInt("threadID"), threadName); + } + } + catch (SQLException sqle) { + sqle.getStackTrace(); + } + return thread; + } + public List<Message> getAllMessagesFromThread(int threadId){ ArrayList<Message> messages = new ArrayList<>(); try{ @@ -101,11 +117,13 @@ public class ThreadDAO { // Créer un thread public void createThread(User user, String threadName) throws SQLException { PreparedStatement ps = this.con.prepareStatement("INSERT INTO thread (userID_thread, threadName) VALUES(?, ?)"); + UserDAO userDAO = new UserDAO(); try { - ps.setInt(1, +user.getId()); + ps.setInt(1, user.getId()); ps.setString(2, threadName); - System.out.println(ps); ps.executeUpdate(); + MyThread thread = getThreadByName(threadName); + userDAO.followThread(user, thread); } catch (SQLException sqle) { sqle.getStackTrace(); diff --git a/WEB-INF/src/dao/UserDAO.java b/WEB-INF/src/dao/UserDAO.java index b1d32f676a8f94f477163978b4059bb97fffc055..54658038e5c7e1c35a15ad51fd7dc1d793b6c163 100644 --- a/WEB-INF/src/dao/UserDAO.java +++ b/WEB-INF/src/dao/UserDAO.java @@ -19,11 +19,13 @@ public class UserDAO { public void createUser(String username, String password){ try{ - PreparedStatement stmt = this.con.prepareStatement("INSERT INTO userAccount (username, password) VALUES (?,?)"); - stmt.setString(1, username); - stmt.setString(2, password); - - stmt.executeUpdate(); + if (getUserByLogs(username, password) == null) { + PreparedStatement stmt = this.con.prepareStatement("INSERT INTO userAccount (username, password) VALUES (?,?)"); + stmt.setString(1, username); + stmt.setString(2, password); + + stmt.executeUpdate(); + } } catch (SQLException e) { throw new RuntimeException(e); } @@ -71,7 +73,7 @@ public class UserDAO { public User getUserByLogs(String username, String password){ - User user = new User(0,null,null); + User user = null; try{ PreparedStatement stmt = this.con.prepareStatement("SELECT * FROM userAccount WHERE username=? AND password=?"); @@ -142,12 +144,15 @@ public class UserDAO { } // S'abonner à un thread - public void followThread(User user, Thread thread) throws SQLException { + public void followThread(User user, MyThread thread) throws SQLException { PreparedStatement ps = this.con.prepareStatement("INSERT INTO follow VALUES(?, ?, ?)"); try { - ps.setString(1, ""+user.getId()); - ps.setString(2, ""+thread.getId()); - ps.setString(3, LocalDate.now().toString()); + ps.setInt(1, user.getId()); + System.out.println(user.getId()); + ps.setInt(2, thread.getId()); + System.out.println(thread.getId()); + ps.setString(3, "TO_DATE(" +LocalDate.now().toString() + ", 'YYYY/MM/DD')"); + ps.executeUpdate(); } catch (SQLException sqle) { sqle.getStackTrace();