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

correction de base de données + permet de créer un thread

parent a9c97b44
No related branches found
No related tags found
No related merge requests found
...@@ -25,33 +25,37 @@ public class NewThread extends HttpServlet { ...@@ -25,33 +25,37 @@ public class NewThread extends HttpServlet {
String login = (String) req.getSession().getAttribute("login"); String login = (String) req.getSession().getAttribute("login");
User user = (User) req.getSession().getAttribute("user"); User user = (User) req.getSession().getAttribute("user");
String threadname = null; String threadname = null;
String message = null; //String message = null;
boolean newThreadWasCreated = false; boolean newThreadWasCreated = false;
if (req.getParameter("threadname") != null) { if (req.getParameter("threadname") != null) {
threadname = req.getParameter("threadname"); threadname = req.getParameter("threadname");
} }
if (req.getParameter("first_message") != null) { // if (req.getParameter("first_message") != null) {
message = req.getParameter("first_message"); // message = req.getParameter("first_message");
} // }
if (threadname != null && message != null) { if (threadname != null
//&& message != null
) {
try { try {
threadDAO.createThread(user, threadname); threadDAO.createThread(user, threadname);
List<MyThread> threadsCreated = userDAO.getThreadsCreatedByUser(user); List<MyThread> threadsCreated = userDAO.getThreadsCreatedByUser(user);
MyThread newThread = null; MyThread newThread = null;
for (MyThread t : threadsCreated) { for (MyThread t : threadsCreated) {
System.out.println("liste threads : " + t.getThreadName());
if (t.getThreadName().equals(threadname)) { if (t.getThreadName().equals(threadname)) {
newThread = t; newThread = t;
newThreadWasCreated = true; newThreadWasCreated = true;
} }
} }
// PROBLEME ICI : Cannot invoke "dto.MyThread.getId()" because "<parameter2>" is null // PROBLEME ICI : Cannot invoke "dto.MyThread.getId()" because "<parameter2>" is null
userDAO.postMessage(user, newThread, message); //userDAO.postMessage(user, newThread, message);
} }
catch (SQLException sqle) { catch (SQLException sqle) {
sqle.getStackTrace(); sqle.getStackTrace();
} }
System.out.println(newThreadWasCreated);
} }
...@@ -62,7 +66,7 @@ public class NewThread extends HttpServlet { ...@@ -62,7 +66,7 @@ public class NewThread extends HttpServlet {
out.println("<h3>Create a new thread</h3>"); out.println("<h3>Create a new thread</h3>");
out.println("<form action=\"http://localhost:8080/s4a021-web-backend/NewThread\" method=\"post\">"); out.println("<form action=\"http://localhost:8080/s4a021-web-backend/NewThread\" method=\"post\">");
out.println("<input name=\"threadname\" type=\"text\" placeholder=\"Name of new thread\">"); out.println("<input name=\"threadname\" type=\"text\" placeholder=\"Name of new thread\">");
out.println("<input name=\"first_message\" type=\"text\" placeholder=\"Post the first message of your new thread!\">"); // out.println("<input name=\"first_message\" type=\"text\" placeholder=\"Post the first message of your new thread!\">");
out.println("<button type=\"submit\">Create</button>"); out.println("<button type=\"submit\">Create</button>");
out.println("</form>"); out.println("</form>");
......
...@@ -101,8 +101,10 @@ public class ThreadDAO { ...@@ -101,8 +101,10 @@ public class ThreadDAO {
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(?, ?)");
try { try {
ps.setString(1, ""+user.getId()); ps.setInt(1, +user.getId());
ps.setString(2, threadName); ps.setString(2, threadName);
System.out.println(ps);
ps.executeUpdate();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
sqle.getStackTrace(); sqle.getStackTrace();
......
...@@ -212,6 +212,7 @@ public class UserDAO { ...@@ -212,6 +212,7 @@ public class UserDAO {
ps.setInt(1, user.getId()); ps.setInt(1, user.getId());
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
while(rs.next()){ while(rs.next()){
System.out.println("thread créé : " + rs.getString(2));
int threadId = rs.getInt(1); int threadId = rs.getInt(1);
String threadname = rs.getString(2); String threadname = rs.getString(2);
threads.add(new MyThread(threadId, threadname)); threads.add(new MyThread(threadId, threadname));
......
...@@ -5,10 +5,10 @@ INSERT INTO userAccount (username, password) VALUES ('toto', 'totopwd123'), ...@@ -5,10 +5,10 @@ INSERT INTO userAccount (username, password) VALUES ('toto', 'totopwd123'),
('test', 'test'); ('test', 'test');
INSERT INTO thread VALUES (1, 2, 'Gaming'), INSERT INTO thread (userID_thread, threadName) VALUES (2, 'Gaming'),
(2, 3, 'Animaux'), (3, 'Animaux'),
(3, 3, 'Objet perdu'), (3, 'Objet perdu'),
(4, 1, 'Discussion'); (1, 'Discussion');
INSERT INTO follow VALUES (2,1, TO_DATE('11/02/2025', 'DD/MM/YYYY')), INSERT INTO follow VALUES (2,1, TO_DATE('11/02/2025', 'DD/MM/YYYY')),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment