Changed mapping paradigm

This commit is contained in:
Aleksei 2020-02-15 01:40:38 -05:00
parent a8936d1471
commit a7f0556db9
2 changed files with 18 additions and 84 deletions

View File

@ -21,7 +21,7 @@ public class OutfitController {
} }
public static OutfitModel findById(String id) throws SQLException { 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)) { try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE id = ?", id)) {
if (rs.next()) { if (rs.next()) {
outfit = mapObject(rs); outfit = mapObject(rs);
@ -34,7 +34,7 @@ public class OutfitController {
} }
public static OutfitModel findRandom() throws SQLException { 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")) { try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits ORDERBY random() LIMIT 1")) {
if (rs.next()) { if (rs.next()) {
outfit = mapObject(rs); outfit = mapObject(rs);
@ -47,7 +47,7 @@ public class OutfitController {
} }
public static OutfitModel findRandomByTag(String tag) { 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)) { try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE tag = ? ORDERBY random() LIMIT 1", tag)) {
if (rs.next()) { if (rs.next()) {
outfit = mapObject(rs); outfit = mapObject(rs);
@ -60,7 +60,7 @@ public class OutfitController {
} }
public static OutfitModel findRandomBySubmitter(String submitterId) { 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)) { try (ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE submitter = ? ORDERBY random() LIMIT 1", submitterId)) {
if (rs.next()) { if (rs.next()) {
outfit = mapObject(rs); outfit = mapObject(rs);
@ -99,18 +99,17 @@ public class OutfitController {
} }
private static OutfitModel mapObject(ResultSet rs) throws SQLException { private static OutfitModel mapObject(ResultSet rs) throws SQLException {
return new OutfitModel.OutfitBuilder( OutfitModel outfit = new OutfitModel();
rs.getString("link"), outfit.id = rs.getString("id");
rs.getString("submitter"), outfit.link = rs.getString("link");
rs.getString("tag") outfit.tag = rs.getString("tag");
) outfit.submitter = rs.getString("submitter");
.setId(rs.getString("id")) outfit.created = rs.getTimestamp("created");
.setCreated(rs.getTimestamp("created")) outfit.updated = rs.getTimestamp("updated");
.setUpdated(rs.getTimestamp("updated")) outfit.deleted = rs.getBoolean("deleted");
.setDeleted(rs.getBoolean("deleted")) outfit.featured = rs.getBoolean("featured");
.setFeatured(rs.getBoolean("featured")) outfit.displayCount = rs.getInt("display_count");
.setDisplayCount(rs.getInt("display_count")) outfit.deletionHash = rs.getString("deletion_hash");
.setDeletionHash(rs.getString("deletion_hash")) return outfit;
.build();
} }
} }

View File

@ -21,15 +21,8 @@ public class OutfitModel extends DatabaseModel {
public int displayCount = 0; public int displayCount = 0;
public String deletionHash = ""; public String deletionHash = "";
private OutfitModel(OutfitBuilder builder) { public OutfitModel() {
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 static String schema() { public static String schema() {
@ -45,62 +38,4 @@ public class OutfitModel extends DatabaseModel {
"display_count INT" + "display_count INT" +
"deletion_hash TEXT)"; "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);
}
}
} }