Database rework and Startup refactor#45
Database rework and Startup refactor#45LoadingBG wants to merge 3 commits intoJava-Discord:mainfrom LoadingBG:loadingbg
Conversation
There was a problem hiding this comment.
I appreciate the effort, but we should not change the database methods to be static. This makes testing individual parts of the application logic very difficult when we can no longer inject a mock of the database somewhere.
If you want to really refactor things, I would appreciate if you could figure out a way to remove the database configuration stuff entirely. There is no point in storing the config in a database when a normal JSON or YAML file will work just as well, especially when the config is read-only for 99.99% of the time.
| try (BufferedReader startupMsgReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("textfiles/startup.txt")))) { | ||
| String startup = startupMsgReader | ||
| .lines() | ||
| .collect(Collectors.joining("\n")) | ||
| .replace("{!version}", new Version().getVersion()); | ||
| System.out.println("\n" + startup); |
There was a problem hiding this comment.
If you're going to refactor this, you might as well do Files.readString(Path) instead of all that buffered reader stuff.
There was a problem hiding this comment.
What is the format of the config?
There was a problem hiding this comment.
It may have changed, but Dynxsty pasted this a while back. The config is all stored in JSON somewhere in a database, but there is not really a reason to store some simple JSON in a database when we can use GSON or Jackson to read from a file.
Also, I would suggest maybe being a bit patient with this change, because I've also been pushing him to upgrade to java 16 which adds records, which would be really handy for config.
{
"name": "Java",
"guild_id": "648956210850299986",
"welcome_system": {
"image": {
"avatar": {
"avX": 75,
"avY": 100,
"avH": 400,
"avW": 400
},
"imgW": 1920,
"imgH": 600,
"overlayURL": "https://cdn.discordapp.com/attachments/744899463591624815/827303132098461696/WelcomeOverlay_NoShadow.png",
"bgURL": "https://cdn.discordapp.com/attachments/744899463591624815/840928322661122048/WelcomeBG.png",
"primCol": "16777215",
"secCol": "16720173"
},
"join_msg": "Hey {!member}, welcome to **{!server}**!",
"leave_msg": "**{!membertag}**{!boticon} left the server.",
"welcome_cid": "650632933165760512",
"welcome_status": true
},
"channels": {
"report_cid": "760172343971807244",
"log_cid": "793486198584115270",
"suggestion_cid": "752535909228085348",
"submission_cid": "811277208223875112",
"jam_announcement_cid": "796057592405032990",
"jam_vote_cid": "796057702635274270"
},
"roles": {
"mute_rid": "736591398849085460",
"jam_ping_rid": "761320255082463263",
"jam_admin_rid": "735510411377508411"
},
"other": {
"stats_category": {
"stats_cid": "773622646876274728",
"stats_text": "Server · {!membercount} Members"
},
"qotw": {
"dm-qotw": true
},
"server_lock": {
"lock_status": false,
"lock_count": 0
},
"starboard": {
"starboard_cid": "866099127821402112",
"starboard_emote": "⭐",
"starboard_emote2": "🌟",
"starboard_emote3": "🌠"
}
}
}
Replace for loops with streams for readability (
For loops can get messy sometimes so I replaced them with streams for a bit more readability in exchange for speed. However,events/Startup.java)Startupruns only once so the speed doesn't really matter.Refactor the code a bit (
There were some questionable decisions on how to write the code, for example the pattern:other/Database.java)#getConfigString,#getConfigIntand#getConfigBooleanhad this weird line:if (guildDocExists(guild)) insertGuildDoc(guild);which I thought was nonsensical because why insert the guild doc if it already exists. I changed it to:
if (!guildDocExists(guild)) insertGuildDoc(guild);Make
All the code used randomother/DatabasestaticDatabaseinstances for one time and then destroyed them. SinceDatabasedoesn't hold any internal data, I made all the methods static and made the constructor private.