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)
......@@ -70,7 +70,7 @@ public class Feed extends HttpServlet {
boolean msgIsLiked = false;
try {
msgIsLiked = messageDao.isMessageLikedByUser(message, user);
msgIsLiked = messageDao.isMessageLikedByUser(message.getMsgId(), user);
}
catch (SQLException sqle) {
sqle.getStackTrace();
......
......@@ -33,17 +33,15 @@ public class FollowThread extends HttpServlet {
}
boolean followed = false;
try {
if (threadsFollowed.contains(threadDao.getThreadById((Integer.parseInt(req.getParameter("threadid")))))) {
followed = true;
}
else {
followed = false;
}
if (threadsFollowed.contains(Integer.parseInt(req.getParameter("threadid")))) {
followed = true;
}
catch (SQLException sqle) {
sqle.getStackTrace();
else {
followed = false;
}
System.out.println(followed);
if (!followed) {
try {
......@@ -55,6 +53,7 @@ public class FollowThread extends HttpServlet {
}
}
else if (followed) {
System.out.println("déjà suivi");
try {
userDao.unfollowThread(user, threadDao.getThreadById((Integer.parseInt(req.getParameter("threadid")))));
res.sendRedirect("http://localhost:8080/s4a021-web-backend/Discover");
......
......@@ -23,19 +23,14 @@ public class LikeMessage extends HttpServlet {
ThreadDAO threadDao = new ThreadDAO();
MessageDAO messageDao = new MessageDAO();
Message msg = null;
try {
messageDao.getMessageByID(
Integer.parseInt(req.getParameter("messageid")));
}
catch (SQLException sqle) {
sqle.getStackTrace();
int messageID = 0;
if (req.getParameter("messageid") != null) {
messageID = Integer.parseInt(req.getParameter("messageid"));
}
boolean messageLiked = false;
try {
messageLiked = messageDao.isMessageLikedByUser(msg, user);
messageLiked = messageDao.isMessageLikedByUser(messageID, user);
}
catch (SQLException sqle) {
sqle.getStackTrace();
......@@ -43,7 +38,8 @@ public class LikeMessage extends HttpServlet {
if (!messageLiked) {
try {
userDao.reactToMsg(user, msg);
System.out.println("message liké " + messageLiked);
userDao.reactToMsg(user, messageID);
}
catch (SQLException sqle) {
sqle.getStackTrace();
......@@ -51,7 +47,8 @@ public class LikeMessage extends HttpServlet {
}
else {
try {
userDao.unreactToMsg(user, msg);
System.out.println("message liké" + messageLiked);
userDao.unreactToMsg(user, messageID);
}
catch (SQLException sqle) {
sqle.getStackTrace();
......@@ -63,5 +60,4 @@ public class LikeMessage extends HttpServlet {
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 {
PrintWriter out = res.getWriter();
UserDAO userdao = new UserDAO();
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());
if (user == null) System.out.println("user null");
......
......@@ -35,7 +35,7 @@ public class MessageDAO {
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("""
SELECT reaction FROM reactions
WHERE userID_reactions = ?
......@@ -43,7 +43,7 @@ public class MessageDAO {
""");
try {
ps.setInt(1, user.getId());
ps.setInt(2, message.getMsgId());
ps.setInt(2, messageID);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
if (rs.getBoolean("reaction")) {
......@@ -51,6 +51,38 @@ public class MessageDAO {
}
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) {
sqle.getStackTrace();
......
......@@ -192,6 +192,7 @@ public class UserDAO {
ps.setInt(2, thread.getId());
Date date = Date.valueOf(LocalDate.now());
ps.setDate(3, date);
ps.executeUpdate();
}
catch (SQLException sqle) {
......@@ -200,10 +201,13 @@ public class UserDAO {
}
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 {
ps.setInt(1, user.getId());
ps.setInt(2, thread.getId());
System.out.println(ps);
ps.executeUpdate();
}
catch (SQLException sqle) {
......@@ -230,12 +234,39 @@ public class UserDAO {
}
// Réagir à un message
public void reactToMsg(User user, Message msg) throws SQLException {
PreparedStatement ps = this.con.prepareStatement("INSERT INTO reactions VALUES(?, ?, ?)");
public void reactToMsg(User user, int msgID) throws SQLException {
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 {
ps.setInt(1, user.getId());
ps.setInt(2, msg.getMsgId());
ps.setString(3, "true");
ps.setInt(2, msgID);
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) {
sqle.getStackTrace();
......@@ -359,21 +390,4 @@ public class UserDAO {
}
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