Map<Long, Vote> votes = Vote.findVotesUserByProposalId(user);
Map<Long, Pair<Double, Integer>> moyennes = Vote.caculMoyennes();
ArrayNode result = new ArrayNode(JsonNodeFactory.instance);
for (Proposal proposal : proposals) {
ObjectNode proposalJson = Json.newObject();
proposalJson.put("id", proposal.getId());
proposalJson.put("title", proposal.getTitle());
if (proposal.format != null) {
ObjectNode formatJson = Json.newObject();
formatJson.put("id", proposal.format.getId());
formatJson.put("dureeMinutes", proposal.format.getDureeMinutes());
formatJson.put("libelle", proposal.format.getLibelle());
proposalJson.put("format", formatJson);
} else {
proposalJson.putNull("format");
}
if (proposal.statusProposal != null) {
proposalJson.put("status", proposal.statusProposal.name());
} else {
proposalJson.putNull("status");
}
if (proposal.audience != null) {
proposalJson.put("audience", proposal.audience.getTitle());
} else {
proposalJson.putNull("audience");
}
if (proposal.getSpeaker() != null) {
ObjectNode speakerJson = Json.newObject();
speakerJson.put("id", proposal.getSpeaker().id);
speakerJson.put("fullname", proposal.getSpeaker().getFullname());
speakerJson.put("avatar", proposal.getSpeaker().getAvatar());
proposalJson.put("speaker", speakerJson);
}
Vote voteUser = votes.get(proposal.getId());
if (voteUser != null) {
proposalJson.put("vote", voteUser.getNote());
} else {
proposalJson.putNull("vote");
}
Pair<Double, Integer> moyenne = moyennes.get(proposal.getId());
if (moyenne != null) {
proposalJson.put("moyenne", moyenne.getLeft());
proposalJson.put("nbvote", moyenne.getRight());
} else {
proposalJson.putNull("moyenne");
}
result.add(proposalJson);
}
return ok(result);
}