From 9d560b04784d193db3dc97934f4a84038573dadf Mon Sep 17 00:00:00 2001 From: Aleksei Date: Thu, 27 Feb 2020 00:51:57 -0500 Subject: [PATCH] Added methods for multiple outfits --- .../controllers/OutfitController.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java b/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java index 9b96505..34388e0 100644 --- a/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java +++ b/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java @@ -48,6 +48,16 @@ public class OutfitController { }); } + public static CompletableFuture>> findRandom(int amount) { + return CompletableFuture.supplyAsync(() -> { + try { + return findRandomExec(amount); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + public static CompletableFuture> findRandomByTag(String tag) { return CompletableFuture.supplyAsync(() -> { try { @@ -58,6 +68,16 @@ public class OutfitController { }); } + public static CompletableFuture>> findRandomByTag(String tag, int amount) { + return CompletableFuture.supplyAsync(() -> { + try { + return findRandomByTagExec(tag, amount); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + public static CompletableFuture> findRandomBySubmitter(String submitterId) { return CompletableFuture.supplyAsync(() -> { try { @@ -68,6 +88,16 @@ public class OutfitController { }); } + public static CompletableFuture>> findRandomBySubmitter(String submitterId, int amount) { + return CompletableFuture.supplyAsync(() -> { + try { + return findRandomBySubmitterExec(submitterId, amount); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + public static CompletableFuture countOutfits() { return CompletableFuture.supplyAsync(() -> { try { @@ -150,6 +180,12 @@ public class OutfitController { return Optional.empty(); } + private static Optional> findRandomExec(int amount) throws SQLException { + ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE deleted = 0 ORDER BY random() LIMIT ?", amount); + + return extractMultiple(rs); + } + private static Optional findRandomByTagExec(String tag) throws SQLException { ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE tag = ? AND deleted = 0 ORDER BY random() LIMIT 1", tag); @@ -163,6 +199,12 @@ public class OutfitController { return Optional.empty(); } + private static Optional> findRandomByTagExec(String tag, int amount) throws SQLException { // Still has to be an optional, the tag may not exist. It is user-supplied. + ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE tag = ? AND deleted = 0 ORDER BY random() LIMIT ?", tag, amount); + + return extractMultiple(rs); + } + private static Optional findRandomBySubmitterExec(String submitterId) throws SQLException { ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE submitter = ? AND deleted = 0 ORDER BY random() LIMIT 1", submitterId); @@ -176,6 +218,12 @@ public class OutfitController { return Optional.empty(); } + private static Optional> findRandomBySubmitterExec(String submitterId, int amount) throws SQLException { + ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE submitter = ? AND deleted = 0 ORDER BY random() LIMIT ?", submitterId, amount); + + return extractMultiple(rs); + } + private static int countOutfitsExec() throws SQLException { ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits WHERE deleted = 0"); @@ -212,6 +260,20 @@ public class OutfitController { return tags; } + private static Optional> extractMultiple(ResultSet rs) throws SQLException { + Collection outfits = new ArrayList<>(); + if (rs.next()) { // I don't see a better way to wrap this. If results exist -> iterate through them, return the optional. + while (rs.next()) { + outfits.add(mapObject(rs)); + } + FSDB.get().close(rs); + return Optional.of(outfits); + } + + FSDB.get().close(rs); + return Optional.empty(); + } + private static Outfit mapObject(ResultSet rs) throws SQLException { return new Outfit.OutfitBuilder() .setId(rs.getString("id"))