Moved WelcomeMessages to ServerConf

This commit is contained in:
Aleksei 2020-07-27 17:27:07 -04:00
parent d1dd1fe758
commit badb05202b
5 changed files with 49 additions and 236 deletions

View File

@ -43,7 +43,6 @@ public class FSDB {
get().query(ServerConfig.schema());
get().query(ServerBlacklist.schema());
get().query(UserBlacklist.schema());
get().query(WelcomeMessage.schema());
get().query(StaticPermission.schema());
} catch (SQLException e) {
e.printStackTrace();

View File

@ -30,10 +30,10 @@ public class ServerConfigController {
});
}
public static CompletableFuture<Optional<ServerConfig>> getServerConfig(String serverId) {
public static CompletableFuture<Optional<ServerConfig>> get(String serverId) {
return CompletableFuture.supplyAsync(() -> {
try {
return getServerConfigExec(serverId);
return getExec(serverId);
} catch (SQLException e) {
throw new CompletionException(e);
}
@ -51,14 +51,14 @@ public class ServerConfigController {
}
private static void insertExec(ServerConfig config) throws SQLException {
FSDB.get().insert("INSERT INTO server_config('id', 'name', 'prefix') VALUES (?, ?, ?)",
FSDB.get().insert("INSERT INTO server_config(id, prefix, welcome_message) VALUES (?, ?, ?)",
config.getId(),
config.getName(),
config.getPrefix()
config.getPrefix(),
config.getWelcomeMessage()
);
}
private static Optional<ServerConfig> getServerConfigExec(String serverId) throws SQLException {
private static Optional<ServerConfig> getExec(String serverId) throws SQLException {
ResultSet rs = FSDB.get().select("SELECT * FROM server_config WHERE id = ?");
if (rs.next()) {
@ -72,9 +72,10 @@ public class ServerConfigController {
}
private static void updateExec(ServerConfig config) throws SQLException {
FSDB.get().query("UPDATE server_config SET prefix = ?, name = ? WHERE id = ?",
FSDB.get().query("UPDATE server_config SET prefix = ?, welcome_message = ?, welcome_channel = ?, WHERE id = ?",
config.getPrefix(),
config.getName(),
config.getWelcomeMessage(),
config.getWelcomeChannel(),
config.getId()
);
}
@ -82,8 +83,9 @@ public class ServerConfigController {
private static ServerConfig mapObject(ResultSet rs) throws SQLException {
return new ServerConfig.ServerConfigBuilder()
.setId(rs.getString("id"))
.setName(rs.getString("name"))
.setPrefix(rs.getString("prefix"))
.setWelcomeMessage(rs.getString("welcome_message"))
.setWelcomeChannel(rs.getString("welcome_channel"))
.build();
}
}

View File

@ -1,130 +0,0 @@
/*
* 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.WelcomeMessage;
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 WelcomeMessageController {
// Needs insert, update, and delete. Only one per server -> needs exists
public static CompletableFuture<Void> insert(WelcomeMessage msg) {
return CompletableFuture.runAsync(() -> {
try {
insertExec(msg);
} catch (SQLException e) {
throw new CompletionException(e);
}
});
}
public static CompletableFuture<Optional<WelcomeMessage>> get(String id) {
return CompletableFuture.supplyAsync(() -> {
try {
return getExec(id);
} catch (SQLException e) {
throw new CompletionException(e);
}
});
}
public static CompletableFuture<Void> update(WelcomeMessage msg) {
return CompletableFuture.runAsync(() -> {
try {
updateExec(msg);
} 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<Void> delete(String id) {
return CompletableFuture.runAsync(() -> {
try {
deleteExec(id);
} catch (SQLException e) {
throw new CompletionException(e);
}
});
}
public static CompletableFuture<Void> delete(WelcomeMessage msg) {
return CompletableFuture.runAsync(() -> {
try {
deleteExec(msg.getServerId());
} catch (SQLException e) {
throw new CompletionException(e);
}
});
}
private static void insertExec(WelcomeMessage msg) throws SQLException {
FSDB.get().insert("INSERT INTO welcome_messages (server_id, message, updated, editor) VALUES (?, ?, ?, ?)",
msg.getServerId(),
msg.getMessage(),
msg.getUpdated(),
msg.getEditor()
);
}
private static Optional<WelcomeMessage> getExec(String id) throws SQLException {
ResultSet rs = FSDB.get().select("SELECT * FROM welcome_messages WHERE server_id = ?", id);
if (rs.next()) {
WelcomeMessage msg = mapObject(rs);
FSDB.get().close(rs);
return Optional.of(msg);
}
FSDB.get().close(rs);
return Optional.empty();
}
private static void updateExec(WelcomeMessage msg) throws SQLException {
FSDB.get().query("UPDATE welcome_messages SET message = ?, updated = ?, editor = ? WHERE server_id = ?",
msg.getMessage(),
msg.getUpdated(),
msg.getEditor(),
msg.getServerId()
);
}
private static boolean existsExec(String id) throws SQLException {
ResultSet rs = FSDB.get().select("SELECT EXISTS(SELECT 1 FROM welcome_messages WHERE server_id = ?) AS hmm", id);
boolean exists = rs.getBoolean("hmm");
FSDB.get().close(rs);
return exists;
}
private static void deleteExec(String id) throws SQLException {
FSDB.get().query("DELETE FROM welcome_messages WHERE server_id = ?", id);
}
private static WelcomeMessage mapObject(ResultSet rs) throws SQLException {
return new WelcomeMessage.WelcomeMessageBuilder(rs.getString("server_id"))
.setMessage(rs.getString("message"))
.setUpdated(new Timestamp(rs.getLong("updated")))
.setEditor(rs.getString("editor"))
.build();
}
}

View File

@ -3,58 +3,69 @@ 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;
private final String id;
private final String prefix;
private final String welcomeMessage;
private final String welcomeChannel;
public ServerConfig(ServerConfigBuilder builder) {
this.id = builder.id;
this.name = builder.name;
this.prefix = builder.prefix;
this.welcomeMessage = builder.welcomeMessage;
this.welcomeChannel = builder.welcomeChannel;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getPrefix() {
return prefix;
}
public String getWelcomeMessage() {
return welcomeMessage;
}
public String getWelcomeChannel() {
return welcomeChannel;
}
public static String schema() {
return "CREATE TABLE IF NOT EXISTS server_config (" +
"id TEXT PRIMARY KEY ," +
"name TEXT," +
"prefix TEXT)";
"prefix TEXT," +
"welcome_message TEXT," +
"welcome_channel TEXT)";
}
@Override
public String toString() {
return String.format("Server Config: [id: %s, name: %s, prefix: %s]",
id, name, prefix
return String.format("Server Config: [id: %s, prefix: %s, welcome_message: %s, welcome_channel: %s]",
id, prefix, welcomeMessage, welcomeChannel
);
}
public static class ServerConfigBuilder {
private String id;
private String name;
private String prefix;
private String welcomeMessage;
private String welcomeChannel;
public ServerConfigBuilder() {
}
public ServerConfigBuilder setId(String id) {
this.id = id;
public ServerConfigBuilder from(ServerConfig config) {
this.id = config.id;
this.prefix = config.prefix;
this.welcomeMessage = config.welcomeMessage;
this.welcomeChannel = config.welcomeChannel;
return this;
}
public ServerConfigBuilder setName(String name) {
this.name = name;
public ServerConfigBuilder setId(String id) {
this.id = id;
return this;
}
@ -63,6 +74,16 @@ public class ServerConfig extends DatabaseModel {
return this;
}
public ServerConfigBuilder setWelcomeMessage(String welcomeMessage) {
this.welcomeMessage = welcomeMessage;
return this;
}
public ServerConfigBuilder setWelcomeChannel(String welcomeChannel) {
this.welcomeChannel = welcomeChannel;
return this;
}
public ServerConfig build() {
return new ServerConfig(this);
}

View File

@ -1,79 +0,0 @@
/*
* 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 WelcomeMessage extends DatabaseModel {
// Need the message, the last time it was updated, and the id of the last person who updated it.
private String serverId;
private String message;
private Timestamp updated;
private String editor;
private WelcomeMessage(WelcomeMessageBuilder builder) {
serverId = builder.serverId;
message = builder.message;
updated = builder.updated;
editor = builder.editor;
}
public String getServerId() {
return serverId;
}
public String getMessage() {
return message;
}
public Timestamp getUpdated() {
return updated;
}
public String getEditor() {
return editor;
}
public static String schema() {
return "CREATE TABLE IF NOT EXISTS welcome_messages (" +
"server_id TEXT PRIMARY KEY," +
"message TEXT," +
"updated TEXT," +
"editor TEXT)";
}
public static class WelcomeMessageBuilder {
private String serverId;
private String message = null;
private Timestamp updated = null;
private String editor = null;
public WelcomeMessageBuilder(String serverId) {
this.serverId = serverId;
}
public WelcomeMessageBuilder setMessage(String message) {
this.message = message;
return this;
}
public WelcomeMessageBuilder setUpdated(Timestamp updated) {
this.updated = updated;
return this;
}
public WelcomeMessageBuilder setEditor(String editor) {
this.editor = editor;
return this;
}
public WelcomeMessage build() {
return new WelcomeMessage(this);
}
}
}