Added ColorRole Model/Controller
This commit is contained in:
parent
1a7b6c6ada
commit
9c8de32397
@ -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<Void> insert(ColorRole cr) {
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
insertExec(cr);
|
||||
} catch (SQLException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Boolean> exists(String color) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
return existsExec(color);
|
||||
} catch (SQLException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Boolean> exists(long roleId) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
return existsExec(roleId);
|
||||
} catch (SQLException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Optional<ColorRole>> get(String color) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
return getExec(color);
|
||||
} catch (SQLException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Optional<ColorRole>> get(long roleId) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
return getExec(roleId);
|
||||
} catch (SQLException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Integer> count(long serverId) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
return countExec(serverId);
|
||||
} catch (SQLException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> delete(long roleId) {
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
deleteExec(roleId);
|
||||
} catch (SQLException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> 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<ColorRole> 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<ColorRole> 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();
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user