Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • charlie.darques.etu/s4a021-web-backend
1 result
Show changes
Commits on Source (2)
......@@ -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>");
......
......@@ -16,6 +16,7 @@ import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@WebServlet("/Welcome")
public class Feed extends HttpServlet {
......@@ -54,15 +55,23 @@ public class Feed extends HttpServlet {
MyThread msgThread = new MyThread(0,null);
if (!messages.isEmpty()) {
try {
Message msg = messages.get(0);
msgThread = threadDao.getThreadById(msg.getThreadId());
} catch (Exception e) {
throw new RuntimeException(e);
}
out.println("<h2 class=\"feedMsgThread\"> Thread : <span style=\"color : grey\">" + msgThread.getThreadName().toUpperCase() + "</span></h2>");
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\">");
out.println("<h3 class=\"msgThread\">" + msgThread.getThreadName() + "</h3>");
out.println("<p class=\"msgContent\">" + message.getContent() + "</p>");
out.println("<p class=\"msgContent\"><strong style=\"color : grey\">" + userDao.getUserById(message.getSenderId()).getUserName() + "</strong> : " + message.getContent() + "</p>");
boolean msgIsLiked = false;
try {
......@@ -72,7 +81,7 @@ public class Feed extends HttpServlet {
sqle.getStackTrace();
}
out.println("<form class=\"feedForm\" action=\"http://localhost:8080/s4a021-web-backend/LikeMessage\" method=\"post\">");
out.println("<form class=\"feedForm\" action=\"http://localhost:8080/s4a021-web-backend/LikeMessage\" method=\"post\">");
out.println("<input name=\"messageid\" type=\"hidden\" value=\"" + message.getMsgId() +"\">");
if (msgIsLiked) {
......@@ -90,7 +99,7 @@ public class Feed extends HttpServlet {
out.println("<form class=\"feedForm\" action=\"http://localhost:8080/s4a021-web-backend/PostMessage\" method=\"post\">");
out.println("<textarea name=\"message\" rows=\"2\" cols=\"30\" placeholder=\"Post a message in this thread\"></textarea>");
out.println("<input name=\"threadid\" type=\"hidden\" value=\"" + msgThread.getId() + "\">");
out.println("<button type=\"submit\">Post</button>");
out.println("<button class=\"postButton\" type=\"submit\">Post</button>");
out.println("</form>");
out.println("<div class=\"discover\">");
......
......@@ -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;
......@@ -74,7 +74,7 @@ public class Following extends HttpServlet {
}
else {
out.println("<h3>You are not following any thread yet.</h3>");
out.println("<button><a href=\"\">Discover new threads</a></button>");
out.println("<button><a href=\"http://localhost:8080/s4a021-web-backend/Discover\">Discover new threads</a></button>");
}
out.println("</body>");
......
......@@ -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;
}
......
......@@ -7,6 +7,14 @@ body{
background-color: #232323;
}
toCenter{
text-align: center;
}
.feedMsgThread{
margin-top : 5%;
}
.menu{
display: flex;
align-items: center;
......@@ -31,7 +39,7 @@ ul{
padding : 0;
}
form:not(.followThread, .feedForm) {
form:not(.followThread, .feedForm, .feedFormu) {
text-align: center;
display: flex;
flex-direction: column;
......@@ -41,6 +49,16 @@ form:not(.followThread, .feedForm) {
margin-top: 5%;
}
textarea{
margin-top : 1%;
margin-right : auto;
margin-left : auto;
}
postButton{
}
.menu_option{
text-align : center;
list-style: none;
......@@ -73,6 +91,7 @@ form>.link {
}
.discover{
margin-top : 5%;
text-align : center;
}
......@@ -83,7 +102,7 @@ form>.link {
}
.information{
margin-top : 10%;
margin-bottom : 2%;
margin-top : 5%;
margin-bottom : 5%;
text-align : center;
}
\ No newline at end of file