ServerBlacklist model/controller
This commit is contained in:
parent
bb8e31ff8d
commit
ef466013e0
@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020. Aleksei Gryczewski
|
||||||
|
* All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.salmonllama.fsbot.database.controllers;
|
||||||
|
|
||||||
|
import dev.salmonllama.fsbot.database.FSDB;
|
||||||
|
import dev.salmonllama.fsbot.database.models.ServerBlacklist;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionException;
|
||||||
|
|
||||||
|
public class ServerBlacklistController {
|
||||||
|
// Need to create, read, and delete. No need for update?
|
||||||
|
public static CompletableFuture<Void> insert(ServerBlacklist bl) {
|
||||||
|
return CompletableFuture.runAsync(() -> {
|
||||||
|
try {
|
||||||
|
insertExec(bl);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new CompletionException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<Boolean> exists(String id) {
|
||||||
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
|
try {
|
||||||
|
return existsExec(id);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new CompletionException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<Boolean> exists(ServerBlacklist bl) {
|
||||||
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
|
try {
|
||||||
|
return existsExec(bl.getId());
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new CompletionException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<Optional<ServerBlacklist>> get(String id) {
|
||||||
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
|
try {
|
||||||
|
return getExec(id);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new CompletionException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<Void> delete(String id) {
|
||||||
|
return CompletableFuture.runAsync(() -> {
|
||||||
|
try {
|
||||||
|
deleteExec(id);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new CompletionException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<Void> delete(ServerBlacklist bl) {
|
||||||
|
return CompletableFuture.runAsync(() -> {
|
||||||
|
try {
|
||||||
|
deleteExec(bl.getId());
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new CompletionException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void insertExec(ServerBlacklist bl) throws SQLException {
|
||||||
|
FSDB.get().insert("INSERT INTO blacklist_servers ('id', 'name', 'owner_id', 'added') VALUES (?, ?, ?, ?)",
|
||||||
|
bl.getId(),
|
||||||
|
bl.getName(),
|
||||||
|
bl.getOwnerId(),
|
||||||
|
bl.getAdded()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean existsExec(String id) throws SQLException {
|
||||||
|
ResultSet rs = FSDB.get().select("SELECT EXISTS(SELECT 1 FROM blacklist_servers WHERE id = ?) AS hmm", id);
|
||||||
|
boolean exists = rs.getBoolean("hmm");
|
||||||
|
|
||||||
|
FSDB.get().close(rs);
|
||||||
|
return exists;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Optional<ServerBlacklist> getExec(String id) throws SQLException {
|
||||||
|
ResultSet rs = FSDB.get().select("SELECT * FROM blacklist_servers WHERE id = ?", id);
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
ServerBlacklist bl = mapObject(rs);
|
||||||
|
FSDB.get().close(rs);
|
||||||
|
return Optional.of(bl);
|
||||||
|
}
|
||||||
|
|
||||||
|
FSDB.get().close(rs);
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void deleteExec(String id) throws SQLException {
|
||||||
|
FSDB.get().query("DELETE FROM blacklist_servers WHERE id = ?", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ServerBlacklist mapObject(ResultSet rs) throws SQLException {
|
||||||
|
return new ServerBlacklist.ServerBlacklistBuilder(rs.getString("id"))
|
||||||
|
.setName(rs.getString("name"))
|
||||||
|
.setOwnerId(rs.getString("owner_id"))
|
||||||
|
.setAdded(new Timestamp( rs.getLong("added")))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020. Aleksei Gryczewski
|
||||||
|
* All rights reserved.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package dev.salmonllama.fsbot.database.models;
|
||||||
|
|
||||||
|
import dev.salmonllama.fsbot.database.DatabaseModel;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
|
public class ServerBlacklist extends DatabaseModel {
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private String ownerId;
|
||||||
|
private Timestamp added;
|
||||||
|
|
||||||
|
private ServerBlacklist(ServerBlacklistBuilder builder) {
|
||||||
|
this.id = builder.id;
|
||||||
|
this.name = builder.name;
|
||||||
|
this.ownerId = builder.ownerId;
|
||||||
|
this.added = builder.added;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOwnerId() {
|
||||||
|
return ownerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Timestamp getAdded() {
|
||||||
|
return added;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String schema() {
|
||||||
|
return "CREATE TABLE IF NOT EXISTS blacklist_servers (" +
|
||||||
|
"id TEXT PRIMARY KEY," +
|
||||||
|
"name TEXT," +
|
||||||
|
"owner_id TEXT," +
|
||||||
|
"added TEXT)";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ServerBlacklistBuilder {
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private String ownerId;
|
||||||
|
private Timestamp added;
|
||||||
|
|
||||||
|
public ServerBlacklistBuilder(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerBlacklistBuilder setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerBlacklistBuilder setOwnerId(String ownerId) {
|
||||||
|
this.ownerId = ownerId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerBlacklistBuilder setAdded(Timestamp added) {
|
||||||
|
this.added = added;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerBlacklist build() {
|
||||||
|
return new ServerBlacklist(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user