Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Notification when creating a poll #5

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.shweit.pollmaster.commands;

import com.shweit.pollmaster.utils.ConnectionManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
Expand All @@ -11,6 +12,7 @@

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -82,22 +84,29 @@ public boolean onCommand(final CommandSender sender, final Command command, fina
String optionsAsJsonString = convertListToJson(answers);

// Speichere das Poll in der Datenbank
int id = 0;
try {
savePollToDatabase(player.getUniqueId(), question, optionsAsJsonString, allowMultipleAnswers);
id = savePollToDatabase(player.getUniqueId(), question, optionsAsJsonString, allowMultipleAnswers);
player.sendMessage("Poll created successfully!");
} catch (SQLException e) {
player.sendMessage("An error occurred while saving the poll.");
e.printStackTrace();
}

int finalId = id;
Bukkit.getOnlinePlayers().forEach(p -> {
p.sendMessage(ChatColor.GREEN + "A new poll has been created by " + player.getName() + ".");
p.sendMessage(ChatColor.GREEN + "To view the poll, type /vote " + finalId);
});

return true;
}

private String convertListToJson(final List<String> list) {
return gson.toJson(list);
}

private void savePollToDatabase(final UUID uniqueId, final String question, final String answersAsJsonString, final boolean multi) throws SQLException {
private int savePollToDatabase(final UUID uniqueId, final String question, final String answersAsJsonString, final boolean multi) throws SQLException {
Connection connection = new ConnectionManager().getConnection();
String insertPollQuery = "INSERT INTO polls (uuid, question, answers, allowMultiple, isOpen) VALUES (?, ?, ?, ?, ?)";

Expand All @@ -109,6 +118,23 @@ private void savePollToDatabase(final UUID uniqueId, final String question, fina
preparedStatement.setBoolean(5, true);
preparedStatement.executeUpdate();
}

// Return the ID of the poll
String query = "SELECT id FROM polls WHERE uuid = ? AND question = ? AND answers = ? AND allowMultiple = ? AND isOpen = ?";
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, uniqueId.toString());
statement.setString(2, question);
statement.setString(3, answersAsJsonString);
statement.setBoolean(4, multi);
statement.setBoolean(5, true);
try (ResultSet results = statement.executeQuery()) {
if (results.next()) {
return results.getInt("id");
}
}
}

return 0;
}

@Override
Expand Down
Loading