Select Git revision
UserDAO.java
-
Yannis Devos authoredYannis Devos authored
UserDAO.java 4.36 KiB
package dao;
import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import dto.Message;
import dto.User;
import jakarta.servlet.http.*;
public class UserDAO extends HttpServlet{
private Connection con;
public UserDAO(){
this.con = new BDConnection().getConnection();
}
public List<User> getAllUsers(){
ArrayList<User> users = new ArrayList<>();
try{
Statement stmt = this.con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM userAccount;");
while(rs.next()){
int userId = rs.getInt(1);
String username = rs.getString(2);
String pwd = rs.getString(3);
User nUser = new User(userId, username, pwd);
users.add(nUser);
}
} catch (SQLException sqle) {
sqle.getStackTrace();
}
return users;
}
public User getUserByLogs(String username, String password){
User user = new User(0,null,null);
try{
PreparedStatement stmt = con.prepareStatement("SELECT username,password FROM userAccount WHERE username=? AND password=?"); //à changer pour utiliser le DAO
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
int userId = rs.getInt(1);
String login = rs.getString(2);
String pwd = rs.getString(3);
user = new User(userId,login,pwd);
}
} catch (SQLException sqle) {
sqle.getStackTrace();
}
return user;
}
public User getUserById(int id){
User user = new User(0,null,null);
try{
PreparedStatement stmt = this.con.prepareStatement("SELECT * FROM userAccount WHERE id=?");
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
if(rs.next()){
int userId = rs.getInt(1);
String username = rs.getString(2);
String pwd = rs.getString(3);
user = new User(userId,username,pwd);
}
} catch (SQLException sqle) {
sqle.getStackTrace();
}
return user;
}
public void addUser(User nUser){
try{
PreparedStatement stmt = this.con.prepareStatement("INSERT INTO userAccount (username, password) VALUES (?,?)");
stmt.setString(1, nUser.getUserName());
stmt.setString(2, nUser.getPwd());
stmt.executeUpdate();
} catch (SQLException sqle) {
sqle.getStackTrace();
}
}
public void removeUser(User nUser){
try{
PreparedStatement stmt = this.con.prepareStatement("DELETE FROM userAccount WHERE userID=?");
stmt.setInt(1, nUser.getId());
stmt.executeUpdate();
} catch (SQLException sqle) {
sqle.getStackTrace();
}
}
// S'abonner à un thread
public void followThread(User user, Thread thread) throws SQLException {
PreparedStatement ps = this.con.prepareStatement("INSERT INTO follow VALUES(?, ?, ?)");
try {
ps.setString(1, ""+user.getId());
ps.setString(2, ""+thread.getId());
ps.setString(3, LocalDate.now().toString());
}
catch (SQLException sqle) {
sqle.getStackTrace();
}
}
// Poster un message dans un thread
public void postMessage(User user, Thread thread, String message) throws SQLException {
PreparedStatement ps = this.con.prepareStatement("INSERT INTO msg (userID_msg, threadID, msg) VALUES(?, ?, ?)");
try {
ps.setInt(1, user.getId());
ps.setInt(2, (int) thread.getId());
ps.setString(3, message);
}
catch (SQLException sqle) {
sqle.getStackTrace();
}
}
// Réagir à un message
public void reactToMsg(User user, Message msg) throws SQLException {
PreparedStatement ps = this.con.prepareStatement("INSERT INTO reactions VALUES(?, ?, ?)");
try {
ps.setInt(1, user.getId());
ps.setInt(2, msg.getId());
ps.setString(3, "true");
}
catch (SQLException sqle) {
sqle.getStackTrace();
}
}
}