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)
...@@ -70,7 +70,7 @@ public class Feed extends HttpServlet { ...@@ -70,7 +70,7 @@ public class Feed extends HttpServlet {
boolean msgIsLiked = false; boolean msgIsLiked = false;
try { try {
msgIsLiked = messageDao.isMessageLikedByUser(message, user); msgIsLiked = messageDao.isMessageLikedByUser(message.getMsgId(), user);
} }
catch (SQLException sqle) { catch (SQLException sqle) {
sqle.getStackTrace(); sqle.getStackTrace();
......
...@@ -33,17 +33,15 @@ public class FollowThread extends HttpServlet { ...@@ -33,17 +33,15 @@ public class FollowThread extends HttpServlet {
} }
boolean followed = false; boolean followed = false;
try {
if (threadsFollowed.contains(threadDao.getThreadById((Integer.parseInt(req.getParameter("threadid")))))) { if (threadsFollowed.contains(Integer.parseInt(req.getParameter("threadid")))) {
followed = true; followed = true;
}
else {
followed = false;
}
} }
catch (SQLException sqle) { else {
sqle.getStackTrace(); followed = false;
} }
System.out.println(followed);
if (!followed) { if (!followed) {
try { try {
...@@ -55,6 +53,7 @@ public class FollowThread extends HttpServlet { ...@@ -55,6 +53,7 @@ public class FollowThread extends HttpServlet {
} }
} }
else if (followed) { else if (followed) {
System.out.println("déjà suivi");
try { try {
userDao.unfollowThread(user, threadDao.getThreadById((Integer.parseInt(req.getParameter("threadid"))))); userDao.unfollowThread(user, threadDao.getThreadById((Integer.parseInt(req.getParameter("threadid")))));
res.sendRedirect("http://localhost:8080/s4a021-web-backend/Discover"); res.sendRedirect("http://localhost:8080/s4a021-web-backend/Discover");
......
...@@ -23,19 +23,14 @@ public class LikeMessage extends HttpServlet { ...@@ -23,19 +23,14 @@ public class LikeMessage extends HttpServlet {
ThreadDAO threadDao = new ThreadDAO(); ThreadDAO threadDao = new ThreadDAO();
MessageDAO messageDao = new MessageDAO(); MessageDAO messageDao = new MessageDAO();
Message msg = null; int messageID = 0;
if (req.getParameter("messageid") != null) {
try { messageID = Integer.parseInt(req.getParameter("messageid"));
messageDao.getMessageByID(
Integer.parseInt(req.getParameter("messageid")));
}
catch (SQLException sqle) {
sqle.getStackTrace();
} }
boolean messageLiked = false; boolean messageLiked = false;
try { try {
messageLiked = messageDao.isMessageLikedByUser(msg, user); messageLiked = messageDao.isMessageLikedByUser(messageID, user);
} }
catch (SQLException sqle) { catch (SQLException sqle) {
sqle.getStackTrace(); sqle.getStackTrace();
...@@ -43,7 +38,8 @@ public class LikeMessage extends HttpServlet { ...@@ -43,7 +38,8 @@ public class LikeMessage extends HttpServlet {
if (!messageLiked) { if (!messageLiked) {
try { try {
userDao.reactToMsg(user, msg); System.out.println("message liké " + messageLiked);
userDao.reactToMsg(user, messageID);
} }
catch (SQLException sqle) { catch (SQLException sqle) {
sqle.getStackTrace(); sqle.getStackTrace();
...@@ -51,7 +47,8 @@ public class LikeMessage extends HttpServlet { ...@@ -51,7 +47,8 @@ public class LikeMessage extends HttpServlet {
} }
else { else {
try { try {
userDao.unreactToMsg(user, msg); System.out.println("message liké" + messageLiked);
userDao.unreactToMsg(user, messageID);
} }
catch (SQLException sqle) { catch (SQLException sqle) {
sqle.getStackTrace(); sqle.getStackTrace();
...@@ -63,5 +60,4 @@ public class LikeMessage extends HttpServlet { ...@@ -63,5 +60,4 @@ public class LikeMessage extends HttpServlet {
res.sendRedirect("http://localhost:8080/s4a021-web-backend/index.html"); res.sendRedirect("http://localhost:8080/s4a021-web-backend/index.html");
} }
} }
} }
// A TESTER \ No newline at end of file
\ No newline at end of file
...@@ -18,6 +18,9 @@ public class LogIn extends HttpServlet { ...@@ -18,6 +18,9 @@ public class LogIn extends HttpServlet {
PrintWriter out = res.getWriter(); PrintWriter out = res.getWriter();
UserDAO userdao = new UserDAO(); UserDAO userdao = new UserDAO();
User user = userdao.getUserByLogs(req.getParameter("login"), req.getParameter("pwd")); User user = userdao.getUserByLogs(req.getParameter("login"), req.getParameter("pwd"));
if (user.getUserName() == null) {
res.sendRedirect("http://localhost:8080/s4a021-web-backend/index.html");
}
req.getSession().setAttribute("login", user.getUserName()); req.getSession().setAttribute("login", user.getUserName());
if (user == null) System.out.println("user null"); if (user == null) System.out.println("user null");
......
...@@ -35,7 +35,7 @@ public class MessageDAO { ...@@ -35,7 +35,7 @@ public class MessageDAO {
return message; return message;
} }
public boolean isMessageLikedByUser(Message message, User user) throws SQLException { public boolean isMessageLikedByUser(int messageID, User user) throws SQLException {
PreparedStatement ps = this.con.prepareStatement(""" PreparedStatement ps = this.con.prepareStatement("""
SELECT reaction FROM reactions SELECT reaction FROM reactions
WHERE userID_reactions = ? WHERE userID_reactions = ?
...@@ -43,7 +43,7 @@ public class MessageDAO { ...@@ -43,7 +43,7 @@ public class MessageDAO {
"""); """);
try { try {
ps.setInt(1, user.getId()); ps.setInt(1, user.getId());
ps.setInt(2, message.getMsgId()); ps.setInt(2, messageID);
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
while(rs.next()) { while(rs.next()) {
if (rs.getBoolean("reaction")) { if (rs.getBoolean("reaction")) {
...@@ -51,6 +51,38 @@ public class MessageDAO { ...@@ -51,6 +51,38 @@ public class MessageDAO {
} }
else return false; else return false;
} }
if (!rs.next()) {
return false;
}
}
catch (SQLException sqle) {
sqle.getStackTrace();
}
return false;
}
public boolean userReactedToMessage(User user, int msgID) throws SQLException {
PreparedStatement ps = this.con.prepareStatement("SELECT * FROM reactions WHERE userid_reactions=? AND msgID_reactions=?");
try {
ps.setInt(1, user.getId());
ps.setInt(2, msgID);
ResultSet rs = ps.executeQuery();
if (rs.next()) return true;
else return false;
}
catch (SQLException sqle) {
sqle.getStackTrace();
}
return false;
}
public boolean isInReactionTable(int msgID) throws SQLException {
PreparedStatement ps = this.con.prepareStatement("SELECT * FROM msg WHERE msgID=?");
try {
ps.setInt(1, msgID);
ResultSet rs = ps.executeQuery();
if (rs.next()) return true;
else return false;
} }
catch (SQLException sqle) { catch (SQLException sqle) {
sqle.getStackTrace(); sqle.getStackTrace();
......
...@@ -192,6 +192,7 @@ public class UserDAO { ...@@ -192,6 +192,7 @@ public class UserDAO {
ps.setInt(2, thread.getId()); ps.setInt(2, thread.getId());
Date date = Date.valueOf(LocalDate.now()); Date date = Date.valueOf(LocalDate.now());
ps.setDate(3, date); ps.setDate(3, date);
ps.executeUpdate(); ps.executeUpdate();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
...@@ -200,10 +201,13 @@ public class UserDAO { ...@@ -200,10 +201,13 @@ public class UserDAO {
} }
public void unfollowThread(User user, MyThread thread) throws SQLException { public void unfollowThread(User user, MyThread thread) throws SQLException {
PreparedStatement ps = this.con.prepareStatement("DELETE FROM follow WHERE userID_follow = ? AND threadID_follow = ?)"); System.out.println("dans méthode");
PreparedStatement ps = this.con.prepareStatement("DELETE FROM follow WHERE userID_follow=? AND threadID_follow=?");
try { try {
ps.setInt(1, user.getId()); ps.setInt(1, user.getId());
ps.setInt(2, thread.getId()); ps.setInt(2, thread.getId());
System.out.println(ps);
ps.executeUpdate(); ps.executeUpdate();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
...@@ -230,12 +234,39 @@ public class UserDAO { ...@@ -230,12 +234,39 @@ public class UserDAO {
} }
// Réagir à un message // Réagir à un message
public void reactToMsg(User user, Message msg) throws SQLException { public void reactToMsg(User user, int msgID) throws SQLException {
PreparedStatement ps = this.con.prepareStatement("INSERT INTO reactions VALUES(?, ?, ?)"); PreparedStatement ps = null;
MessageDAO messageDAO = new MessageDAO();
if (messageDAO.isInReactionTable(msgID) && messageDAO.userReactedToMessage(user, msgID)) {
ps = this.con.prepareStatement("UPDATE reactions SET reaction=true WHERE userid_reactions=? AND msgid_reactions=?");
}
else {
ps = this.con.prepareStatement("INSERT INTO reactions VALUES(?, ?, true)");
}
try { try {
ps.setInt(1, user.getId()); ps.setInt(1, user.getId());
ps.setInt(2, msg.getMsgId()); ps.setInt(2, msgID);
ps.setString(3, "true"); System.out.println(ps);
ps.executeUpdate();
}
catch (SQLException sqle) {
sqle.getStackTrace();
}
}
public void unreactToMsg(User user, int msgID) throws SQLException {
PreparedStatement ps = this.con.prepareStatement("""
UPDATE reactions
SET reaction = false
WHERE userID_reactions = ?
AND msgID_reactions = ?
""");
try {
ps.setInt(1, user.getId());
ps.setInt(2, msgID);
System.out.println("requete unreact");
ps.executeUpdate();
} }
catch (SQLException sqle) { catch (SQLException sqle) {
sqle.getStackTrace(); sqle.getStackTrace();
...@@ -359,21 +390,4 @@ public class UserDAO { ...@@ -359,21 +390,4 @@ public class UserDAO {
} }
return messages; return messages;
} }
public void unreactToMsg(User user, Message msg) throws SQLException {
PreparedStatement ps = this.con.prepareStatement("""
UPDATE reactions
SET reaction = false
WHERE userID_reactions = ?
AND msgID_reactions = ?
""");
try {
ps.setInt(1, user.getId());
ps.setInt(2, msg.getMsgId());
ps.executeUpdate();
}
catch (SQLException sqle) {
sqle.getStackTrace();
}
}
} }
\ No newline at end of file