From 5b5faa6b5fd7e763c0278a660fb626a7fb38972e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 24 Oct 2022 21:57:11 +0200 Subject: [PATCH] Implement format update command --- .../nounspicker/commands/NounsCommand.java | 3 ++ .../subcommands/NounsCreateSubCommand.java | 1 - .../subcommands/NounsUpdateSubCmd.java | 39 +++++++++++++++++++ .../nounspicker/utils/DatabaseManager.java | 17 ++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsUpdateSubCmd.java diff --git a/src/main/java/wtf/beatrice/nounspicker/commands/NounsCommand.java b/src/main/java/wtf/beatrice/nounspicker/commands/NounsCommand.java index 7ee5652..a8eed16 100644 --- a/src/main/java/wtf/beatrice/nounspicker/commands/NounsCommand.java +++ b/src/main/java/wtf/beatrice/nounspicker/commands/NounsCommand.java @@ -8,6 +8,7 @@ import wtf.beatrice.nounspicker.NounsPicker; import wtf.beatrice.nounspicker.commands.subcommands.NounsCreateSubCommand; import wtf.beatrice.nounspicker.commands.subcommands.NounsDeleteSubCommand; import wtf.beatrice.nounspicker.commands.subcommands.NounsSetSubCommand; +import wtf.beatrice.nounspicker.commands.subcommands.NounsUpdateSubCmd; public class NounsCommand implements CommandExecutor { @@ -39,6 +40,8 @@ public class NounsCommand implements CommandExecutor return NounsCreateSubCommand.run(sender, args); case "delete": return NounsDeleteSubCommand.run(sender, args); + case "update": + return NounsUpdateSubCmd.run(sender, args); } diff --git a/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsCreateSubCommand.java b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsCreateSubCommand.java index a0410d8..8066a36 100644 --- a/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsCreateSubCommand.java +++ b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsCreateSubCommand.java @@ -11,7 +11,6 @@ public class NounsCreateSubCommand @NotNull String[] args) { - if(args.length < 3) { sender.sendMessage("usage: /nouns create "); diff --git a/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsUpdateSubCmd.java b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsUpdateSubCmd.java new file mode 100644 index 0000000..5335900 --- /dev/null +++ b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsUpdateSubCmd.java @@ -0,0 +1,39 @@ +package wtf.beatrice.nounspicker.commands.subcommands; + +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; +import wtf.beatrice.nounspicker.utils.Cache; + +public class NounsUpdateSubCmd +{ + + + public static boolean run(@NotNull CommandSender sender, + @NotNull String[] args) { + + + if (args.length < 3) { + sender.sendMessage("usage: /nouns update "); + return true; + } + + String pronoun = args[1].toLowerCase(); + if(!Cache.dbManager.isPronounValid(pronoun)) { + sender.sendMessage("Invalid pronoun " + pronoun + "!"); + return true; + } + + int pronounId = Cache.dbManager.getPronounId(pronoun); + String oldFormat = Cache.dbManager.getPronounFormat(pronounId); + String newFormat = args[2]; + + if(Cache.dbManager.updatePronounFormat(pronounId, newFormat)) { + sender.sendMessage("Format of pronoun " + pronoun + " updated from " + oldFormat + " to " + newFormat + "!"); + return true; + } else { + sender.sendMessage("Error updating pronoun format! Please check console for details."); + return true; + } + + } +} diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java index 37da3f2..dd3d442 100644 --- a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java +++ b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java @@ -287,5 +287,22 @@ public class DatabaseManager return false; } + public boolean updatePronounFormat(int pronounId, String newFormat) { + + String query = "UPDATE pronouns SET format=? " + + "WHERE id=?;"; + + try(PreparedStatement pStatement = dbConnection.prepareStatement(query)) { + pStatement.setString(1, newFormat); + pStatement.setInt(2, pronounId); + pStatement.executeUpdate(); + return true; + } catch (SQLException e) { + e.printStackTrace(); + } + + return false; + } + }