Skip to content
Snippets Groups Projects
Select Git revision
  • d37d608fcffc8c5dbecece9ccbc6191da7ec40d0
  • main default protected
2 results

NewThread.java

Blame
  • NewThread.java 3.40 KiB
    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");
            }
        }
        
    }