From f1abf6998881ff8dfcf4e0ba50c18b064a007582 Mon Sep 17 00:00:00 2001 From: Aleksei Date: Fri, 28 Feb 2020 17:43:53 -0500 Subject: [PATCH] Added more counting methods --- .../controllers/OutfitController.java | 94 +++++++++++++++---- 1 file changed, 78 insertions(+), 16 deletions(-) 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 e583703..eb4ee87 100644 --- a/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java +++ b/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java @@ -98,10 +98,50 @@ public class OutfitController { }); } - public static CompletableFuture countOutfits() { + public static CompletableFuture countTotalOutfits() { return CompletableFuture.supplyAsync(() -> { try { - return countOutfitsExec(); + return countTotalOutfitsExec(); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public static CompletableFuture countDeletedOutfits() { + return CompletableFuture.supplyAsync(() -> { + try { + return countDeletedOutfitsExec(); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public static CompletableFuture countActiveOutfits() { + return CompletableFuture.supplyAsync(() -> { + try { + return countActiveOutfitsExec(); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public static CompletableFuture countFeaturedOutfits() { + return CompletableFuture.supplyAsync(() -> { + try { + return countFeaturedOutfitsExec(); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public static CompletableFuture countOutfits(boolean deleted, boolean featured) { + return CompletableFuture.supplyAsync(() -> { + try { + return countOutfitsExec(deleted, featured); } catch (SQLException e) { throw new CompletionException(e); } @@ -224,28 +264,40 @@ public class OutfitController { return extractMultiple(rs); } - private static int countOutfitsExec() throws SQLException { + private static int countTotalOutfitsExec() throws SQLException { + ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits"); + + return extractCount(rs); + } + + private static int countDeletedOutfitsExec() throws SQLException { + ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits WHERE deleted = 1"); + + return extractCount(rs); + } + + private static int countActiveOutfitsExec() throws SQLException { ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits WHERE deleted = 0"); - int count = 0; - if (rs.next()) { - count = rs.getInt("count"); - } + return extractCount(rs); + } - FSDB.get().close(rs); - return count; + private static int countFeaturedOutfitsExec() throws SQLException { + ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits WHERE featured = 1"); + + return extractCount(rs); + } + + private static int countOutfitsExec(boolean deleted, boolean featured) throws SQLException { + ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits WHERE deleted = ? AND featured = ?", deleted, featured); + + return extractCount(rs); } 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; - if (rs.next()) { - count = rs.getInt("count"); - } - - FSDB.get().close(rs); - return count; + return extractCount(rs); } private static Collection getDistinctTagsExec() throws SQLException { @@ -276,6 +328,16 @@ public class OutfitController { return Optional.of(outfits); } + private static int extractCount(ResultSet rs) throws SQLException { + int count = 0; + if (rs.next()) { + count = rs.getInt("count"); + } + + FSDB.get().close(rs); + return count; + } + private static Outfit mapObject(ResultSet rs) throws SQLException { return new Outfit.OutfitBuilder() .setId(rs.getString("id"))