ServerConfig model and controller
This commit is contained in:
parent
f2113b7671
commit
07fd40a948
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* 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.ServerConfig;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionException;
|
||||||
|
|
||||||
|
public class ServerConfigController {
|
||||||
|
// insert
|
||||||
|
// retrieve
|
||||||
|
// update
|
||||||
|
// No need for delete -> guilds should persist after leaving.
|
||||||
|
|
||||||
|
public static CompletableFuture<Void> insert(ServerConfig config) {
|
||||||
|
return CompletableFuture.runAsync(() -> {
|
||||||
|
try {
|
||||||
|
insertExec(config);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new CompletionException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CompletableFuture<Optional<ServerConfig>> getServerConfig(String serverId) {
|
||||||
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
|
try {
|
||||||
|
return getServerConfigExec(serverId);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new CompletionException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void insertExec(ServerConfig config) throws SQLException {
|
||||||
|
FSDB.get().insert("INSERT INTO server_config('id', 'name', 'prefix') VALUES (?, ?, ?)",
|
||||||
|
config.getId(),
|
||||||
|
config.getName(),
|
||||||
|
config.getPrefix()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Optional<ServerConfig> getServerConfigExec(String serverId) throws SQLException {
|
||||||
|
ResultSet rs = FSDB.get().select("SELECT * FROM server_config WHERE id = ?");
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
ServerConfig config = mapObject(rs);
|
||||||
|
FSDB.get().close(rs);
|
||||||
|
return Optional.of(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
FSDB.get().close(rs);
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ServerConfig mapObject(ResultSet rs) throws SQLException {
|
||||||
|
return new ServerConfig.ServerConfigBuilder()
|
||||||
|
.setId(rs.getString("id"))
|
||||||
|
.setName(rs.getString("name"))
|
||||||
|
.setPrefix(rs.getString("prefix"))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
package dev.salmonllama.fsbot.database.models;
|
||||||
|
|
||||||
|
import dev.salmonllama.fsbot.database.DatabaseModel;
|
||||||
|
|
||||||
|
public class ServerConfig extends DatabaseModel {
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private String prefix;
|
||||||
|
|
||||||
|
public ServerConfig(ServerConfigBuilder builder) {
|
||||||
|
this.id = builder.id;
|
||||||
|
this.name = builder.name;
|
||||||
|
this.prefix = builder.prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPrefix() {
|
||||||
|
return prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String schema() {
|
||||||
|
return "CREATE TABLE IF NOT EXISTS server_config (" +
|
||||||
|
"id TEXT PRIMARY KEY ," +
|
||||||
|
"name TEXT," +
|
||||||
|
"prefix TEXT)";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("Server Config: [id: %s, name: %s, prefix: %s",
|
||||||
|
id, name, prefix
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ServerConfigBuilder {
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private String prefix;
|
||||||
|
|
||||||
|
public ServerConfigBuilder() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerConfigBuilder setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerConfigBuilder setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerConfigBuilder setPrefix(String prefix) {
|
||||||
|
this.prefix = prefix;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerConfig build() {
|
||||||
|
return new ServerConfig(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user