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.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Time;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.CompletableFuture;
|
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(() -> {
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
try {
|
try {
|
||||||
return findRandomExec();
|
return findRandomExec();
|
||||||
@ -87,28 +88,28 @@ public class OutfitController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void insertExec(Outfit outfit) throws SQLException {
|
private static void insertExec(Outfit outfit) throws SQLException {
|
||||||
if (outfit.created == null) {
|
if (outfit.getCreated() == null) {
|
||||||
outfit.created = new Timestamp(System.currentTimeMillis());
|
outfit.setCreated(new Timestamp(System.currentTimeMillis()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (outfit.updated == null) {
|
if (outfit.getUpdated() == null) {
|
||||||
outfit.updated = new Timestamp(System.currentTimeMillis());
|
outfit.setUpdated(new Timestamp(System.currentTimeMillis()));
|
||||||
}
|
}
|
||||||
|
|
||||||
FSDB.get().insert(
|
FSDB.get().insert(
|
||||||
"INSERT INTO " +
|
"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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||||
outfit.id,
|
outfit.getId(),
|
||||||
outfit.link,
|
outfit.getLink(),
|
||||||
outfit.submitter,
|
outfit.getSubmitter(),
|
||||||
outfit.tag,
|
outfit.getTag(),
|
||||||
outfit.created,
|
outfit.getCreated(),
|
||||||
outfit.updated,
|
outfit.getUpdated(),
|
||||||
outfit.deleted,
|
outfit.isDeleted(),
|
||||||
outfit.featured,
|
outfit.isFeatured(),
|
||||||
outfit.displayCount,
|
outfit.getDisplayCount(),
|
||||||
outfit.deletionHash
|
outfit.getDeleteHash()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,16 +126,17 @@ public class OutfitController {
|
|||||||
return Optional.empty();
|
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");
|
ResultSet rs = FSDB.get().select("SELECT * FROM outfits WHERE deleted = 0 ORDER BY random() LIMIT 1");
|
||||||
|
|
||||||
Outfit outfit = new Outfit();
|
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
outfit = mapObject(rs);
|
Outfit outfit = mapObject(rs);
|
||||||
|
FSDB.get().close(rs);
|
||||||
|
return Optional.of(outfit);
|
||||||
}
|
}
|
||||||
FSDB.get().close(rs);
|
|
||||||
|
|
||||||
return outfit;
|
FSDB.get().close(rs);
|
||||||
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Optional<Outfit> findRandomByTagExec(String tag) throws SQLException {
|
private static Optional<Outfit> findRandomByTagExec(String tag) throws SQLException {
|
||||||
@ -188,17 +190,13 @@ public class OutfitController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Outfit mapObject(ResultSet rs) throws SQLException {
|
private static Outfit mapObject(ResultSet rs) throws SQLException {
|
||||||
Outfit outfit = new Outfit();
|
return new Outfit.OutfitBuilder(rs.getString("id"), rs.getString("link"), rs.getString("submitter"), rs.getString("tag"))
|
||||||
outfit.id = rs.getString("id");
|
.setCreated(new Timestamp(rs.getLong("created")))
|
||||||
outfit.link = rs.getString("link");
|
.setUpdated(new Timestamp((rs.getLong("updated"))))
|
||||||
outfit.tag = rs.getString("tag");
|
.setDeleted(rs.getBoolean("deleted"))
|
||||||
outfit.submitter = rs.getString("submitter");
|
.setFeatured(rs.getBoolean("featured"))
|
||||||
outfit.created = new Timestamp(rs.getLong("created"));
|
.setDisplayCount(rs.getInt("display_count"))
|
||||||
outfit.updated = new Timestamp(rs.getLong("updated"));
|
.setDeleteHash(rs.getString("delete_hash"))
|
||||||
outfit.deleted = rs.getBoolean("deleted");
|
.build();
|
||||||
outfit.featured = rs.getBoolean("featured");
|
|
||||||
outfit.displayCount = rs.getInt("display_count");
|
|
||||||
outfit.deletionHash = rs.getString("deletion_hash");
|
|
||||||
return outfit;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,19 +10,76 @@ import dev.salmonllama.fsbot.database.DatabaseModel;
|
|||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
public class Outfit extends DatabaseModel {
|
public class Outfit extends DatabaseModel {
|
||||||
public String id = "";
|
private String id = "";
|
||||||
public String link = "";
|
private String link = "";
|
||||||
public String submitter = "";
|
private String submitter = "";
|
||||||
public String tag = "";
|
private String tag = "";
|
||||||
public Timestamp created = null;
|
private Timestamp created = null;
|
||||||
public Timestamp updated = null;
|
private Timestamp updated = null;
|
||||||
public boolean deleted = false;
|
private boolean deleted = false;
|
||||||
public boolean featured = false;
|
private boolean featured = false;
|
||||||
public int displayCount = 0;
|
private int displayCount = 0;
|
||||||
public String deletionHash = "";
|
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() {
|
public static String schema() {
|
||||||
@ -36,13 +93,67 @@ public class Outfit extends DatabaseModel {
|
|||||||
"deleted TEXT," +
|
"deleted TEXT," +
|
||||||
"featured TEXT," +
|
"featured TEXT," +
|
||||||
"display_count INT," +
|
"display_count INT," +
|
||||||
"deletion_hash TEXT)";
|
"delete_hash TEXT)";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
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]",
|
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