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
Select Git revision

Target

Select target project
  • charlie.darques.etu/s4a021-web-backend
1 result
Select Git revision
Show changes
Commits on Source (2)
...@@ -13,6 +13,7 @@ import jakarta.servlet.http.HttpServletResponse; ...@@ -13,6 +13,7 @@ import jakarta.servlet.http.HttpServletResponse;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@WebServlet("/Discover") @WebServlet("/Discover")
...@@ -28,8 +29,7 @@ public class Discover extends HttpServlet { ...@@ -28,8 +29,7 @@ public class Discover extends HttpServlet {
User user = (User) req.getSession().getAttribute("user"); User user = (User) req.getSession().getAttribute("user");
if (user != null) { if (user != null) {
List<Message> messages = null; List<MyThread> threads = threadDao.getAllThreads();
messages = threadDao.getAllMessages();
out.println("<html><body><meta charset=\"utf-8\">"); out.println("<html><body><meta charset=\"utf-8\">");
out.println("<link rel=\"stylesheet\" href=\"css/feed.css\">"); out.println("<link rel=\"stylesheet\" href=\"css/feed.css\">");
...@@ -50,25 +50,18 @@ public class Discover extends HttpServlet { ...@@ -50,25 +50,18 @@ public class Discover extends HttpServlet {
sqle.getStackTrace(); sqle.getStackTrace();
} }
if (!messages.isEmpty()) { if (!threads.isEmpty()) {
for (Message message : messages) { for (MyThread th : threads) {
MyThread msgThread = null; User sender = userDao.getUserById(th.getUserId());
try {
msgThread = threadDao.getThreadById(message.getThreadId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
User sender = userDao.getUserById(message.getSenderId());
String senderName = sender.getUserName(); String senderName = sender.getUserName();
boolean followed = followedThreads.contains(msgThread.getId()); boolean followed = followedThreads.contains(th.getId());
out.println("<div class=\"message\">"); out.println("<div class=\"message\">");
out.println("<h3 class=\"msgThread\">" + msgThread.getThreadName() + "</h3>"); out.println("<h3 class=\"msgThread\">" + th.getThreadName() + "</h3>");
out.println("<p class=\"msgContent\">" + message.getContent() + "</p>");
out.println("<p class=\"sender\">by " + senderName + "</p>"); 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("<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) { if (followed) {
out.println("<button class=\"followed\" type=\"submit\">Followed</button>"); out.println("<button class=\"followed\" type=\"submit\">Followed</button>");
......
...@@ -16,6 +16,7 @@ import java.io.IOException; ...@@ -16,6 +16,7 @@ import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
@WebServlet("/Welcome") @WebServlet("/Welcome")
public class Feed extends HttpServlet { public class Feed extends HttpServlet {
...@@ -54,15 +55,23 @@ public class Feed extends HttpServlet { ...@@ -54,15 +55,23 @@ public class Feed extends HttpServlet {
MyThread msgThread = new MyThread(0,null); MyThread msgThread = new MyThread(0,null);
if (!messages.isEmpty()) { 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) { for (Message message : messages) {
try { try {
msgThread = threadDao.getThreadById(message.getThreadId()); msgThread = threadDao.getThreadById(message.getThreadId());
} catch (SQLException e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
out.println("<div class=\"message\">"); out.println("<div class=\"message\">");
out.println("<h3 class=\"msgThread\">" + msgThread.getThreadName() + "</h3>"); out.println("<p class=\"msgContent\"><strong style=\"color : grey\">" + userDao.getUserById(message.getSenderId()).getUserName() + "</strong> : " + message.getContent() + "</p>");
out.println("<p class=\"msgContent\">" + message.getContent() + "</p>");
boolean msgIsLiked = false; boolean msgIsLiked = false;
try { try {
...@@ -72,7 +81,7 @@ public class Feed extends HttpServlet { ...@@ -72,7 +81,7 @@ public class Feed extends HttpServlet {
sqle.getStackTrace(); 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() +"\">"); out.println("<input name=\"messageid\" type=\"hidden\" value=\"" + message.getMsgId() +"\">");
if (msgIsLiked) { if (msgIsLiked) {
...@@ -90,7 +99,7 @@ public class Feed extends HttpServlet { ...@@ -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("<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("<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("<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("</form>");
out.println("<div class=\"discover\">"); out.println("<div class=\"discover\">");
......
...@@ -55,7 +55,7 @@ public class Following extends HttpServlet { ...@@ -55,7 +55,7 @@ public class Following extends HttpServlet {
try { try {
thread = threadDao.getThreadById(t); thread = threadDao.getThreadById(t);
} }
catch (SQLException sqle) { catch (Exception sqle) {
sqle.getStackTrace(); sqle.getStackTrace();
} }
LocalDate followDate = null; LocalDate followDate = null;
...@@ -74,7 +74,7 @@ public class Following extends HttpServlet { ...@@ -74,7 +74,7 @@ public class Following extends HttpServlet {
} }
else { else {
out.println("<h3>You are not following any thread yet.</h3>"); 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>"); out.println("</body>");
......
...@@ -37,10 +37,25 @@ public class ThreadDAO { ...@@ -37,10 +37,25 @@ public class ThreadDAO {
return messages; 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); MyThread thread = new MyThread(0, null);
PreparedStatement ps = this.con.prepareStatement("SELECT threadID, threadName FROM thread WHERE threadID = ?");
try { try {
PreparedStatement ps = this.con.prepareStatement("SELECT threadID, threadName FROM thread WHERE threadID = ?");
ps.setInt(1, threadID); ps.setInt(1, threadID);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
if(rs.next()){ if(rs.next()){
......
...@@ -2,6 +2,7 @@ package dto; ...@@ -2,6 +2,7 @@ package dto;
public class MyThread { public class MyThread {
private int threadID; private int threadID;
private int userID;
private String threadName; private String threadName;
...@@ -12,6 +13,12 @@ public class MyThread { ...@@ -12,6 +13,12 @@ public class MyThread {
this.threadName = name; this.threadName = name;
} }
public MyThread(int id, int userId, String name){
this.threadID = id;
this.userID = userId;
this.threadName = name;
}
// Getter // // Getter //
...@@ -19,6 +26,10 @@ public class MyThread { ...@@ -19,6 +26,10 @@ public class MyThread {
return threadID; return threadID;
} }
public int getUserId() {
return userID;
}
public String getThreadName() { public String getThreadName() {
return threadName; return threadName;
} }
......
...@@ -7,6 +7,14 @@ body{ ...@@ -7,6 +7,14 @@ body{
background-color: #232323; background-color: #232323;
} }
toCenter{
text-align: center;
}
.feedMsgThread{
margin-top : 5%;
}
.menu{ .menu{
display: flex; display: flex;
align-items: center; align-items: center;
...@@ -31,7 +39,7 @@ ul{ ...@@ -31,7 +39,7 @@ ul{
padding : 0; padding : 0;
} }
form:not(.followThread, .feedForm) { form:not(.followThread, .feedForm, .feedFormu) {
text-align: center; text-align: center;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
...@@ -41,6 +49,16 @@ form:not(.followThread, .feedForm) { ...@@ -41,6 +49,16 @@ form:not(.followThread, .feedForm) {
margin-top: 5%; margin-top: 5%;
} }
textarea{
margin-top : 1%;
margin-right : auto;
margin-left : auto;
}
postButton{
}
.menu_option{ .menu_option{
text-align : center; text-align : center;
list-style: none; list-style: none;
...@@ -73,6 +91,7 @@ form>.link { ...@@ -73,6 +91,7 @@ form>.link {
} }
.discover{ .discover{
margin-top : 5%;
text-align : center; text-align : center;
} }
...@@ -83,7 +102,7 @@ form>.link { ...@@ -83,7 +102,7 @@ form>.link {
} }
.information{ .information{
margin-top : 10%; margin-top : 5%;
margin-bottom : 2%; margin-bottom : 5%;
text-align : center; text-align : center;
} }
\ No newline at end of file