From a7f0556db955681c38e62c5a7865060d10ea3b20 Mon Sep 17 00:00:00 2001 From: Aleksei Date: Sat, 15 Feb 2020 01:40:38 -0500 Subject: [PATCH] Changed mapping paradigm --- .../controllers/OutfitController.java | 33 +++++---- .../fsbot/database/models/OutfitModel.java | 69 +------------------ 2 files changed, 18 insertions(+), 84 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 1e93497..a318c73 100644 --- a/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java +++ b/src/main/java/dev/salmonllama/fsbot/database/controllers/OutfitController.java @@ -21,7 +21,7 @@ public class OutfitController { } public static OutfitModel findById(String id) throws SQLException { - OutfitModel outfit = null; + OutfitModel outfit = new OutfitModel(); try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE id = ?", id)) { if (rs.next()) { outfit = mapObject(rs); @@ -34,7 +34,7 @@ public class OutfitController { } public static OutfitModel findRandom() throws SQLException { - OutfitModel outfit = null; + OutfitModel outfit = new OutfitModel(); try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits ORDERBY random() LIMIT 1")) { if (rs.next()) { outfit = mapObject(rs); @@ -47,7 +47,7 @@ public class OutfitController { } public static OutfitModel findRandomByTag(String tag) { - OutfitModel outfit = null; + OutfitModel outfit = new OutfitModel(); try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE tag = ? ORDERBY random() LIMIT 1", tag)) { if (rs.next()) { outfit = mapObject(rs); @@ -60,7 +60,7 @@ public class OutfitController { } public static OutfitModel findRandomBySubmitter(String submitterId) { - OutfitModel outfit = null; + OutfitModel outfit = new OutfitModel(); try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE submitter = ? ORDERBY random() LIMIT 1", submitterId)) { if (rs.next()) { outfit = mapObject(rs); @@ -99,18 +99,17 @@ public class OutfitController { } private static OutfitModel mapObject(ResultSet rs) throws SQLException { - 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")) - .setDeleted(rs.getBoolean("deleted")) - .setFeatured(rs.getBoolean("featured")) - .setDisplayCount(rs.getInt("display_count")) - .setDeletionHash(rs.getString("deletion_hash")) - .build(); + OutfitModel outfit = new OutfitModel(); + outfit.id = rs.getString("id"); + outfit.link = rs.getString("link"); + outfit.tag = rs.getString("tag"); + outfit.submitter = rs.getString("submitter"); + outfit.created = rs.getTimestamp("created"); + outfit.updated = rs.getTimestamp("updated"); + outfit.deleted = rs.getBoolean("deleted"); + outfit.featured = rs.getBoolean("featured"); + outfit.displayCount = rs.getInt("display_count"); + outfit.deletionHash = rs.getString("deletion_hash"); + return outfit; } } diff --git a/src/main/java/dev/salmonllama/fsbot/database/models/OutfitModel.java b/src/main/java/dev/salmonllama/fsbot/database/models/OutfitModel.java index 08bd88d..7fddeb1 100644 --- a/src/main/java/dev/salmonllama/fsbot/database/models/OutfitModel.java +++ b/src/main/java/dev/salmonllama/fsbot/database/models/OutfitModel.java @@ -21,15 +21,8 @@ public class OutfitModel extends DatabaseModel { public int displayCount = 0; public String deletionHash = ""; - private OutfitModel(OutfitBuilder builder) { - id = builder.id; - link = builder.link; - submitter = builder.submitter; - tag = builder.tag; - created = builder.created; - updated = builder.updated; - deleted = builder.deleted; - deletionHash = builder.deletionHash; + public OutfitModel() { + } public static String schema() { @@ -45,62 +38,4 @@ public class OutfitModel extends DatabaseModel { "display_count INT" + "deletion_hash TEXT)"; } - - public static class OutfitBuilder { - public String id = ""; - public final String link; - public final String submitter; - public String tag; - public Timestamp created = null; - public Timestamp updated = null; - public boolean deleted = false; - public boolean featured = false; - public int displayCount = 0; - public String deletionHash = ""; - - public OutfitBuilder(String link, String submitter, String tag) { - this.link = link; - this.submitter = submitter; - this.tag = tag; - } - - public OutfitBuilder setId(String id) { - this.id = id; - return this; - } - - public OutfitBuilder setCreated(Timestamp created) { - this.created = created; - return this; - } - - public OutfitBuilder setUpdated(Timestamp updated) { - this.updated = updated; - return this; - } - - public OutfitBuilder setDeleted(boolean deleted) { - this.deleted = deleted; - return this; - } - - public OutfitBuilder setFeatured(boolean featured) { - this.featured = featured; - return this; - } - - public OutfitBuilder setDisplayCount(int displayCount) { - this.displayCount = displayCount; - return this; - } - - public OutfitBuilder setDeletionHash(String hash) { - this.deletionHash = hash; - return this; - } - - public OutfitModel build() { - return new OutfitModel(this); - } - } }