Skip to content

Commit

Permalink
fixed error reported by checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed Feb 24, 2019
1 parent 651c30f commit 5d1deaa
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions src/main/java/com/erudika/scoold/controllers/VoteController.java
Expand Up @@ -46,7 +46,6 @@
import java.util.List;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -87,10 +86,7 @@ public Boolean votedown(@PathVariable String type, @PathVariable String id, Http
}

boolean processVoteRequest(boolean isUpvote, String type, String id, HttpServletRequest req) {
if (StringUtils.isBlank(id) || StringUtils.isBlank(type)) {
return false;
}
ParaObject votable = pc.read(id);
ParaObject votable = pc.read(type, id);
Profile author = null;
Profile authUser = utils.getAuthUser(req);
boolean result = false;
Expand All @@ -106,38 +102,19 @@ boolean processVoteRequest(boolean isUpvote, String type, String id, HttpServlet

author = (Profile) voteObjects.stream().filter((p) -> p instanceof Profile).findFirst().orElse(null);
Integer votes = votable.getVotes() != null ? votable.getVotes() : 0;

boolean upvoteExists = voteObjects.stream().anyMatch((v) -> v instanceof Vote && ((Vote) v).isUpvote());
boolean downvoteExists = voteObjects.stream().anyMatch((v) -> v instanceof Vote && ((Vote) v).isDownvote());
boolean isVoteCorrection = (isUpvote && downvoteExists) || (!isUpvote && upvoteExists);

if (isUpvote && pc.voteUp(votable, authUser.getId())) {
votes++;
result = true;

if (author != null) {
if (!isVoteCorrection) {
author.addRep(addReward(votable, author, votes));
authUser.incrementUpvotes();
update = true;
}
}
update = updateReputationOnUpvote(votable, votes, authUser, author, isVoteCorrection);
} else if (!isUpvote && pc.voteDown(votable, authUser.getId())) {
votes--;
result = true;

hideCommentAndReport(votable, votes, id, req);

if (author != null) {
if (isVoteCorrection) {
author.removeRep(addReward(votable, author, votes));
} else {
authUser.incrementDownvotes();
}
author.removeRep(POST_VOTEDOWN_PENALTY_AUTHOR);
//small penalty to voter
authUser.removeRep(POST_VOTEDOWN_PENALTY_VOTER);
update = true;
}
update = updateReputationOnDownvote(votable, votes, authUser, author, isVoteCorrection);
}
} catch (Exception ex) {
logger.error(null, ex);
Expand All @@ -153,6 +130,34 @@ boolean processVoteRequest(boolean isUpvote, String type, String id, HttpServlet
return result;
}

private boolean updateReputationOnUpvote(ParaObject votable, Integer votes,
Profile authUser, Profile author, boolean isVoteCorrection) {
if (author != null) {
if (!isVoteCorrection) {
author.addRep(addReward(votable, author, votes));
authUser.incrementUpvotes();
return true;
}
}
return false;
}

private boolean updateReputationOnDownvote(ParaObject votable, Integer votes,
Profile authUser, Profile author, boolean isVoteCorrection) {
if (author != null) {
if (isVoteCorrection) {
author.removeRep(addReward(votable, author, votes));
} else {
authUser.incrementDownvotes();
}
author.removeRep(POST_VOTEDOWN_PENALTY_AUTHOR);
//small penalty to voter
authUser.removeRep(POST_VOTEDOWN_PENALTY_VOTER);
return true;
}
return false;
}

private int addReward(Votable votable, Profile author, int votes) {
int reward;
if (votable instanceof Post) {
Expand Down

0 comments on commit 5d1deaa

Please sign in to comment.