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

début fonctionnalité de création de thread, problème de méthode pour poster un message

parent a25e6725
Branches
No related tags found
No related merge requests found
...@@ -39,15 +39,16 @@ public class Feed extends HttpServlet { ...@@ -39,15 +39,16 @@ public class Feed extends HttpServlet {
out.println("<html><body><meta charset=\"utf-8\">"); out.println("<html><body><meta charset=\"utf-8\">");
out.println("<title>Welcome</title>"); out.println("<title>Welcome</title>");
out.println("<h1>Welcome " + login + "</h1>");
out.println("<nav class=\"menu\">"); out.println("<nav>");
out.println("<ul class=\"options_list\">"); out.println("<ul class=\"menu\">");
out.println("<li class=\"option\"><a href=\"#\">\uD83C\uDFE0 Home</a></li>"); out.println("<li class=\"menu_option\"><a href=\"#\">\uD83C\uDFE0 Home</a></li>");
out.println("<li class=\"option\"><a href=\"#\">\uD83E\uDDF5 Followed</a></li>"); out.println("<li class=\"menu_option\"><a href=\"#\">\uD83E\uDDF5 Followed</a></li>");
out.println("<li class=\"option\"><a href=\"#\">\uD83D\uDC64 Account</a></li>"); out.println("<li class=\"menu_option\"><a href=\"#\">\uD83D\uDC64 Account</a></li>");
out.println("</ul></nav>"); out.println("</ul></nav>");
out.println("<h1>Welcome " + login + "</h1>");
if (!messages.isEmpty()) { if (!messages.isEmpty()) {
for (Message message : messages) { for (Message message : messages) {
MyThread msgThread = null; MyThread msgThread = null;
...@@ -59,7 +60,7 @@ public class Feed extends HttpServlet { ...@@ -59,7 +60,7 @@ public class Feed extends HttpServlet {
out.println("<div class=\"message\">"); out.println("<div class=\"message\">");
out.println("<h3 class=\"msgThread\">" + msgThread.getThreadName() + "</h3>"); out.println("<h3 class=\"msgThread\">" + msgThread.getThreadName() + "</h3>");
out.println("<p class=\"msgContent\">" + message.getContent() + "</p>"); out.println("<p class=\"msgContent\">" + message.getContent() + "</p>");
out.println("<form><button class=\"react\" type=\"submit\" value=\"Like\">"); out.println("<form><button class=\"react\" type=\"submit\" value=\"Like\">Like</button>");
out.println("</div>"); out.println("</div>");
} }
} }
...@@ -68,6 +69,8 @@ public class Feed extends HttpServlet { ...@@ -68,6 +69,8 @@ public class Feed extends HttpServlet {
out.println("<button><a href=\"\">Discover new threads</a></button>"); out.println("<button><a href=\"\">Discover new threads</a></button>");
} }
out.println("<a href=\"http://localhost:8080/s4a021-web-backend/NewThread\">Create a new thread</a>");
out.println("</body>"); out.println("</body>");
out.println("<footer>"); out.println("<footer>");
out.println("Connected as " + login); out.println("Connected as " + login);
......
package controleurs;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import dao.ThreadDAO;
import dao.UserDAO;
import dto.MyThread;
import dto.User;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/NewThread")
public class NewThread extends HttpServlet {
public void service (HttpServletRequest req, HttpServletResponse res) throws IOException {
if (req.getSession().getAttribute("user") != null) {
PrintWriter out = res.getWriter();
ThreadDAO threadDAO = new ThreadDAO();
UserDAO userDAO = new UserDAO();
String login = (String) req.getSession().getAttribute("login");
User user = (User) req.getSession().getAttribute("user");
String threadname = null;
String message = null;
boolean newThreadWasCreated = false;
if (req.getParameter("threadname") != null) {
threadname = req.getParameter("threadname");
}
if (req.getParameter("first_message") != null) {
message = req.getParameter("first_message");
}
if (threadname != null && message != null) {
try {
threadDAO.createThread(user, threadname);
List<MyThread> threadsCreated = userDAO.getThreadsCreatedByUser(user);
MyThread newThread = null;
for (MyThread t : threadsCreated) {
if (t.getThreadName().equals(threadname)) {
newThread = t;
newThreadWasCreated = true;
}
}
// PROBLEME ICI : Cannot invoke "dto.MyThread.getId()" because "<parameter2>" is null
userDAO.postMessage(user, newThread, message);
}
catch (SQLException sqle) {
sqle.getStackTrace();
}
}
out.println("<html><body><meta charset=\"utf-8\">");
out.println("<title>New thread</title>");
out.println("<div class=\"write\">");
out.println("<h3>Create a new thread</h3>");
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=\"first_message\" type=\"text\" placeholder=\"Post the first message of your new thread!\">");
out.println("<button type=\"submit\">Create</button>");
out.println("</form>");
if (newThreadWasCreated) {
out.println("<p class=\"information\">The thread " + threadname + " was successfully created</p>");
}
out.println("<button><a href=\"http://localhost:8080/s4a021-web-backend/Welcome\">Back to feed</a></button>");
out.println("</body>");
out.println("<footer>");
out.println("Connected as " + login);
out.println("</footer>");
out.println("</html>");
}
else {
res.sendRedirect("http://localhost:8080/s4a021-web-backend/index.html");
}
}
}
...@@ -155,10 +155,11 @@ public class UserDAO { ...@@ -155,10 +155,11 @@ public class UserDAO {
} }
// Poster un message dans un thread // Poster un message dans un thread
public void postMessage(User user, Thread thread, String message) throws SQLException { public void postMessage(User user, MyThread thread, String message) throws SQLException {
PreparedStatement ps = this.con.prepareStatement("INSERT INTO msg (userID_msg, threadID, msg) VALUES(?, ?, ?)"); PreparedStatement ps = this.con.prepareStatement("INSERT INTO msg (userID_msg, threadID, msg) VALUES(?, ?, ?)");
try { try {
ps.setInt(1, user.getId()); ps.setInt(1, user.getId());
// PROBLEME ICI
ps.setInt(2, (int) thread.getId()); ps.setInt(2, (int) thread.getId());
ps.setString(3, message); ps.setString(3, message);
} }
...@@ -181,7 +182,7 @@ public class UserDAO { ...@@ -181,7 +182,7 @@ public class UserDAO {
} }
// Récupérer les thread auxquels un utilisateur est abonné // Récupérer les thread auxquels un utilisateur est abonné
public List<MyThread> getThreadsByUser(User user) throws SQLException { public List<MyThread> getThreadsFollowedByUser(User user) throws SQLException {
List<MyThread> threads = new ArrayList<>(); List<MyThread> threads = new ArrayList<>();
PreparedStatement ps = this.con.prepareStatement("SELECT threadID_follow FROM follow WHERE userID_follow = ?"); PreparedStatement ps = this.con.prepareStatement("SELECT threadID_follow FROM follow WHERE userID_follow = ?");
try { try {
...@@ -199,6 +200,29 @@ public class UserDAO { ...@@ -199,6 +200,29 @@ public class UserDAO {
return threads; return threads;
} }
public List<MyThread> getThreadsCreatedByUser(User user) throws SQLException {
List<MyThread> threads = new ArrayList<>();
PreparedStatement ps = this.con.prepareStatement("""
SELECT threadID, threadname FROM thread AS t
INNER JOIN userAccount AS u
ON t.userid_thread = u.userid
WHERE u.userid = ?
""");
try {
ps.setInt(1, user.getId());
ResultSet rs = ps.executeQuery();
while(rs.next()){
int threadId = rs.getInt(1);
String threadname = rs.getString(2);
threads.add(new MyThread(threadId, threadname));
}
}
catch (SQLException sqle) {
sqle.getStackTrace();
}
return threads;
}
// Récupérer les messages de chaque thread à partir du plus récent // Récupérer les messages de chaque thread à partir du plus récent
public List<Message> getMostRecentMessages(User user) throws SQLException { public List<Message> getMostRecentMessages(User user) throws SQLException {
List<Message> messages = new ArrayList<>(); List<Message> messages = new ArrayList<>();
...@@ -217,10 +241,8 @@ public class UserDAO { ...@@ -217,10 +241,8 @@ public class UserDAO {
try { try {
System.out.println("user id : " + user.getId()); System.out.println("user id : " + user.getId());
ps.setInt(1, user.getId()); ps.setInt(1, user.getId());
System.out.println("dans le try");
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
while(rs.next()){ while(rs.next()){
System.out.println("dans le while");
int msgID = rs.getInt(1); int msgID = rs.getInt(1);
int sender = rs.getInt(2); int sender = rs.getInt(2);
int threadID = rs.getInt(3); int threadID = rs.getInt(3);
...@@ -230,10 +252,8 @@ public class UserDAO { ...@@ -230,10 +252,8 @@ public class UserDAO {
} }
} }
catch (SQLException sqle) { catch (SQLException sqle) {
System.out.println("erreur");
sqle.getStackTrace(); sqle.getStackTrace();
} }
System.out.println("test");
return messages; return messages;
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment