Added user blacklisting
This commit is contained in:
parent
e5ec9c75b5
commit
73e41b6c97
@ -7,10 +7,7 @@
|
||||
package dev.salmonllama.fsbot.database;
|
||||
|
||||
import dev.salmonllama.fsbot.config.BotConfig;
|
||||
import dev.salmonllama.fsbot.database.models.GalleryChannel;
|
||||
import dev.salmonllama.fsbot.database.models.Outfit;
|
||||
import dev.salmonllama.fsbot.database.models.ServerBlacklist;
|
||||
import dev.salmonllama.fsbot.database.models.ServerConfig;
|
||||
import dev.salmonllama.fsbot.database.models.*;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
@ -45,6 +42,7 @@ public class FSDB {
|
||||
get().query(GalleryChannel.schema());
|
||||
get().query(ServerConfig.schema());
|
||||
get().query(ServerBlacklist.schema());
|
||||
get().query(UserBlacklist.schema());
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright (c) 2020. Aleksei Gryczewski
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
package dev.salmonllama.fsbot.database.controllers;
|
||||
|
||||
import dev.salmonllama.fsbot.database.FSDB;
|
||||
import dev.salmonllama.fsbot.database.models.ServerBlacklist;
|
||||
import dev.salmonllama.fsbot.database.models.UserBlacklist;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalDouble;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
|
||||
public class UserBlacklistController {
|
||||
// Need to create, read, and delete. No need for update?
|
||||
// Exists, insert, get, delete
|
||||
public static CompletableFuture<Void> insert(UserBlacklist bl) {
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
insertExec(bl);
|
||||
} catch (SQLException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static CompletableFuture<Boolean> exists(String id) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
return existsExec(id);
|
||||
} catch (SQLException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static CompletableFuture<Boolean> exists(UserBlacklist bl) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
return existsExec(bl.getId());
|
||||
} catch (SQLException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static CompletableFuture<Optional<UserBlacklist>> get(String id) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
return getExec(id);
|
||||
} catch (SQLException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public static CompletableFuture<Void> delete(String id) {
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
deleteExec(id);
|
||||
} catch (SQLException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static CompletableFuture<Void> delete(UserBlacklist bl) {
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
deleteExec(bl.getId());
|
||||
} catch (SQLException e) {
|
||||
throw new CompletionException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void insertExec(UserBlacklist bl) throws SQLException {
|
||||
FSDB.get().insert("INSERT INTO blacklist_users ('id', 'reason', 'added') VALUES (?, ?, ?)",
|
||||
bl.getId(),
|
||||
bl.getReason(),
|
||||
bl.getAdded()
|
||||
);
|
||||
}
|
||||
|
||||
private static boolean existsExec(String id) throws SQLException {
|
||||
ResultSet rs = FSDB.get().select("SELECT EXISTS(SELECT 1 FROM blacklist_users WHERE id = ?) AS hmm", id);
|
||||
boolean exists = rs.getBoolean("hmm");
|
||||
|
||||
FSDB.get().close(rs);
|
||||
return exists;
|
||||
}
|
||||
|
||||
private static Optional<UserBlacklist> getExec(String id) throws SQLException {
|
||||
ResultSet rs = FSDB.get().select("SELECT * FROM blacklist_users WHERE id = ?", id);
|
||||
|
||||
if (rs.next()) {
|
||||
UserBlacklist bl = mapObject(rs);
|
||||
FSDB.get().close(rs);
|
||||
return Optional.of(bl);
|
||||
}
|
||||
|
||||
FSDB.get().close(rs);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private static void deleteExec(String id) throws SQLException {
|
||||
FSDB.get().query("DELETE FROM blacklist_users WHERE id = ?", id);
|
||||
}
|
||||
|
||||
private static UserBlacklist mapObject(ResultSet rs) throws SQLException {
|
||||
return new UserBlacklist.UserBlacklistBuilder(rs.getString("id"))
|
||||
.setReason(rs.getString("reason"))
|
||||
.setAdded(new Timestamp(rs.getLong("added")))
|
||||
.build();
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2020. Aleksei Gryczewski
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
package dev.salmonllama.fsbot.database.models;
|
||||
|
||||
import dev.salmonllama.fsbot.database.DatabaseModel;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
public class UserBlacklist extends DatabaseModel {
|
||||
private String id;
|
||||
private String reason;
|
||||
private Timestamp added;
|
||||
|
||||
private UserBlacklist(UserBlacklistBuilder builder) {
|
||||
this.id = builder.id;
|
||||
this.reason = builder.reason;
|
||||
this.added = builder.added;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getReason() {
|
||||
return reason;
|
||||
}
|
||||
|
||||
public Timestamp getAdded() {
|
||||
return added;
|
||||
}
|
||||
|
||||
public static String schema() {
|
||||
return "CREATE TABLE IF NOT EXISTS blacklist_users(" +
|
||||
"id TEXT PRIMARY KEY," +
|
||||
"reason TEXT," +
|
||||
"added TEXT)";
|
||||
}
|
||||
|
||||
public static class UserBlacklistBuilder {
|
||||
private String id;
|
||||
private String reason;
|
||||
private Timestamp added;
|
||||
|
||||
public UserBlacklistBuilder(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public UserBlacklistBuilder setId(String id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserBlacklistBuilder setReason(String reason) {
|
||||
this.reason = reason;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserBlacklistBuilder setAdded(Timestamp added) {
|
||||
this.added = added;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserBlacklist build() {
|
||||
return new UserBlacklist(this);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user