Skip to content
Snippets Groups Projects
Commit 9730d62f authored by Charlie Darques's avatar Charlie Darques
Browse files

bug de followThread(), corrections en cours pour la création d'un thread

parent 0e67507e
No related branches found
No related tags found
No related merge requests found
...@@ -54,6 +54,22 @@ public class ThreadDAO { ...@@ -54,6 +54,22 @@ public class ThreadDAO {
return thread; 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){ public List<Message> getAllMessagesFromThread(int threadId){
ArrayList<Message> messages = new ArrayList<>(); ArrayList<Message> messages = new ArrayList<>();
try{ try{
...@@ -101,11 +117,13 @@ public class ThreadDAO { ...@@ -101,11 +117,13 @@ public class ThreadDAO {
// Créer un thread // Créer un thread
public void createThread(User user, String threadName) throws SQLException { public void createThread(User user, String threadName) throws SQLException {
PreparedStatement ps = this.con.prepareStatement("INSERT INTO thread (userID_thread, threadName) VALUES(?, ?)"); PreparedStatement ps = this.con.prepareStatement("INSERT INTO thread (userID_thread, threadName) VALUES(?, ?)");
UserDAO userDAO = new UserDAO();
try { try {
ps.setInt(1, +user.getId()); ps.setInt(1, user.getId());
ps.setString(2, threadName); ps.setString(2, threadName);
System.out.println(ps);
ps.executeUpdate(); ps.executeUpdate();
MyThread thread = getThreadByName(threadName);
userDAO.followThread(user, thread);
} }
catch (SQLException sqle) { catch (SQLException sqle) {
sqle.getStackTrace(); sqle.getStackTrace();
......
...@@ -19,11 +19,13 @@ public class UserDAO { ...@@ -19,11 +19,13 @@ public class UserDAO {
public void createUser(String username, String password){ public void createUser(String username, String password){
try{ try{
if (getUserByLogs(username, password) == null) {
PreparedStatement stmt = this.con.prepareStatement("INSERT INTO userAccount (username, password) VALUES (?,?)"); PreparedStatement stmt = this.con.prepareStatement("INSERT INTO userAccount (username, password) VALUES (?,?)");
stmt.setString(1, username); stmt.setString(1, username);
stmt.setString(2, password); stmt.setString(2, password);
stmt.executeUpdate(); stmt.executeUpdate();
}
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
...@@ -71,7 +73,7 @@ public class UserDAO { ...@@ -71,7 +73,7 @@ public class UserDAO {
public User getUserByLogs(String username, String password){ public User getUserByLogs(String username, String password){
User user = new User(0,null,null); User user = null;
try{ try{
PreparedStatement stmt = this.con.prepareStatement("SELECT * FROM userAccount WHERE username=? AND password=?"); PreparedStatement stmt = this.con.prepareStatement("SELECT * FROM userAccount WHERE username=? AND password=?");
...@@ -142,12 +144,15 @@ public class UserDAO { ...@@ -142,12 +144,15 @@ public class UserDAO {
} }
// S'abonner à un thread // 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(?, ?, ?)"); PreparedStatement ps = this.con.prepareStatement("INSERT INTO follow VALUES(?, ?, ?)");
try { try {
ps.setString(1, ""+user.getId()); ps.setInt(1, user.getId());
ps.setString(2, ""+thread.getId()); System.out.println(user.getId());
ps.setString(3, LocalDate.now().toString()); 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) { catch (SQLException sqle) {
sqle.getStackTrace(); sqle.getStackTrace();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment