From ef466013e0fdffa101bac46850a33f338f76eb1e Mon Sep 17 00:00:00 2001 From: Aleksei Date: Sat, 11 Apr 2020 19:31:52 -0400 Subject: [PATCH] ServerBlacklist model/controller --- .../ServerBlacklistController.java | 121 ++++++++++++++++++ .../database/models/ServerBlacklist.java | 78 +++++++++++ 2 files changed, 199 insertions(+) create mode 100644 src/main/java/dev/salmonllama/fsbot/database/controllers/ServerBlacklistController.java create mode 100644 src/main/java/dev/salmonllama/fsbot/database/models/ServerBlacklist.java diff --git a/src/main/java/dev/salmonllama/fsbot/database/controllers/ServerBlacklistController.java b/src/main/java/dev/salmonllama/fsbot/database/controllers/ServerBlacklistController.java new file mode 100644 index 0000000..3d3068c --- /dev/null +++ b/src/main/java/dev/salmonllama/fsbot/database/controllers/ServerBlacklistController.java @@ -0,0 +1,121 @@ +/* + * 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 java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; + +public class ServerBlacklistController { + // Need to create, read, and delete. No need for update? + public static CompletableFuture insert(ServerBlacklist bl) { + return CompletableFuture.runAsync(() -> { + try { + insertExec(bl); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public static CompletableFuture exists(String id) { + return CompletableFuture.supplyAsync(() -> { + try { + return existsExec(id); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public static CompletableFuture exists(ServerBlacklist bl) { + return CompletableFuture.supplyAsync(() -> { + try { + return existsExec(bl.getId()); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public static CompletableFuture> get(String id) { + return CompletableFuture.supplyAsync(() -> { + try { + return getExec(id); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public static CompletableFuture delete(String id) { + return CompletableFuture.runAsync(() -> { + try { + deleteExec(id); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + public static CompletableFuture delete(ServerBlacklist bl) { + return CompletableFuture.runAsync(() -> { + try { + deleteExec(bl.getId()); + } catch (SQLException e) { + throw new CompletionException(e); + } + }); + } + + private static void insertExec(ServerBlacklist bl) throws SQLException { + FSDB.get().insert("INSERT INTO blacklist_servers ('id', 'name', 'owner_id', 'added') VALUES (?, ?, ?, ?)", + bl.getId(), + bl.getName(), + bl.getOwnerId(), + bl.getAdded() + ); + } + + private static boolean existsExec(String id) throws SQLException { + ResultSet rs = FSDB.get().select("SELECT EXISTS(SELECT 1 FROM blacklist_servers WHERE id = ?) AS hmm", id); + boolean exists = rs.getBoolean("hmm"); + + FSDB.get().close(rs); + return exists; + } + + private static Optional getExec(String id) throws SQLException { + ResultSet rs = FSDB.get().select("SELECT * FROM blacklist_servers WHERE id = ?", id); + + if (rs.next()) { + ServerBlacklist 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_servers WHERE id = ?", id); + } + + private static ServerBlacklist mapObject(ResultSet rs) throws SQLException { + return new ServerBlacklist.ServerBlacklistBuilder(rs.getString("id")) + .setName(rs.getString("name")) + .setOwnerId(rs.getString("owner_id")) + .setAdded(new Timestamp( rs.getLong("added"))) + .build(); + } +} diff --git a/src/main/java/dev/salmonllama/fsbot/database/models/ServerBlacklist.java b/src/main/java/dev/salmonllama/fsbot/database/models/ServerBlacklist.java new file mode 100644 index 0000000..4450daf --- /dev/null +++ b/src/main/java/dev/salmonllama/fsbot/database/models/ServerBlacklist.java @@ -0,0 +1,78 @@ +/* + * 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 ServerBlacklist extends DatabaseModel { + private String id; + private String name; + private String ownerId; + private Timestamp added; + + private ServerBlacklist(ServerBlacklistBuilder builder) { + this.id = builder.id; + this.name = builder.name; + this.ownerId = builder.ownerId; + this.added = builder.added; + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public String getOwnerId() { + return ownerId; + } + + public Timestamp getAdded() { + return added; + } + + public static String schema() { + return "CREATE TABLE IF NOT EXISTS blacklist_servers (" + + "id TEXT PRIMARY KEY," + + "name TEXT," + + "owner_id TEXT," + + "added TEXT)"; + } + + public static class ServerBlacklistBuilder { + private String id; + private String name; + private String ownerId; + private Timestamp added; + + public ServerBlacklistBuilder(String id) { + this.id = id; + } + + public ServerBlacklistBuilder setName(String name) { + this.name = name; + return this; + } + + public ServerBlacklistBuilder setOwnerId(String ownerId) { + this.ownerId = ownerId; + return this; + } + + public ServerBlacklistBuilder setAdded(Timestamp added) { + this.added = added; + return this; + } + + public ServerBlacklist build() { + return new ServerBlacklist(this); + } + } +}