· 6 years ago · Jul 17, 2019, 06:58 PM
1/*
2 * Developed by **REDACTED** on 1/19/19 9:28 PM
3 * Last modified 10/25/18 7:08 PM
4 * Copyright (c) 2019. All rights reserved.
5 */
6
7package studio.spiderling.fsbot.utilities.database;
8
9import com.rethinkdb.RethinkDB;
10import com.rethinkdb.gen.ast.Table;
11import com.rethinkdb.net.Connection;
12import com.rethinkdb.net.Cursor;
13import org.javacord.api.DiscordApi;
14import studio.spiderling.fsbot.utilities.Outfit;
15import org.javacord.api.entity.server.Server;
16import studio.spiderling.fsbot.utilities.exceptions.OutfitNotFoundException;
17
18import java.util.*;
19
20public class DatabaseUtilities {
21
22 private final RethinkDB r;
23 private final Connection conn;
24 private final DiscordApi api;
25
26 public DatabaseUtilities(RethinkDB r, Connection conn, DiscordApi api) {
27 this.r = r;
28 this.conn = conn;
29 this.api = api;
30 }
31
32 private Table getTable(String table) {
33 if (this.r.db("fsbot").tableList().contains(table).run(this.conn)) {
34 return this.r.db("fsbot").table(table);
35 }
36 else {
37 return null;
38 }
39 }
40
41 private boolean validateId(String id) {
42 return r.db("fsbot").table("outfits").getField("id").contains(id).run(this.conn);
43 }
44
45 public boolean tagExists(String tag) {
46 return r.db("fsbot").table("outfits").getField("tag").distinct().contains(tag).run(this.conn);
47 }
48
49 public int countTags() {
50 return this.r.db("fsbot").table("outfits").getField("tag").distinct().count().run(this.conn);
51 }
52
53 public ArrayList<String> getTags() {
54 return r.db("fsbot").table("outfits").getField("tag").distinct().run(this.conn);
55 }
56
57 public Outfit getOutfitFromId(String id) throws OutfitNotFoundException {
58 if (!this.validateId(id)) throw new OutfitNotFoundException();
59
60 HashMap sample = this.r.db("fsbot").table("outfits")
61 .get(id)
62 .run(this.conn);
63
64 String tag = sample.get("tag").toString();
65 String link = sample.get("link").toString();
66 String submitterId = sample.get("submitter").toString();
67
68 String submitterName = api.getUserById(submitterId).join().getDiscriminatedName();
69
70 return new Outfit(id, tag, submitterName, link);
71 }
72
73 public String removeFromDatabase(String id) throws OutfitNotFoundException {
74 if (!this.validateId(id)) throw new OutfitNotFoundException();
75
76 // Remove outfit return deletion status. 0 = failed, >= 1 = success
77 HashMap deletion = r.db("fsbot").table("outfits").get(id).delete().run(conn);
78
79 return deletion.get("deleted").toString();
80 }
81
82 public Outfit randomOutfit() {
83 HashMap sample = this.r.db("fsbot").table("outfits")
84 .sample(1).nth(0)
85 .run(this.conn);
86
87 String id = sample.get("id").toString();
88 String tag = sample.get("tag").toString();
89 String link = sample.get("link").toString();
90 String submitterId = sample.get("submitter").toString();
91
92 String submitterName = api.getUserById(submitterId).join().getDiscriminatedName();
93
94 return new Outfit(id, tag, submitterName, link);
95 }
96
97 public Outfit randomTaggedOutfit(String targetTag) {
98 HashMap sample = r.db("fsbot").table("outfits")
99 .filter(row -> row.getField("tag").eq(targetTag))
100 .sample(1).nth(0)
101 .run(conn);
102
103 String id = String.valueOf(sample.get("id"));
104 String tag = String.valueOf(sample.get("tag"));
105 String link = String.valueOf(sample.get("link"));
106 String submitterId = String.valueOf(sample.get("submitter"));
107
108 String submitterName = api.getUserById(submitterId).join().getDiscriminatedName(); // Try a thenAccept with a thenApply
109
110 return new Outfit(id, tag, submitterName, link);
111 }
112
113 public String changeOutfitTag(String outfitId, String newTag) throws OutfitNotFoundException {
114 if (!this.validateId(outfitId)) throw new OutfitNotFoundException();
115
116 HashMap replacement = this.r.db("fsbot").table("outfits").get(outfitId).update(r.hashMap("tag", newTag)).run(this.conn);
117
118 return replacement.get("replaced").toString();
119 }
120
121 public Outfit getOutfitBySubmitter(String userId) {
122 HashMap sample = r.db("fsbot").table("outfits")
123 .filter(row -> row.getField("submitter").eq(userId))
124 .sample(1).nth(0)
125 .run(conn);
126
127 String id = String.valueOf(sample.get("id"));
128 String tag = String.valueOf(sample.get("tag"));
129 String link = String.valueOf(sample.get("link"));
130 String submitterId = String.valueOf(sample.get("submitter"));
131
132 String submitterName = api.getUserById(submitterId).join().getDiscriminatedName();
133
134 return new Outfit(id, tag, submitterName, link);
135 }
136
137 public long getSubmitterCount(String submitter) {
138 return r.db("fsbot").table("outfits")
139 .filter(
140 row -> row.getField("submitter").eq(submitter)
141 )
142 .count()
143 .run(conn);
144 }
145
146 public List<String> getSubmitterIds(String submitter) {
147 List<String> ids = new ArrayList<>();
148 Cursor<String> cursor = r.db("fsbot").table("outfits")
149 .filter(
150 row -> row.getField("submitter").eq(submitter)
151 )
152 .getField("id")
153 .run(conn);
154
155 cursor.forEach(ids::add);
156
157 return ids;
158 }
159
160 public void updateSubmitter(String submitter, String newSubmitter) {
161 // Add feature to return update-error?
162 r.db("fsbot").table("outfits")
163 .filter(r.hashMap("submitter", submitter))
164 .update(r.hashMap("submitter", newSubmitter))
165 .run(conn);
166 }
167
168 public void newServerProcess(Server server) {
169
170 if (this.r.db("fsbot").table("serverConf").contains(server.getIdAsString()).run(this.conn)) {
171 return;
172 }
173
174 String serverName = server.getName();
175 String serverId = server.getIdAsString();
176 String logChannel = "null";
177 String giveawayChannel = "null";
178 String welcomeChannel = "null";
179 String defaultWelcome = "welcome to the server";
180
181 this.r.db("fsbot").table("serverConf").insert(
182 this.r.hashMap("id", serverId)
183 .with("name", serverName)
184 .with("logChannel", logChannel)
185 .with("giveawayChannel", giveawayChannel)
186 .with("welcomeMsg", defaultWelcome)
187 .with("welcomeChannel", welcomeChannel)
188 .with("prefix", "~")
189 ).run(this.conn);
190 }
191
192 public void tableSetup() { // TODO: Fix this -- invert conditionals, just create the tables. -> if *not* exist then create
193 // Check for database existence, if not, create
194 if (r.dbList().contains("fsbot").run(conn)) {
195// System.out.println("database 'fsbot' already exists.");
196 }
197 else {
198 r.dbCreate("fsbot").run(conn);
199 System.out.println("database fsbot did not exist, and has been created");
200 }
201
202 // Check for channels table existence, if not, create
203 if (r.db("fsbot").tableList().contains("channels").run(conn)) {
204// System.out.println("table channels already exists");
205 }
206 else {
207 r.db("fsbot").tableCreate("channels").run(conn);
208 System.out.println("table channels did not exist, and has been created.");
209 }
210
211 // Check for serverconf table existence, if not, create
212 if (r.db("fsbot").tableList().contains("serverConf").run(conn)) {
213// System.out.println("table serverConf already exists");
214 }
215 else {
216 r.db("fsbot").tableCreate("serverConf").run(conn);
217 System.out.println("table serverConf did not exist, and has been created");
218 }
219
220 // Check for permissions table existene, if not, create
221 if (r.db("fsbot").tableList().contains("permissions").run(conn)) {
222// System.out.println("table permissions already exists");
223 }
224 else {
225 r.db("fsbot").tableCreate("permissions").run(conn);
226 System.out.println("table permissions did not exist and has been created");
227 }
228
229 // Check for outfits table existence, if not, create
230 if (r.db("fsbot").tableList().contains("outfits").run(conn)) {
231// System.out.println("table outfits already exists");
232 }
233 else {
234 r.db("fsbot").tableCreate("outfits").run(conn);
235 System.out.println("table outfits did not exist and has been created");
236 }
237
238 // Check for colourRoles table existence, if not, create
239 if (r.db("fsbot").tableList().contains("colourRoles").run(conn)) {
240// System.out.println("table colourRoles already exists");
241 }
242 else {
243 r.db("fsbot").tableCreate("colourRoles").run(conn);
244 System.out.println("table colourRoles did not exist and has been created");
245 }
246 }
247}