Skip to content
Snippets Groups Projects
Commit 4020d966 authored by Charlie Darques's avatar Charlie Darques
Browse files

IMPORTANT ajout d'une colonne dans la table msg et changement des données...

IMPORTANT ajout d'une colonne dans la table msg et changement des données représentatives + ajout méthodes de récupération des messages des threads suivis à partir du plus récent
parent 6d1d3f60
Branches
No related tags found
No related merge requests found
File deleted
File deleted
...@@ -6,6 +6,7 @@ import java.util.ArrayList; ...@@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import dto.Message; import dto.Message;
import dto.MyThread;
import dto.User; import dto.User;
import jakarta.servlet.http.*; import jakarta.servlet.http.*;
...@@ -182,15 +183,50 @@ public class UserDAO extends HttpServlet{ ...@@ -182,15 +183,50 @@ public class UserDAO extends HttpServlet{
} }
// Récupérer les thread auxquels un utilisateur est abonné // Récupérer les thread auxquels un utilisateur est abonné
public List<Thread> getThreadsByUser(User user) throws SQLException { public List<MyThread> getThreadsByUser(User user) throws SQLException {
List<Thread> threads = new ArrayList<>(); List<MyThread> threads = new ArrayList<>();
PreparedStatement ps = this.con.prepareStatement("SELECT threadID_follow FROM follow WHERE userID_follow = ?"); PreparedStatement ps = this.con.prepareStatement("SELECT threadID_follow FROM follow WHERE userID_follow = ?");
try { try {
ps.setInt(1, user.getId()); ps.setInt(1, user.getId());
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
while(rs.next()){ while(rs.next()){
int threadId = rs.getInt(1);
String threadname = rs.getString(2);
threads.add(new MyThread(threadId, threadname));
}
}
catch (SQLException sqle) {
sqle.getStackTrace();
}
return threads;
}
// Récupérer les messages de chaque thread à partir du plus récent
public List<Message> getMostRecentMessages(User user) throws SQLException {
List<Message> messages = new ArrayList<>();
PreparedStatement ps = this.con.prepareStatement("""
SELECT m.msgID, m.userID_msg, m.threadID, m.msg, m.posted_at FROM msg AS m
INNER JOIN follow AS f
ON m.userID_msg = f.userID_follow
WHERE f.threadID_follow = m.threadID
AND m.userID_msg = ?
ORDER BY m.posted_at DESC
""");
try {
ps.setInt(1, user.getId());
ResultSet rs = ps.executeQuery();
while(rs.next()){
int msgID = rs.getInt(1);
int sender = rs.getInt(2);
int threadID = rs.getInt(3);
String content = rs.getString(4);
Date creationDate = rs.getDate(5);
messages.add(new Message(msgID, sender, threadID, content, creationDate));
} }
} }
catch (SQLException sqle) {
sqle.getStackTrace();
}
return messages;
} }
} }
\ No newline at end of file
package dto; package dto;
import java.util.Date;
public class Message { public class Message {
private int msgId; private int msgId;
private int senderId; private int senderId;
private int threadId; private int threadId;
private String content; private String content;
private Date creationDate;
// Constructor // // Constructor //
public Message(int msgId, int senderId, int threadId, String content) { public Message(int msgId, int senderId, int threadId, String content, Date creationDate) {
this.msgId = msgId; this.msgId = msgId;
this.senderId = senderId; this.senderId = senderId;
this.threadId = threadId; this.threadId = threadId;
this.content = content; this.content = content;
this.creationDate = creationDate;
} }
......
...@@ -14,6 +14,7 @@ CREATE TABLE msg ( ...@@ -14,6 +14,7 @@ CREATE TABLE msg (
userID_msg int, userID_msg int,
threadID int, threadID int,
msg text, msg text,
posted_at DATE,
CONSTRAINT fk_message FOREIGN KEY (userID_msg) REFERENCES userAccount(userID), CONSTRAINT fk_message FOREIGN KEY (userID_msg) REFERENCES userAccount(userID),
CONSTRAINT pk_message PRIMARY KEY (msgID) CONSTRAINT pk_message PRIMARY KEY (msgID)
); );
......
...@@ -7,7 +7,7 @@ INSERT INTO userAccount VALUES (1, 'toto', 'totopwd123'), ...@@ -7,7 +7,7 @@ INSERT INTO userAccount VALUES (1, 'toto', 'totopwd123'),
INSERT INTO thread VALUES (1, 2, 'Gaming'), INSERT INTO thread VALUES (1, 2, 'Gaming'),
(2, 3, 'Animaux'), (2, 3, 'Animaux'),
(3, 3, 'Objet perdu'), (3, 3, 'Objet perdu'),
(4, 1, 'Discution'); (4, 1, 'Discussion');
INSERT INTO follow VALUES (2,1, TO_DATE('11/02/2025', 'DD/MM/YYYY')), INSERT INTO follow VALUES (2,1, TO_DATE('11/02/2025', 'DD/MM/YYYY')),
...@@ -16,9 +16,9 @@ INSERT INTO follow VALUES (2,1, TO_DATE('11/02/2025', 'DD/MM/YYYY')), ...@@ -16,9 +16,9 @@ INSERT INTO follow VALUES (2,1, TO_DATE('11/02/2025', 'DD/MM/YYYY')),
(1,4, TO_DATE('20/09/2025', 'DD/MM/YYYY')); (1,4, TO_DATE('20/09/2025', 'DD/MM/YYYY'));
INSERT INTO msg VALUES (1, 3 , 1, 'Bonjour !'), INSERT INTO msg VALUES (1, 3 , 1, 'Bonjour !', TO_DATE('11/02/2025', 'DD/MM/YYYY'),
(2, 4, 1, 'Hello !'), (2, 4, 1, 'Hello !', TO_DATE('18/02/2025', 'DD/MM/YYYY'),
(3, 1, 3, 'Mon objet était ici.'); (3, 1, 3, 'Mon objet était ici.', TO_DATE('02/02/2025', 'DD/MM/YYYY');
INSERT INTO reactions VALUES (2, 1, 'TRUE'), INSERT INTO reactions VALUES (2, 1, 'TRUE'),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment