Plex/server/src/main/java/dev/plex/storage/punishment/SQLNotes.java

99 lines
3.4 KiB
Java
Raw Normal View History

package dev.plex.storage.punishment;
2022-04-07 00:38:15 +00:00
import com.google.common.collect.Lists;
import dev.plex.Plex;
import dev.plex.punishment.extra.Note;
import dev.plex.util.TimeUtils;
2023-03-08 20:26:10 +00:00
2022-04-07 00:38:15 +00:00
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
2022-05-10 05:08:45 +00:00
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
2022-04-07 00:38:15 +00:00
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class SQLNotes
{
private static final String SELECT = "SELECT * FROM `notes` WHERE uuid=?";
2022-04-07 02:05:29 +00:00
private static final String INSERT = "INSERT INTO `notes` (`id`, `uuid`, `written_by`, `note`, `timestamp`) VALUES(?, ?, ?, ?, ?)";
private static final String DELETE = "DELETE FROM `notes` WHERE uuid=? AND id=?";
2022-04-07 00:38:15 +00:00
public CompletableFuture<List<Note>> getNotes(UUID uuid)
{
return CompletableFuture.supplyAsync(() ->
{
List<Note> notes = Lists.newArrayList();
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement(SELECT);
statement.setString(1, uuid.toString());
ResultSet set = statement.executeQuery();
while (set.next())
{
Note note = new Note(
uuid,
set.getString("note"),
UUID.fromString(set.getString("written_by")),
ZonedDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("timestamp")), ZoneId.of(TimeUtils.TIMEZONE))
2022-04-07 00:38:15 +00:00
);
2022-04-07 02:05:29 +00:00
note.setId(set.getInt("id"));
2022-04-07 00:38:15 +00:00
notes.add(note);
}
2022-04-07 02:39:45 +00:00
}
catch (SQLException e)
2022-04-07 00:38:15 +00:00
{
e.printStackTrace();
return notes;
2022-04-07 00:38:15 +00:00
}
return notes;
});
}
2022-04-07 02:05:29 +00:00
public CompletableFuture<Void> deleteNote(int id, UUID uuid)
2022-04-07 00:38:15 +00:00
{
return CompletableFuture.runAsync(() ->
{
try (Connection con = Plex.get().getSqlConnection().getCon())
{
2022-04-07 02:05:29 +00:00
PreparedStatement statement = con.prepareStatement(DELETE);
statement.setString(1, uuid.toString());
statement.setInt(2, id);
2022-04-07 00:38:15 +00:00
statement.execute();
2022-04-07 02:39:45 +00:00
}
catch (SQLException e)
2022-04-07 00:38:15 +00:00
{
e.printStackTrace();
}
});
}
2022-04-07 02:05:29 +00:00
public CompletableFuture<Void> addNote(Note note)
{
return CompletableFuture.runAsync(() ->
{
getNotes(note.getUuid()).whenComplete((notes, throwable) ->
{
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement(INSERT);
statement.setInt(1, notes.size() + 1);
2022-04-07 02:05:29 +00:00
statement.setString(2, note.getUuid().toString());
statement.setString(3, note.getWrittenBy().toString());
statement.setString(4, note.getNote());
statement.setLong(5, note.getTimestamp().toInstant().toEpochMilli());
2022-04-07 02:05:29 +00:00
statement.execute();
note.setId(notes.size());
2022-04-07 02:39:45 +00:00
}
catch (SQLException e)
2022-04-07 02:05:29 +00:00
{
e.printStackTrace();
}
});
});
}
2022-04-07 00:38:15 +00:00
}