Modified Database class and added correlated classes
This commit is contained in:
parent
c04f952607
commit
4588be4d74
@ -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<Conversation> 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<Conversation> 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<Message> 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<Message> 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"),
|
||||
|
4
src/main/java/models/Conversation.java
Normal file
4
src/main/java/models/Conversation.java
Normal file
@ -0,0 +1,4 @@
|
||||
package models;
|
||||
|
||||
public record Conversation(int id, int sender, int recipient) {
|
||||
}
|
7
src/main/java/models/Message.java
Normal file
7
src/main/java/models/Message.java
Normal file
@ -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) {
|
||||
}
|
Loading…
Reference in New Issue
Block a user