From a8936d14715fbef003d23e4e0c80503b60fadcce Mon Sep 17 00:00:00 2001 From: Aleksei Date: Sat, 15 Feb 2020 00:46:55 -0500 Subject: [PATCH] Added more operations for outfits --- .../controllers/OutfitController.java | 80 +++++++++++++++++-- 1 file changed, 73 insertions(+), 7 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 9d2b28d..1e93497 100644 --- a/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java +++ b/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java @@ -14,30 +14,96 @@ import java.sql.SQLException; public class OutfitController { public static void insert(OutfitModel outfit) { try { - FSDB.get().insert( - "INSERT INTO outfits('id', 'link') VALUES (?, ?)", - outfit.id, outfit.link - ); + FSDB.get().insert("INSERT INTO outfits('id', 'link') VALUES (?, ?)", outfit.id, outfit.link); } catch (SQLException e) { e.printStackTrace(); } } public static OutfitModel findById(String id) throws SQLException { - OutfitModel outfit; + OutfitModel outfit = null; 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(); } + return outfit; + } - return null; + public static OutfitModel findRandom() throws SQLException { + OutfitModel outfit = null; + try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits ORDERBY random() LIMIT 1")) { + if (rs.next()) { + outfit = mapObject(rs); + } + rs.getStatement().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return outfit; + } + + public static OutfitModel findRandomByTag(String tag) { + OutfitModel outfit = null; + try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE tag = ? ORDERBY random() LIMIT 1", tag)) { + if (rs.next()) { + outfit = mapObject(rs); + } + rs.getStatement().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return outfit; + } + + public static OutfitModel findRandomBySubmitter(String submitterId) { + OutfitModel outfit = null; + try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE submitter = ? ORDERBY random() LIMIT 1", submitterId)) { + if (rs.next()) { + outfit = mapObject(rs); + } + rs.getStatement().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return outfit; + } + + public static int countOutfits() { + int count = 0; + try (ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits")) { + if (rs.next()) { + count = rs.getInt("count"); + } + rs.getStatement().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return count; + } + + public static int countOutfitsBySubmitter(String submitterId) { + int count = 0; + try (ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM outfits WHERE submitter = ?", submitterId)) { + if (rs.next()) { + count = rs.getInt("count"); + } + rs.getStatement().close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return count; } private static OutfitModel mapObject(ResultSet rs) throws SQLException { - return new OutfitModel.OutfitBuilder(rs.getString("link"), rs.getString("submitter"), rs.getString("tag")) + return new OutfitModel.OutfitBuilder( + rs.getString("link"), + rs.getString("submitter"), + rs.getString("tag") + ) .setId(rs.getString("id")) .setCreated(rs.getTimestamp("created")) .setUpdated(rs.getTimestamp("updated"))