From 43b1570b9dd3bd7f581f42bf509e9cf9642bb428 Mon Sep 17 00:00:00 2001 From: Salmonllama Date: Wed, 19 Aug 2020 15:14:25 -0400 Subject: [PATCH] Fixed accessors and added getAll method --- .../controllers/ColorRoleController.java | 53 ++++++++++--------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/src/main/java/dev/salmonllama/fsbot/database/controllers/ColorRoleController.java b/src/main/java/dev/salmonllama/fsbot/database/controllers/ColorRoleController.java index 896dd11..e249342 100644 --- a/src/main/java/dev/salmonllama/fsbot/database/controllers/ColorRoleController.java +++ b/src/main/java/dev/salmonllama/fsbot/database/controllers/ColorRoleController.java @@ -5,13 +5,15 @@ import dev.salmonllama.fsbot.database.models.ColorRole; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; public class ColorRoleController { // Need insert, get by color, exists by color, exists by role, get by server, count, and delete - public CompletableFuture insert(ColorRole cr) { + public static CompletableFuture insert(ColorRole cr) { return CompletableFuture.runAsync(() -> { try { insertExec(cr); @@ -21,7 +23,7 @@ public class ColorRoleController { }); } - public CompletableFuture exists(String color) { + public static CompletableFuture exists(String color) { return CompletableFuture.supplyAsync(() -> { try { return existsExec(color); @@ -31,7 +33,7 @@ public class ColorRoleController { }); } - public CompletableFuture exists(long roleId) { + public static CompletableFuture exists(long roleId) { return CompletableFuture.supplyAsync(() -> { try { return existsExec(roleId); @@ -41,7 +43,7 @@ public class ColorRoleController { }); } - public CompletableFuture> get(String color) { + public static CompletableFuture> get(String color) { return CompletableFuture.supplyAsync(() -> { try { return getExec(color); @@ -51,7 +53,7 @@ public class ColorRoleController { }); } - public CompletableFuture> get(long roleId) { + public static CompletableFuture> get(long roleId) { return CompletableFuture.supplyAsync(() -> { try { return getExec(roleId); @@ -61,17 +63,17 @@ public class ColorRoleController { }); } - public CompletableFuture count(long serverId) { + public static CompletableFuture>> getAll() { return CompletableFuture.supplyAsync(() -> { try { - return countExec(serverId); + return getAllExec(); } catch (SQLException e) { throw new CompletionException(e); } }); } - public CompletableFuture delete(long roleId) { + public static CompletableFuture delete(long roleId) { return CompletableFuture.runAsync(() -> { try { deleteExec(roleId); @@ -81,7 +83,7 @@ public class ColorRoleController { }); } - public CompletableFuture delete(String color) { + public static CompletableFuture delete(String color) { return CompletableFuture.runAsync(() -> { try { deleteExec(color); @@ -91,7 +93,7 @@ public class ColorRoleController { }); } - private void insertExec(ColorRole cr) throws SQLException { + private static void insertExec(ColorRole cr) throws SQLException { FSDB.get().insert("INSERT INTO color_roles (role_id, server_id, color) VALUES (?, ?, ?)", cr.getRoleId(), cr.getServerId(), @@ -99,7 +101,7 @@ public class ColorRoleController { ); } - private boolean existsExec(String color) throws SQLException { + private static boolean existsExec(String color) throws SQLException { ResultSet rs = FSDB.get().select("SELECT EXISTS(SELECT 1 FROM color_roles WHERE color = ?) AS hmm", color); boolean exists = rs.getBoolean("hmm"); @@ -107,7 +109,7 @@ public class ColorRoleController { return exists; } - private boolean existsExec(long roleId) throws SQLException { + private static boolean existsExec(long roleId) throws SQLException { ResultSet rs = FSDB.get().select("SELECT EXISTS(SELECT 1 FROM color_roles WHERE role_id = ?) AS hmm", roleId); boolean exists = rs.getBoolean("hmm"); @@ -115,7 +117,7 @@ public class ColorRoleController { return exists; } - private Optional getExec(String color) throws SQLException { + private static Optional getExec(String color) throws SQLException { ResultSet rs = FSDB.get().select("SELECT * FROM color_roles WHERE color = ?", color); if (rs.next()) { @@ -128,7 +130,7 @@ public class ColorRoleController { return Optional.empty(); } - private Optional getExec(Long roleId) throws SQLException { + private static Optional getExec(Long roleId) throws SQLException { ResultSet rs = FSDB.get().select("SELECT * FROM color_roles WHERE role_id = ?", roleId); if (rs.next()) { @@ -141,28 +143,31 @@ public class ColorRoleController { return Optional.empty(); } - private int countExec(long serverId) throws SQLException { - ResultSet rs = FSDB.get().select("SELECT COUNT(*) AS count FROM color_roles WHERE server_id = ?", serverId); + private static Optional> getAllExec() throws SQLException { + ResultSet rs = FSDB.get().select("SELECT * FROM color_roles"); - if (rs.next()) { - int count = rs.getInt("count"); + Collection roles = new ArrayList<>(); + while (rs.next()) { + roles.add(mapObject(rs)); FSDB.get().close(rs); - return count; } - FSDB.get().close(rs); - return 0; + if (roles.isEmpty()) { + return Optional.empty(); + } + + return Optional.of(roles); } - private void deleteExec(long roleId) throws SQLException { + private static void deleteExec(long roleId) throws SQLException { FSDB.get().query("DELETE FROM color_roles WHERE role_id = ?", roleId); } - private void deleteExec(String color) throws SQLException { + private static void deleteExec(String color) throws SQLException { FSDB.get().query("DELETE FROM color_roles WHERE color = ?", color); } - private ColorRole mapObject(ResultSet rs) throws SQLException { + private static ColorRole mapObject(ResultSet rs) throws SQLException { return new ColorRole.ColorRoleBuilder(rs.getLong("role_id")) .setServerId(rs.getLong("server_id")) .setColor(rs.getString("color"))