mirror of
https://github.com/Qortal/qortal.git
synced 2025-05-04 16:57:51 +00:00
Added count to get votes API call
This commit is contained in:
parent
cda32a47f1
commit
49c0d45bc6
56
src/main/java/org/qortal/api/model/PollVotes.java
Normal file
56
src/main/java/org/qortal/api/model/PollVotes.java
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package org.qortal.api.model;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import javax.xml.bind.annotation.XmlAccessType;
|
||||||
|
import javax.xml.bind.annotation.XmlAccessorType;
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
|
||||||
|
import org.qortal.data.voting.VoteOnPollData;
|
||||||
|
|
||||||
|
@Schema(description = "Poll vote info, including voters")
|
||||||
|
// All properties to be converted to JSON via JAX-RS
|
||||||
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
public class PollVotes {
|
||||||
|
|
||||||
|
@Schema(description = "List of individual votes")
|
||||||
|
@XmlElement(name = "votes")
|
||||||
|
public List<VoteOnPollData> votes;
|
||||||
|
|
||||||
|
@Schema(description = "Total number of votes")
|
||||||
|
public Integer totalVotes;
|
||||||
|
|
||||||
|
@Schema(description = "List of vote counts for each option")
|
||||||
|
public List<OptionCount> voteCounts;
|
||||||
|
|
||||||
|
// For JAX-RS
|
||||||
|
protected PollVotes() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public PollVotes(List<VoteOnPollData> votes, Integer totalVotes, List<OptionCount> voteCounts) {
|
||||||
|
this.votes = votes;
|
||||||
|
this.totalVotes = totalVotes;
|
||||||
|
this.voteCounts = voteCounts;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Schema(description = "Vote info")
|
||||||
|
// All properties to be converted to JSON via JAX-RS
|
||||||
|
@XmlAccessorType(XmlAccessType.FIELD)
|
||||||
|
public static class OptionCount {
|
||||||
|
@Schema(description = "Option name")
|
||||||
|
public String optionName;
|
||||||
|
|
||||||
|
@Schema(description = "Vote count")
|
||||||
|
public Integer voteCount;
|
||||||
|
|
||||||
|
// For JAX-RS
|
||||||
|
protected OptionCount() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public OptionCount(String optionName, Integer voteCount) {
|
||||||
|
this.optionName = optionName;
|
||||||
|
this.voteCount = voteCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -31,12 +31,17 @@ import javax.ws.rs.core.MediaType;
|
|||||||
|
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
import io.swagger.v3.oas.annotations.media.ArraySchema;
|
import io.swagger.v3.oas.annotations.media.ArraySchema;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
import org.qortal.api.ApiException;
|
import org.qortal.api.ApiException;
|
||||||
|
import org.qortal.api.model.PollVotes;
|
||||||
import org.qortal.data.voting.PollData;
|
import org.qortal.data.voting.PollData;
|
||||||
|
import org.qortal.data.voting.PollOptionData;
|
||||||
import org.qortal.data.voting.VoteOnPollData;
|
import org.qortal.data.voting.VoteOnPollData;
|
||||||
|
|
||||||
@Path("/polls")
|
@Path("/polls")
|
||||||
@ -112,19 +117,41 @@ public class PollsResource {
|
|||||||
description = "poll votes",
|
description = "poll votes",
|
||||||
content = @Content(
|
content = @Content(
|
||||||
mediaType = MediaType.APPLICATION_JSON,
|
mediaType = MediaType.APPLICATION_JSON,
|
||||||
schema = @Schema(implementation = VoteOnPollData.class)
|
schema = @Schema(implementation = PollVotes.class)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ApiErrors({ApiError.REPOSITORY_ISSUE})
|
@ApiErrors({ApiError.REPOSITORY_ISSUE})
|
||||||
public List<VoteOnPollData> getVoteOnPollData(@PathParam("pollName") String pollName) {
|
public PollVotes getPollVotes(@PathParam("pollName") String pollName) {
|
||||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||||
if (repository.getVotingRepository().fromPollName(pollName) == null)
|
PollData pollData = repository.getVotingRepository().fromPollName(pollName);
|
||||||
|
if (pollData == null)
|
||||||
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.POLL_NO_EXISTS);
|
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.POLL_NO_EXISTS);
|
||||||
|
|
||||||
List<VoteOnPollData> voteOnPollData = repository.getVotingRepository().getVotes(pollName);
|
List<VoteOnPollData> votes = repository.getVotingRepository().getVotes(pollName);
|
||||||
return voteOnPollData;
|
|
||||||
|
// Initialize map for counting votes
|
||||||
|
Map<String, Integer> voteCountMap = new HashMap<>();
|
||||||
|
for (PollOptionData optionData : pollData.getPollOptions()) {
|
||||||
|
voteCountMap.put(optionData.getOptionName(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int totalVotes = 0;
|
||||||
|
for (VoteOnPollData vote : votes) {
|
||||||
|
String selectedOption = pollData.getPollOptions().get(vote.getOptionIndex()).getOptionName();
|
||||||
|
if (voteCountMap.containsKey(selectedOption)) {
|
||||||
|
voteCountMap.put(selectedOption, voteCountMap.get(selectedOption) + 1);
|
||||||
|
totalVotes++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert map to list of VoteInfo
|
||||||
|
List<PollVotes.OptionCount> voteCounts = voteCountMap.entrySet().stream()
|
||||||
|
.map(entry -> new PollVotes.OptionCount(entry.getKey(), entry.getValue()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
return new PollVotes(votes, totalVotes, voteCounts);
|
||||||
} catch (ApiException e) {
|
} catch (ApiException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (DataException e) {
|
} catch (DataException e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user