Refactored for proper builder design
This commit is contained in:
parent
e2c6bbbc74
commit
757e02cfeb
@ -10,6 +10,7 @@ import dev.salmonllama.fsbot.database.models.Outfit;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Time;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@ -36,7 +37,7 @@ public class OutfitController {
|
||||
});
|
||||
}
|
||||
|
||||
public static CompletableFuture<Outfit> findRandom() {
|
||||
public static CompletableFuture<Optional<Outfit>> findRandom() {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
return findRandomExec();
|
||||
@ -87,28 +88,28 @@ public class OutfitController {
|
||||
}
|
||||
|
||||
private static void insertExec(Outfit outfit) throws SQLException {
|
||||
if (outfit.created == null) {
|
||||
outfit.created = new Timestamp(System.currentTimeMillis());
|
||||
if (outfit.getCreated() == null) {
|
||||
outfit.setCreated(new Timestamp(System.currentTimeMillis()));
|
||||
}
|
||||
|
||||
if (outfit.updated == null) {
|
||||
outfit.updated = new Timestamp(System.currentTimeMillis());
|
||||
if (outfit.getUpdated() == null) {
|
||||
outfit.setUpdated(new Timestamp(System.currentTimeMillis()));
|
||||
}
|
||||
|
||||
FSDB.get().insert(
|
||||
"INSERT INTO " +
|
||||
"outfits('id', 'link', 'submitter', 'tag', 'created', 'updated', 'deleted', 'featured', 'display_count', 'deletion_hash') " +
|
||||
"outfits('id', 'link', 'submitter', 'tag', 'created', 'updated', 'deleted', 'featured', 'display_count', 'delete_hash') " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
outfit.id,
|
||||
outfit.link,
|
||||
outfit.submitter,
|
||||
outfit.tag,
|
||||
outfit.created,
|
||||
outfit.updated,
|
||||
outfit.deleted,
|
||||
outfit.featured,
|
||||
outfit.displayCount,
|
||||
outfit.deletionHash
|
||||
outfit.getId(),
|
||||
outfit.getLink(),
|
||||
outfit.getSubmitter(),
|
||||
outfit.getTag(),
|
||||
outfit.getCreated(),
|
||||
outfit.getUpdated(),
|
||||
outfit.isDeleted(),
|
||||
outfit.isFeatured(),
|
||||
outfit.getDisplayCount(),
|
||||
outfit.getDeleteHash()
|
||||
);
|
||||
}
|
||||
|
||||
@ -125,16 +126,17 @@ public class OutfitController {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private static Outfit findRandomExec() throws SQLException {
|
||||
private static Optional<Outfit> findRandomExec() throws SQLException {
|
||||
ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE deleted = 0 ORDER BY random() LIMIT 1");
|
||||
|
||||
Outfit outfit = new Outfit();
|
||||
if (rs.next()) {
|
||||
outfit = mapObject(rs);
|
||||
}
|
||||
Outfit outfit = mapObject(rs);
|
||||
FSDB.get().close(rs);
|
||||
return Optional.of(outfit);
|
||||
}
|
||||
|
||||
return outfit;
|
||||
FSDB.get().close(rs);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private static Optional<Outfit> findRandomByTagExec(String tag) throws SQLException {
|
||||
@ -188,17 +190,13 @@ public class OutfitController {
|
||||
}
|
||||
|
||||
private static Outfit mapObject(ResultSet rs) throws SQLException {
|
||||
Outfit outfit = new Outfit();
|
||||
outfit.id = rs.getString("id");
|
||||
outfit.link = rs.getString("link");
|
||||
outfit.tag = rs.getString("tag");
|
||||
outfit.submitter = rs.getString("submitter");
|
||||
outfit.created = new Timestamp(rs.getLong("created"));
|
||||
outfit.updated = new Timestamp(rs.getLong("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;
|
||||
return new Outfit.OutfitBuilder(rs.getString("id"), rs.getString("link"), rs.getString("submitter"), rs.getString("tag"))
|
||||
.setCreated(new Timestamp(rs.getLong("created")))
|
||||
.setUpdated(new Timestamp((rs.getLong("updated"))))
|
||||
.setDeleted(rs.getBoolean("deleted"))
|
||||
.setFeatured(rs.getBoolean("featured"))
|
||||
.setDisplayCount(rs.getInt("display_count"))
|
||||
.setDeleteHash(rs.getString("delete_hash"))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
@ -10,19 +10,76 @@ import dev.salmonllama.fsbot.database.DatabaseModel;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
public class Outfit extends DatabaseModel {
|
||||
public String id = "";
|
||||
public String link = "";
|
||||
public 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 = "";
|
||||
private String id = "";
|
||||
private String link = "";
|
||||
private String submitter = "";
|
||||
private String tag = "";
|
||||
private Timestamp created = null;
|
||||
private Timestamp updated = null;
|
||||
private boolean deleted = false;
|
||||
private boolean featured = false;
|
||||
private int displayCount = 0;
|
||||
private String deleteHash = "";
|
||||
|
||||
public Outfit() {
|
||||
public Outfit(OutfitBuilder builder) {
|
||||
id = builder.id;
|
||||
link = builder.link;
|
||||
submitter = builder.submitter;
|
||||
tag = builder.tag;
|
||||
created = builder.created;
|
||||
updated = builder.updated;
|
||||
deleted = builder.deleted;
|
||||
featured = builder.featured;
|
||||
displayCount = builder.displayCount;
|
||||
deleteHash = builder.deleteHash;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getLink() {
|
||||
return link;
|
||||
}
|
||||
|
||||
public String getSubmitter() {
|
||||
return submitter;
|
||||
}
|
||||
|
||||
public String getTag() {
|
||||
return tag;
|
||||
}
|
||||
|
||||
public Timestamp getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
public void setCreated(Timestamp c) {
|
||||
this.created = c;
|
||||
}
|
||||
|
||||
public Timestamp getUpdated() {
|
||||
return updated;
|
||||
}
|
||||
|
||||
public void setUpdated(Timestamp u) {
|
||||
this.updated = u;
|
||||
}
|
||||
|
||||
public boolean isDeleted() {
|
||||
return deleted;
|
||||
}
|
||||
|
||||
public boolean isFeatured() {
|
||||
return featured;
|
||||
}
|
||||
|
||||
public int getDisplayCount() {
|
||||
return displayCount;
|
||||
}
|
||||
|
||||
public String getDeleteHash() {
|
||||
return deleteHash;
|
||||
}
|
||||
|
||||
public static String schema() {
|
||||
@ -36,13 +93,67 @@ public class Outfit extends DatabaseModel {
|
||||
"deleted TEXT," +
|
||||
"featured TEXT," +
|
||||
"display_count INT," +
|
||||
"deletion_hash TEXT)";
|
||||
"delete_hash TEXT)";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Outfit: [id: %s, link: %s, submitter: %s, tag: %s, created: %s, updated: %s, deleted: %s, featured: %s, display count: %s, deletion hash: %s]",
|
||||
id, link, submitter, tag, created, updated, deleted, featured, displayCount, deletionHash
|
||||
id, link, submitter, tag, created, updated, deleted, featured, displayCount, deleteHash
|
||||
);
|
||||
}
|
||||
|
||||
public static class OutfitBuilder {
|
||||
private String id;
|
||||
private String link;
|
||||
private String submitter;
|
||||
private String tag;
|
||||
private Timestamp created = null;
|
||||
private Timestamp updated = null;
|
||||
private boolean deleted = false;
|
||||
private boolean featured = false;
|
||||
private int displayCount = 0;
|
||||
private String deleteHash = "";
|
||||
|
||||
public OutfitBuilder(String id, String link, String submitter, String tag) {
|
||||
this.id = id;
|
||||
this.link = link;
|
||||
this.submitter = submitter;
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
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 setDeleteHash(String deletionHash) {
|
||||
this.deleteHash = deletionHash;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Outfit build() {
|
||||
return new Outfit(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user