Added more operations for outfits

This commit is contained in:
Aleksei 2020-02-15 00:46:55 -05:00
parent b2da583fa1
commit a8936d1471

View File

@ -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"))