From 30aa2df849ac867de854e610bb96f137c8a4cf6d Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Fri, 16 Sep 2022 02:42:32 +0200 Subject: [PATCH] Added history command --- .../queries/QueriesHistory/History.java | 4 + .../QueriesHistory/QueriesHistory.java | 118 ++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 src/main/java/com/alttd/database/queries/QueriesHistory/History.java create mode 100644 src/main/java/com/alttd/database/queries/QueriesHistory/QueriesHistory.java diff --git a/src/main/java/com/alttd/database/queries/QueriesHistory/History.java b/src/main/java/com/alttd/database/queries/QueriesHistory/History.java new file mode 100644 index 0000000..af84faa --- /dev/null +++ b/src/main/java/com/alttd/database/queries/QueriesHistory/History.java @@ -0,0 +1,4 @@ +package com.alttd.database.queries.QueriesHistory; + +public record History(HistoryType historyType, String bannedBy, String reason, long time, long until, boolean active) { +} diff --git a/src/main/java/com/alttd/database/queries/QueriesHistory/QueriesHistory.java b/src/main/java/com/alttd/database/queries/QueriesHistory/QueriesHistory.java new file mode 100644 index 0000000..40b6e27 --- /dev/null +++ b/src/main/java/com/alttd/database/queries/QueriesHistory/QueriesHistory.java @@ -0,0 +1,118 @@ +package com.alttd.database.queries.QueriesHistory; + +import com.alttd.database.Database; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class QueriesHistory { + + public static List getHistory(UUID uuid) { + List historyList = getBans(uuid); + if (historyList == null) { + return null; + } + + List tmp = getMutes(uuid); + if (tmp == null) + return null; + historyList.addAll(tmp); + + tmp = getKicks(uuid); + if (tmp == null) + return null; + historyList.addAll(tmp); + + tmp = getWarns(uuid); + if (tmp == null) + return null; + historyList.addAll(tmp); + return historyList; + } + + public static List getBans(UUID uuid) { + String sql = "SELECT * FROM bans_view WHERE uuid = ?"; + try { + PreparedStatement statement = Database.getDatabase().getConnection().prepareStatement(sql); + statement.setString(1, uuid.toString()); + + ResultSet resultSet = statement.executeQuery(); + ArrayList historyList = new ArrayList<>(); + while (resultSet.next()) { + historyList.add(storeHistory(HistoryType.BAN, resultSet)); + } + return historyList; + } catch (SQLException exception) { + exception.printStackTrace(); + } + return null; + } + + public static List getMutes(UUID uuid) { + String sql = "SELECT * FROM mutes_view WHERE uuid = ?"; + try { + PreparedStatement statement = Database.getDatabase().getConnection().prepareStatement(sql); + statement.setString(1, uuid.toString()); + + ResultSet resultSet = statement.executeQuery(); + ArrayList historyList = new ArrayList<>(); + while (resultSet.next()) { + historyList.add(storeHistory(HistoryType.MUTE, resultSet)); + } + return historyList; + } catch (SQLException exception) { + exception.printStackTrace(); + } + return null; + } + + public static List getKicks(UUID uuid) { + String sql = "SELECT * FROM kicks_view WHERE uuid = ?"; + try { + PreparedStatement statement = Database.getDatabase().getConnection().prepareStatement(sql); + statement.setString(1, uuid.toString()); + + ResultSet resultSet = statement.executeQuery(); + ArrayList historyList = new ArrayList<>(); + while (resultSet.next()) { + historyList.add(storeHistory(HistoryType.KICK, resultSet)); + } + return historyList; + } catch (SQLException exception) { + exception.printStackTrace(); + } + return null; + } + + public static List getWarns(UUID uuid) { + String sql = "SELECT * FROM warnings_view WHERE uuid = ?"; + try { + PreparedStatement statement = Database.getDatabase().getConnection().prepareStatement(sql); + statement.setString(1, uuid.toString()); + + ResultSet resultSet = statement.executeQuery(); + ArrayList historyList = new ArrayList<>(); + while (resultSet.next()) { + historyList.add(storeHistory(HistoryType.WARN, resultSet)); + } + return historyList; + } catch (SQLException exception) { + exception.printStackTrace(); + } + return null; + } + + private static History storeHistory(HistoryType historyType, ResultSet resultSet) throws SQLException { + return new History(historyType, + resultSet.getString("banned_by_name"), + resultSet.getString("reason"), + resultSet.getLong("time"), + resultSet.getLong("until"), + resultSet.getInt("active") == 1); + } + +}