first commit

This commit is contained in:
Patrick
2026-05-01 18:46:17 +02:00
commit 61ae38701e
104 changed files with 20058 additions and 0 deletions
@@ -0,0 +1,97 @@
package de.winniepat.minePanel.persistence;
import de.winniepat.minePanel.logs.PanelLogEntry;
import java.sql.*;
import java.time.Instant;
import java.util.*;
public final class LogRepository {
private final Database database;
public LogRepository(Database database) {
this.database = database;
}
public void appendLog(String kind, String source, String message) {
String sql = "INSERT INTO panel_logs(kind, source, message, created_at) VALUES (?, ?, ?, ?)";
try (Connection connection = database.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, kind);
statement.setString(2, source);
statement.setString(3, message);
statement.setLong(4, Instant.now().toEpochMilli());
statement.executeUpdate();
} catch (SQLException exception) {
throw new IllegalStateException("Could not append panel log", exception);
}
}
public List<PanelLogEntry> recentLogs(int limit) {
int safeLimit = Math.max(1, Math.min(limit, 1000));
String sql = "SELECT id, kind, source, message, created_at FROM panel_logs ORDER BY id DESC LIMIT ?";
List<PanelLogEntry> entries = new ArrayList<>();
try (Connection connection = database.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setInt(1, safeLimit);
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
entries.add(new PanelLogEntry(
resultSet.getLong("id"),
resultSet.getString("kind"),
resultSet.getString("source"),
resultSet.getString("message"),
resultSet.getLong("created_at")
));
}
}
} catch (SQLException exception) {
throw new IllegalStateException("Could not query panel logs", exception);
}
return entries;
}
public List<PanelLogEntry> allLogsAscending() {
String sql = "SELECT id, kind, source, message, created_at FROM panel_logs ORDER BY id ASC";
List<PanelLogEntry> entries = new ArrayList<>();
try (Connection connection = database.getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
entries.add(new PanelLogEntry(
resultSet.getLong("id"),
resultSet.getString("kind"),
resultSet.getString("source"),
resultSet.getString("message"),
resultSet.getLong("created_at")
));
}
} catch (SQLException exception) {
throw new IllegalStateException("Could not query all panel logs", exception);
}
return entries;
}
public long latestLogId() {
String sql = "SELECT COALESCE(MAX(id), 0) AS latest_id FROM panel_logs";
try (Connection connection = database.getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery()) {
return resultSet.getLong("latest_id");
} catch (SQLException exception) {
throw new IllegalStateException("Could not get latest panel log id", exception);
}
}
public void clearLogs() {
String sql = "DELETE FROM panel_logs";
try (Connection connection = database.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.executeUpdate();
} catch (SQLException exception) {
throw new IllegalStateException("Could not clear panel logs", exception);
}
}
}