Skip to content
Snippets Groups Projects
Commit 9d9f1655 authored by Yannis Devos's avatar Yannis Devos
Browse files

Correction discover page

parent b8714f2a
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,7 @@ import jakarta.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/Discover")
......@@ -28,8 +29,7 @@ public class Discover extends HttpServlet {
User user = (User) req.getSession().getAttribute("user");
if (user != null) {
List<Message> messages = null;
messages = threadDao.getAllMessages();
List<MyThread> threads = threadDao.getAllThreads();
out.println("<html><body><meta charset=\"utf-8\">");
out.println("<link rel=\"stylesheet\" href=\"css/feed.css\">");
......@@ -50,25 +50,18 @@ public class Discover extends HttpServlet {
sqle.getStackTrace();
}
if (!messages.isEmpty()) {
for (Message message : messages) {
MyThread msgThread = null;
try {
msgThread = threadDao.getThreadById(message.getThreadId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
User sender = userDao.getUserById(message.getSenderId());
if (!threads.isEmpty()) {
for (MyThread th : threads) {
User sender = userDao.getUserById(th.getUserId());
String senderName = sender.getUserName();
boolean followed = followedThreads.contains(msgThread.getId());
boolean followed = followedThreads.contains(th.getId());
out.println("<div class=\"message\">");
out.println("<h3 class=\"msgThread\">" + msgThread.getThreadName() + "</h3>");
out.println("<p class=\"msgContent\">" + message.getContent() + "</p>");
out.println("<h3 class=\"msgThread\">" + th.getThreadName() + "</h3>");
out.println("<p class=\"sender\">by " + senderName + "</p>");
out.println("<form class=\"followThread\" action=\"http://localhost:8080/s4a021-web-backend/FollowThread\" method=\"post\">");
out.println("<input name=\"threadid\" type=\"hidden\" value=\"" + msgThread.getId() +"\">");
out.println("<input name=\"threadid\" type=\"hidden\" value=\"" + th.getId() +"\">");
if (followed) {
out.println("<button class=\"followed\" type=\"submit\">Followed</button>");
......
......@@ -57,7 +57,7 @@ public class Feed extends HttpServlet {
for (Message message : messages) {
try {
msgThread = threadDao.getThreadById(message.getThreadId());
} catch (SQLException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
out.println("<div class=\"message\">");
......
......@@ -55,7 +55,7 @@ public class Following extends HttpServlet {
try {
thread = threadDao.getThreadById(t);
}
catch (SQLException sqle) {
catch (Exception sqle) {
sqle.getStackTrace();
}
LocalDate followDate = null;
......
......@@ -37,10 +37,25 @@ public class ThreadDAO {
return messages;
}
public MyThread getThreadById(int threadID) throws SQLException {
public List<MyThread> getAllThreads(){
ArrayList<MyThread> threads = new ArrayList<>();
try {
PreparedStatement ps = this.con.prepareStatement("SELECT * FROM thread;");
ResultSet rs = ps.executeQuery();
while(rs.next()){
threads.add(new MyThread(rs.getInt(1), rs.getInt(2), rs.getString(3)));
}
}
catch (SQLException sqle) {
sqle.getStackTrace();
}
return threads;
}
public MyThread getThreadById(int threadID) {
MyThread thread = new MyThread(0, null);
PreparedStatement ps = this.con.prepareStatement("SELECT threadID, threadName FROM thread WHERE threadID = ?");
try {
PreparedStatement ps = this.con.prepareStatement("SELECT threadID, threadName FROM thread WHERE threadID = ?");
ps.setInt(1, threadID);
ResultSet rs = ps.executeQuery();
if(rs.next()){
......
......@@ -2,6 +2,7 @@ package dto;
public class MyThread {
private int threadID;
private int userID;
private String threadName;
......@@ -12,6 +13,12 @@ public class MyThread {
this.threadName = name;
}
public MyThread(int id, int userId, String name){
this.threadID = id;
this.userID = userId;
this.threadName = name;
}
// Getter //
......@@ -19,6 +26,10 @@ public class MyThread {
return threadID;
}
public int getUserId() {
return userID;
}
public String getThreadName() {
return threadName;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment