Refactored OutfitController for futures

This commit is contained in:
Aleksei 2020-02-25 09:49:13 -05:00
parent d2b64760ce
commit bcb7c260a4
2 changed files with 147 additions and 86 deletions

View File

@ -6,13 +6,11 @@
package dev.salmonllama.fsbot.commands.developer; package dev.salmonllama.fsbot.commands.developer;
import dev.salmonllama.fsbot.database.controllers.OutfitController; import dev.salmonllama.fsbot.database.controllers.OutfitController;
import dev.salmonllama.fsbot.database.models.Outfit;
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;
import dev.salmonllama.fsbot.guthix.PermissionType; import dev.salmonllama.fsbot.guthix.PermissionType;
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;
@ -27,11 +25,8 @@ public class TestCommand extends Command {
@Override @Override
public void onCommand(CommandContext ctx) { public void onCommand(CommandContext ctx) {
try { OutfitController.findRandom().thenAccept(outfit -> {
Outfit outfit = OutfitController.findRandom();
ctx.reply(outfit.toString()); ctx.reply(outfit.toString());
} catch (SQLException e) { });
ctx.reply(e.getSQLState());
}
} }
} }

View File

@ -11,9 +11,82 @@ import dev.salmonllama.fsbot.database.models.Outfit;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
public class OutfitController { // TODO: Refactor for closing via FSDB.get(). SQLExceptions should be elevated; thrown in the command for logging. public class OutfitController {
public static void insert(Outfit outfit) { public static CompletableFuture<Void> insert(Outfit outfit) {
return CompletableFuture.runAsync(() -> {
try {
insertExec(outfit);
} catch (SQLException e) {
throw new CompletionException(e);
}
});
}
public static CompletableFuture<Optional<Outfit>> findById(String id) {
return CompletableFuture.supplyAsync(() -> {
try {
return findByIdExec(id);
} catch (SQLException e) {
throw new CompletionException(e);
}
});
}
public static CompletableFuture<Outfit> findRandom() {
return CompletableFuture.supplyAsync(() -> {
try {
return findRandomExec();
} catch (SQLException e) {
throw new CompletionException(e);
}
});
}
public static CompletableFuture<Optional<Outfit>> findRandomByTag(String tag) {
return CompletableFuture.supplyAsync(() -> {
try {
return findRandomByTagExec(tag);
} catch (SQLException e) {
throw new CompletionException(e);
}
});
}
public static CompletableFuture<Optional<Outfit>> findRandomBySubmitter(String submitterId) {
return CompletableFuture.supplyAsync(() -> {
try {
return findRandomBySubmitterExec(submitterId);
} catch (SQLException e) {
throw new CompletionException(e);
}
});
}
public static CompletableFuture<Integer> countOutfits() {
return CompletableFuture.supplyAsync(() -> {
try {
return countOutfitsExec();
} catch (SQLException e) {
throw new CompletionException(e);
}
});
}
public static CompletableFuture<Integer> countOutfitsBySubmitter(String submitterId) {
return CompletableFuture.supplyAsync(() -> {
try {
return countOutfitsBySubmitterExec(submitterId);
} catch (SQLException e) {
throw new CompletionException(e);
}
});
}
private static void insertExec(Outfit outfit) throws SQLException {
if (outfit.created == null) { if (outfit.created == null) {
outfit.created = new Timestamp(System.currentTimeMillis()); outfit.created = new Timestamp(System.currentTimeMillis());
} }
@ -22,7 +95,6 @@ public class OutfitController { // TODO: Refactor for closing via FSDB.get(). SQ
outfit.updated = new Timestamp(System.currentTimeMillis()); outfit.updated = new Timestamp(System.currentTimeMillis());
} }
try {
FSDB.get().insert( FSDB.get().insert(
"INSERT INTO " + "INSERT INTO " +
"outfits('id', 'link', 'submitter', 'tag', 'created', 'updated', 'deleted', 'featured', 'display_count', 'deletion_hash') " + "outfits('id', 'link', 'submitter', 'tag', 'created', 'updated', 'deleted', 'featured', 'display_count', 'deletion_hash') " +
@ -38,86 +110,80 @@ public class OutfitController { // TODO: Refactor for closing via FSDB.get(). SQ
outfit.displayCount, outfit.displayCount,
outfit.deletionHash outfit.deletionHash
); );
} catch (SQLException e) {
e.printStackTrace();
}
} }
public static Outfit findById(String id) throws SQLException { private static Optional<Outfit> findByIdExec(String id) throws SQLException {
ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE id = ?", id);
if (rs.next()) {
Outfit outfit = mapObject(rs);
FSDB.get().close(rs);
return Optional.of(outfit);
}
FSDB.get().close(rs);
return Optional.empty();
}
private static Outfit findRandomExec() throws SQLException {
ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE deleted = 0 ORDER BY random() LIMIT 1");
Outfit outfit = new Outfit(); Outfit outfit = new Outfit();
try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE id = ?", id)) {
if (rs.next()) { if (rs.next()) {
outfit = mapObject(rs); outfit = mapObject(rs);
} }
rs.getStatement().close(); FSDB.get().close(rs);
} catch (SQLException e) {
e.printStackTrace();
}
return outfit; return outfit;
} }
public static Outfit findRandom() throws SQLException { private static Optional<Outfit> findRandomByTagExec(String tag) throws SQLException {
Outfit outfit = new Outfit(); ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE tag = ? AND deleted = 0 ORDER BY random() LIMIT 1", tag);
try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE deleted = 0 ORDER BY random() LIMIT 1")) {
if (rs.next()) { if (rs.next()) {
outfit = mapObject(rs); Outfit outfit = mapObject(rs);
} FSDB.get().close(rs);
rs.getStatement().close(); return Optional.of(outfit);
} catch (SQLException e) {
e.printStackTrace();
}
return outfit;
} }
public static Outfit findRandomByTag(String tag) { FSDB.get().close(rs);
Outfit outfit = new Outfit(); return Optional.empty();
try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE tag = ? AND deleted = 0 ORDER BY random() LIMIT 1", tag)) { }
private static Optional<Outfit> findRandomBySubmitterExec(String submitterId) throws SQLException {
ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE submitter = ? AND deleted = 0 ORDER BY random() LIMIT 1", submitterId);
if (rs.next()) { if (rs.next()) {
outfit = mapObject(rs); Outfit outfit = mapObject(rs);
} FSDB.get().close(rs);
rs.getStatement().close(); return Optional.of(outfit);
} catch (SQLException e) {
e.printStackTrace();
}
return outfit;
} }
public static Outfit findRandomBySubmitter(String submitterId) { FSDB.get().close(rs);
Outfit outfit = new Outfit(); return Optional.empty();
try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE submitter = ? AND deleted = 0 ORDER BY random() LIMIT 1", submitterId)) {
if (rs.next()) {
outfit = mapObject(rs);
}
rs.getStatement().close();
} catch (SQLException e) {
e.printStackTrace();
}
return outfit;
} }
public static int countOutfits() { private static int countOutfitsExec() throws SQLException {
ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits WHERE deleted = 0");
int count = 0; int count = 0;
try (ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits WHERE deleted = 0")) {
if (rs.next()) { if (rs.next()) {
count = rs.getInt("count"); count = rs.getInt("count");
} }
rs.getStatement().close(); FSDB.get().close(rs);
} catch (SQLException e) {
e.printStackTrace();
}
return count; return count;
} }
public static int countOutfitsBySubmitter(String submitterId) { private static int countOutfitsBySubmitterExec(String submitterId) throws SQLException {
ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits WHERE submitter = ? AND deleted = 0", submitterId);
int count = 0; int count = 0;
try (ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits WHERE submitter = ? AND deleted = 0", submitterId)) {
if (rs.next()) { if (rs.next()) {
count = rs.getInt("count"); count = rs.getInt("count");
} }
rs.getStatement().close(); FSDB.get().close(rs);
} catch (SQLException e) {
e.printStackTrace();
}
return count; return count;
} }