From 4588be4d74d34e3dc2de84641d5e168e022b5392 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Fri, 29 Mar 2024 11:27:51 +0100 Subject: [PATCH] Modified Database class and added correlated classes --- src/main/java/controllers/Database.java | 117 +++++++++++++++++++++++- src/main/java/models/Conversation.java | 4 + src/main/java/models/Message.java | 7 ++ 3 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 src/main/java/models/Conversation.java create mode 100644 src/main/java/models/Message.java diff --git a/src/main/java/controllers/Database.java b/src/main/java/controllers/Database.java index 47ebb64..cf49c25 100644 --- a/src/main/java/controllers/Database.java +++ b/src/main/java/controllers/Database.java @@ -1,12 +1,17 @@ package controllers; import models.Account; +import models.Conversation; +import models.Message; import org.mariadb.jdbc.MariaDbBlob; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import java.math.BigInteger; import java.sql.*; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; public class Database { public static boolean registerAccount(String username, String password, BigInteger e, BigInteger d, BigInteger n) { @@ -33,7 +38,7 @@ public class Database { return false; } } catch (SQLException ex) { - System.err.println("Error while trying to open a connection in: " + ex); + System.err.println("Error while trying to open a connection: " + ex); return false; } } @@ -65,7 +70,7 @@ public class Database { return null; } } catch (SQLException e) { - System.err.println("Error while trying to open a connection in: " + e); + System.err.println("Error while trying to open a connection: " + e); throw new RuntimeException(e); } } @@ -87,11 +92,117 @@ public class Database { return -1; } } catch (SQLException e) { - System.err.println("Error while trying to retrieve the id from the username"); + System.err.println("Error while trying to retrieve the id from the username: " + e); return -1; } } + public static boolean addMessage(int conversationId, String message) { + try (Connection connection = getConnection()) { + if (connection == null) return false; + + try (PreparedStatement statement = connection.prepareStatement(""" + INSERT INTO messages(message, conversation_id, sending_date, sending_time) + VALUES (?, ?, ?, ?) + """)) { + statement.setString(1, message); + statement.setInt(2, conversationId); + statement.setDate(3, Date.valueOf(LocalDateTime.now().toLocalDate())); + statement.setTime(3, Time.valueOf(LocalDateTime.now().toLocalTime())); + + int rowsInserted = statement.executeUpdate(); + return rowsInserted == 1; + } catch (SQLException ex) { + System.err.println("Error while trying to add a message: " + ex); + return false; + } + } catch (SQLException ex) { + System.err.println("Error while trying to open a connection: " + ex); + return false; + } + } + + public static List getConversations(int sender_id) { + try (Connection connection = getConnection()) { + if (connection == null) return null; + + try (PreparedStatement statement = connection.prepareStatement(""" + SELECT id, sender, recipient + FROM conversations + WHERE sender = ? OR recipient = ? + """)) { + statement.setInt(1, sender_id); + statement.setInt(2, sender_id); + ResultSet resultSet = statement.executeQuery(); + List conversations = new ArrayList<>(); + while (resultSet.next()) { + int id = resultSet.getInt(1); + int sender = resultSet.getInt(2); + int recipient = resultSet.getInt(3); + conversations.add(new Conversation(id, sender, recipient)); + } + return conversations; + } catch (SQLException e) { + System.err.println("Error while trying to get conversations: " + e); + return null; + } + } catch (SQLException e) { + System.err.println("Error while trying to open a connection: " + e); + throw new RuntimeException(e); + } + } + + public static boolean addConversation(int conversation_id) { + try (Connection connection = getConnection()) { + if (connection == null) return false; + + try (PreparedStatement statement = connection.prepareStatement(""" + INSERT INTO conversations(id, sender, recipient) + VALUES (?, ?, ?) + """)) { + statement.setInt(1, conversation_id); + + int rowsInserted = statement.executeUpdate(); + return rowsInserted == 1; + } catch (SQLException ex) { + System.err.println("Error while trying to add a conversation: " + ex); + return false; + } + } catch (SQLException ex) { + System.err.println("Error while trying to open a connection: " + ex); + return false; + } + } + + public static List getMessages(int conversation_id) { + try (Connection connection = getConnection()) { + if (connection == null) return null; + + try (PreparedStatement statement = connection.prepareStatement(""" + SELECT message, sending_date, sending_time + FROM messages + WHERE conversation_id = ? + """)) { + statement.setInt(1, conversation_id); + ResultSet resultSet = statement.executeQuery(); + List messages = new ArrayList<>(); + while (resultSet.next()) { + String message = resultSet.getString(1); + Date sending_date = resultSet.getDate(2); + Time sending_time = resultSet.getTime(3); + messages.add(new Message(message, sending_date, sending_time)); + } + return messages; + } catch (SQLException e) { + System.err.println("Error while trying to get messages: " + e); + return null; + } + } catch (SQLException e) { + System.err.println("Error while trying to open a connection: " + e); + throw new RuntimeException(e); + } + } + private static Connection getConnection() throws SQLException { return DriverManager.getConnection( System.getenv("db_url"), diff --git a/src/main/java/models/Conversation.java b/src/main/java/models/Conversation.java new file mode 100644 index 0000000..aac6034 --- /dev/null +++ b/src/main/java/models/Conversation.java @@ -0,0 +1,4 @@ +package models; + +public record Conversation(int id, int sender, int recipient) { +} diff --git a/src/main/java/models/Message.java b/src/main/java/models/Message.java new file mode 100644 index 0000000..3eb15ae --- /dev/null +++ b/src/main/java/models/Message.java @@ -0,0 +1,7 @@ +package models; + +import java.sql.Date; +import java.sql.Time; + +public record Message(String message, Date sending_date, Time sending_time) { +}