Refactored OutfitController for futures
This commit is contained in:
parent
d2b64760ce
commit
bcb7c260a4
@ -6,13 +6,11 @@
|
||||
package dev.salmonllama.fsbot.commands.developer;
|
||||
|
||||
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.CommandContext;
|
||||
import dev.salmonllama.fsbot.guthix.CommandPermission;
|
||||
import dev.salmonllama.fsbot.guthix.PermissionType;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@ -27,11 +25,8 @@ public class TestCommand extends Command {
|
||||
|
||||
@Override
|
||||
public void onCommand(CommandContext ctx) {
|
||||
try {
|
||||
Outfit outfit = OutfitController.findRandom();
|
||||
OutfitController.findRandom().thenAccept(outfit -> {
|
||||
ctx.reply(outfit.toString());
|
||||
} catch (SQLException e) {
|
||||
ctx.reply(e.getSQLState());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,82 @@ import dev.salmonllama.fsbot.database.models.Outfit;
|
||||
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 OutfitController { // TODO: Refactor for closing via FSDB.get(). SQLExceptions should be elevated; thrown in the command for logging.
|
||||
public static void insert(Outfit outfit) {
|
||||
public class OutfitController {
|
||||
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) {
|
||||
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());
|
||||
}
|
||||
|
||||
try {
|
||||
FSDB.get().insert(
|
||||
"INSERT INTO " +
|
||||
"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.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();
|
||||
try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE id = ?", id)) {
|
||||
if (rs.next()) {
|
||||
outfit = mapObject(rs);
|
||||
}
|
||||
rs.getStatement().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
FSDB.get().close(rs);
|
||||
|
||||
return outfit;
|
||||
}
|
||||
|
||||
public static Outfit findRandom() throws SQLException {
|
||||
Outfit outfit = new Outfit();
|
||||
try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE deleted = 0 ORDER BY random() LIMIT 1")) {
|
||||
private static Optional<Outfit> findRandomByTagExec(String tag) throws SQLException {
|
||||
ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE tag = ? AND deleted = 0 ORDER BY random() LIMIT 1", tag);
|
||||
|
||||
if (rs.next()) {
|
||||
outfit = mapObject(rs);
|
||||
}
|
||||
rs.getStatement().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return outfit;
|
||||
Outfit outfit = mapObject(rs);
|
||||
FSDB.get().close(rs);
|
||||
return Optional.of(outfit);
|
||||
}
|
||||
|
||||
public static Outfit findRandomByTag(String tag) {
|
||||
Outfit outfit = new Outfit();
|
||||
try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE tag = ? AND deleted = 0 ORDER BY random() LIMIT 1", tag)) {
|
||||
FSDB.get().close(rs);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
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()) {
|
||||
outfit = mapObject(rs);
|
||||
}
|
||||
rs.getStatement().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return outfit;
|
||||
Outfit outfit = mapObject(rs);
|
||||
FSDB.get().close(rs);
|
||||
return Optional.of(outfit);
|
||||
}
|
||||
|
||||
public static Outfit findRandomBySubmitter(String submitterId) {
|
||||
Outfit outfit = new Outfit();
|
||||
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;
|
||||
FSDB.get().close(rs);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
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;
|
||||
try (ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits WHERE deleted = 0")) {
|
||||
if (rs.next()) {
|
||||
count = rs.getInt("count");
|
||||
}
|
||||
rs.getStatement().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
FSDB.get().close(rs);
|
||||
|
||||
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;
|
||||
try (ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits WHERE submitter = ? AND deleted = 0", submitterId)) {
|
||||
if (rs.next()) {
|
||||
count = rs.getInt("count");
|
||||
}
|
||||
rs.getStatement().close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
FSDB.get().close(rs);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user