Refactored CreateGallery with new Database

This commit is contained in:
Aleksei 2020-02-20 15:58:53 -05:00
parent 3715209c9f
commit 6e6074b770
2 changed files with 51 additions and 51 deletions

View File

@ -8,6 +8,8 @@ package dev.salmonllama.fsbot.commands.developer;
import com.rethinkdb.RethinkDB; import com.rethinkdb.RethinkDB;
import com.rethinkdb.net.Connection; import com.rethinkdb.net.Connection;
import dev.salmonllama.fsbot.config.BotConfig; import dev.salmonllama.fsbot.config.BotConfig;
import dev.salmonllama.fsbot.database.controllers.GalleryController;
import dev.salmonllama.fsbot.database.models.GalleryChannel;
import dev.salmonllama.fsbot.guthix.Command; import dev.salmonllama.fsbot.guthix.Command;
import dev.salmonllama.fsbot.guthix.CommandContext; import dev.salmonllama.fsbot.guthix.CommandContext;
import dev.salmonllama.fsbot.guthix.CommandPermission; import dev.salmonllama.fsbot.guthix.CommandPermission;
@ -17,6 +19,7 @@ import org.javacord.api.entity.message.embed.EmbedBuilder;
import org.javacord.api.entity.server.Server; import org.javacord.api.entity.server.Server;
import java.awt.*; import java.awt.*;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -34,63 +37,50 @@ public class CreateGalleryCommand extends Command { // TODO: This command needs
static final Connection CONN = R.connection().hostname("localhost").port(28015).connect(); static final Connection CONN = R.connection().hostname("localhost").port(28015).connect();
@Override @Override
public void onCommand(CommandContext ctx) { public void onCommand(CommandContext ctx) { // TODO: Might need some logic help...
Optional<Server> opServer = ctx.getServer(); if (ctx.isPrivateMessage()) {
TextChannel channel = ctx.getChannel(); ctx.reply("This command can only be used in a server!"); // TODO: Stop this. Turn this into a preset no-no embed.
String[] args = ctx.getArgs();
String targetChannelId = channel.getIdAsString();
String targetChannelName = channel.asServerChannel().get().getName(); // TODO: un-band-aid this.
if (!opServer.isPresent()) {
ctx.reply("This command can only be used in a server");
return; return;
} }
// Check if the channel is already a registered gallery channel.
// Create a gallery channel of the current channel.
// Store the gallery channel in the database.
Server server = opServer.get(); String channelId = ctx.getChannel().getIdAsString();
String targetServerName = server.getName(); try {
String targetServerId = server.getIdAsString(); if (GalleryController.galleryExists(channelId)) {
ctx.reply("A gallery already exists in this channel, can not create a new one.");
if (!server.getIdAsString().equals(BotConfig.HOME_SERVER)) {
channel.sendMessage("Fashion galleries can only be created in the Fashionscape server");
return; return;
} }
} catch (SQLException e) {
if (args.length < 1) { ctx.reply("An error occurred"); // TODO: error logging.
channel.sendMessage("No Arguments provided");
return;
}
else if (args.length > 1) {
channel.sendMessage("Too many arguments provided.");
return;
} }
String tag = args[0]; GalleryChannel gallery = new GalleryChannel();
gallery.channelId = channelId;
String tag = ctx.getArgs()[0];
if (R.db("fsbot").table("channels").getField("cId").contains(targetChannelId).run(CONN)) { ctx.getServer().ifPresent(server -> {
EmbedBuilder embed = new EmbedBuilder() gallery.serverId = server.getIdAsString();
.setColor(Color.RED) gallery.serverName = server.getName();
.addField("ERROR", "This channel is already gathering images"); });
channel.sendMessage(embed);
ctx.getChannel().asServerTextChannel().ifPresent(channel -> gallery.channelName = channel.getName());
try {
GalleryController.insert(gallery);
} catch(SQLException e) {
ctx.reply("An error occurred."); // TODO: error logging.
} }
else {
R.db("fsbot").table("channels").insert(
R.hashMap("sName", targetServerName)
.with("sId", targetServerId)
.with("cName", targetChannelName)
.with("cId", targetChannelId)
.with("tag", tag)
).run(CONN);
R.db("fsbot").tableCreate(tag).run(CONN);
EmbedBuilder embed = new EmbedBuilder() EmbedBuilder embed = new EmbedBuilder()
.setColor(Color.GREEN) .setColor(Color.GREEN)
.addField("Success", "Channel added to storage with the following values:") .addField("Success", "Channel added to storage with the following values:")
.addField("Channel Name:", targetChannelName) .addField("Channel Name:", gallery.channelName)
.addField("Channel Id:", targetChannelId) .addField("Channel Id:", gallery.channelId)
.addField("Tag:", tag) .addField("Tag:", tag)
.addField("End:", String.format("Table \"%s\" created for images in this channel", tag)); .addField("End:", String.format("Table \"%s\" created for images in this channel", tag));
channel.sendMessage(embed); ctx.getChannel().sendMessage(embed);
}
} }
} }

View File

@ -33,9 +33,19 @@ public class GalleryController {
galleries.add(mapObject(rs)); galleries.add(mapObject(rs));
} }
FSDB.get().close(rs);
return galleries; return galleries;
} }
public static boolean galleryExists(String channelId) throws SQLException {
ResultSet rs = FSDB.get().select("SELECT EXISTS(SELECT 1 AS exists FROM galleries WHERE channel_id = ?", channelId);
boolean exists = rs.getBoolean("exists");
FSDB.get().close(rs);
return exists;
}
private static GalleryChannel mapObject(ResultSet rs) throws SQLException { private static GalleryChannel mapObject(ResultSet rs) throws SQLException {
GalleryChannel gallery = new GalleryChannel(); GalleryChannel gallery = new GalleryChannel();
gallery.serverId = rs.getString("server_id"); gallery.serverId = rs.getString("server_id");