From 9c8de32397d503ceb4ff08a96528816052fcfdd8 Mon Sep 17 00:00:00 2001 From: Aleksei Fox Date: Fri, 10 Jul 2020 18:27:31 -0400 Subject: [PATCH] Added ColorRole Model/Controller --- .../controllers/ColorRoleController.java | 171 ++++++++++++++++++ .../fsbot/database/models/ColorRole.java | 60 ++++++ 2 files changed, 231 insertions(+) create mode 100644 src/main/java/dev/salmonllama/fsbot/database/controllers/ColorRoleController.java create mode 100644 src/main/java/dev/salmonllama/fsbot/database/models/ColorRole.java diff --git a/src/main/java/dev/salmonllama/fsbot/database/controllers/ColorRoleController.java b/src/main/java/dev/salmonllama/fsbot/database/controllers/ColorRoleController.java new file mode 100644 index 0000000..896dd11 --- /dev/null +++ b/src/main/java/dev/salmonllama/fsbot/database/controllers/ColorRoleController.java @@ -0,0 +1,171 @@ +package dev.salmonllama.fsbot.database.controllers; + +import dev.salmonllama.fsbot.database.FSDB; +import dev.salmonllama.fsbot.database.models.ColorRole; + +import java.sql.ResultSet; +import java.sql.SQLException; +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) { + return CompletableFuture.runAsync(() -> { + try { + insertExec(cr); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public CompletableFuture exists(String color) { + return CompletableFuture.supplyAsync(() -> { + try { + return existsExec(color); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public CompletableFuture exists(long roleId) { + return CompletableFuture.supplyAsync(() -> { + try { + return existsExec(roleId); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public CompletableFuture> get(String color) { + return CompletableFuture.supplyAsync(() -> { + try { + return getExec(color); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public CompletableFuture> get(long roleId) { + return CompletableFuture.supplyAsync(() -> { + try { + return getExec(roleId); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public CompletableFuture count(long serverId) { + return CompletableFuture.supplyAsync(() -> { + try { + return countExec(serverId); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public CompletableFuture delete(long roleId) { + return CompletableFuture.runAsync(() -> { + try { + deleteExec(roleId); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public CompletableFuture delete(String color) { + return CompletableFuture.runAsync(() -> { + try { + deleteExec(color); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + private void insertExec(ColorRole cr) throws SQLException { + FSDB.get().insert("INSERT INTO color_roles (role_id, server_id, color) VALUES (?, ?, ?)", + cr.getRoleId(), + cr.getServerId(), + cr.getColor() + ); + } + + private 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"); + + FSDB.get().close(rs); + return exists; + } + + private 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"); + + FSDB.get().close(rs); + return exists; + } + + private Optional getExec(String color) throws SQLException { + ResultSet rs = FSDB.get().select("SELECT * FROM color_roles WHERE color = ?", color); + + if (rs.next()) { + ColorRole colorRole = mapObject(rs); + FSDB.get().close(rs); + return Optional.of(colorRole); + } + + FSDB.get().close(rs); + return Optional.empty(); + } + + private Optional getExec(Long roleId) throws SQLException { + ResultSet rs = FSDB.get().select("SELECT * FROM color_roles WHERE role_id = ?", roleId); + + if (rs.next()) { + ColorRole colorRole = mapObject(rs); + FSDB.get().close(rs); + return Optional.of(colorRole); + } + + FSDB.get().close(rs); + 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); + + if (rs.next()) { + int count = rs.getInt("count"); + FSDB.get().close(rs); + return count; + } + + FSDB.get().close(rs); + return 0; + } + + private void deleteExec(long roleId) throws SQLException { + FSDB.get().query("DELETE FROM color_roles WHERE role_id = ?", roleId); + } + + private void deleteExec(String color) throws SQLException { + FSDB.get().query("DELETE FROM color_roles WHERE color = ?", color); + } + + private ColorRole mapObject(ResultSet rs) throws SQLException { + return new ColorRole.ColorRoleBuilder(rs.getLong("role_id")) + .setServerId(rs.getLong("server_id")) + .setColor(rs.getString("color")) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/dev/salmonllama/fsbot/database/models/ColorRole.java b/src/main/java/dev/salmonllama/fsbot/database/models/ColorRole.java new file mode 100644 index 0000000..39ff347 --- /dev/null +++ b/src/main/java/dev/salmonllama/fsbot/database/models/ColorRole.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2020. Aleksei Gryczewski + * All rights reserved. + */ + +package dev.salmonllama.fsbot.database.models; + +import dev.salmonllama.fsbot.database.DatabaseModel; + +public class ColorRole extends DatabaseModel { + private long roleId; + private long serverId; + private String color; + + public ColorRole(ColorRoleBuilder builder) { + roleId = builder.roleId; + serverId = builder.serverId; + color = builder.color; + } + + public long getRoleId() { + return roleId; + } + + public long getServerId() { + return serverId; + } + + public String getColor() { + return color; + } + + public static String schema() { + return "CREATE TABLE IF NOT EXISTS color_roles (role_id INTEGER, server_id INTEGER, color TEXT)"; + } + + public static class ColorRoleBuilder { + private long roleId; + private long serverId; + private String color; + + public ColorRoleBuilder(long roleId) { + this.roleId = roleId; + } + + public ColorRoleBuilder setServerId(long serverId) { + this.serverId = serverId; + return this; + } + + public ColorRoleBuilder setColor(String color) { + this.color = color; + return this; + } + + public ColorRole build() { + return new ColorRole(this); + } + } +}