Skip to content

Commit

Permalink
Fetching candidates by ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Elrhino committed May 5, 2016
1 parent 1f61b0b commit 3e68b2a
Show file tree
Hide file tree
Showing 31 changed files with 362 additions and 184 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/arcbees/bourseje/client/NameTokens.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.arcbees.bourseje.client;

public class NameTokens {
public static final String PARAM_NAME = "name";
public static final String PARAM_ID = "id";

public static final String HOME = "/home";
public static final String IDENTIFICATION = "/identification";
Expand All @@ -35,5 +35,5 @@ public class NameTokens {
public static final String ADMIN_DASHBOARD = PREFIX_ADMIN + "/dashboard";
public static final String WINNER = PREFIX_ADMIN + "/winner";
public static final String ADD = PREFIX_ADMIN + "/add";
public static final String EDIT = PREFIX_ADMIN+ "/edit";
public static final String EDIT = PREFIX_ADMIN + "/edit";
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
package com.arcbees.bourseje.client.admin.dashboard;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.arcbees.bourseje.client.AdminRestCallback;
import com.arcbees.bourseje.client.NameTokens;
import com.arcbees.bourseje.client.RestCallbackImpl;
import com.arcbees.bourseje.client.admin.AdminPresenter;
import com.arcbees.bourseje.client.admin.dashboard.candidate.CandidateAdminPresenter;
import com.arcbees.bourseje.client.admin.dashboard.candidate.CandidateAdminPresenterFactory;
Expand Down Expand Up @@ -94,13 +91,12 @@ interface MyProxy extends ProxyPlace<AdminDashboardPresenter> {
protected void onReveal() {
clearSlot(SLOT_CANDIDATES);

dispatch.execute(adminService.getVotesPerCandidate(), new AdminRestCallback<Collection<CandidateResult>>() {
@Override
public void onSuccess(final Collection<CandidateResult> votesPerCandidate) {
setVotesPerCandidates(votesPerCandidate);
}
});
getCandidates();

setCurrentVoteState();
}

private void setCurrentVoteState() {
dispatch.execute(voteService.getCurrentVoteState(), new AdminRestCallback<VoteState>() {
@Override
public void onSuccess(VoteState currentState) {
Expand All @@ -109,24 +105,58 @@ public void onSuccess(VoteState currentState) {
});
}

private void setVotesPerCandidates(final Collection<CandidateResult> votesPerCandidate) {
dispatch.execute(candidateService.getCandidates(), new RestCallbackImpl<List<Candidate>>() {
private void getCandidates() {
dispatch.execute(candidateService.getCandidates(), new AdminRestCallback<List<Candidate>>() {
@Override
public void onSuccess(List<Candidate> candidates) {
createCandidates(candidates, convertToMap(candidates, votesPerCandidate));
public void onSuccess(final List<Candidate> result) {
getVotesPerCandidates(result);
}
});
}

private void createCandidates(List<Candidate> candidates, Map<String, Integer> votesPerCandidates) {
for (Candidate candidate : candidates) {
Integer nbOfVotes = votesPerCandidates.get(candidate.getName());
CandidateAdminPresenter widget = presenterWidgetFactory.create(candidate, nbOfVotes);
private void getVotesPerCandidates(final List<Candidate> candidates) {
if (!candidates.isEmpty()) {
dispatch.execute(adminService.getVotesPerCandidate(), new AdminRestCallback<Collection<CandidateResult>>() {
@Override
public void onSuccess(final Collection<CandidateResult> candidateResults) {
createCandidatesWidgets(candidateResults, candidates);
}
});
}
}

private void createCandidatesWidgets(
final Collection<CandidateResult> candidateResults,
final Collection<Candidate> candidates) {
for (final Candidate candidate : candidates) {
CandidateResult candidateResult = findVotesPerCandidate(candidateResults, candidate);

addToSlot(SLOT_CANDIDATES, widget);
if (candidateResult == null) {
createCandidates(candidate, 0);
} else {
createCandidates(candidate, candidateResult.getNumberOfVotes());
}
}
}

private CandidateResult findVotesPerCandidate(
Collection<CandidateResult> candidateResults,
final Candidate candidate) {
for (CandidateResult candidateResult : candidateResults) {
if (candidate.getId().equals(candidateResult.getCandidateId())) {
return candidateResult;
}
}

return null;
}

private void createCandidates(Candidate candidate, int nbOfVotes) {
CandidateAdminPresenter widget = presenterWidgetFactory.create(candidate, nbOfVotes);

addToSlot(SLOT_CANDIDATES, widget);
}

@Override
public void onLoginClicked() {
String currentUrl = Window.Location.getHref();
Expand Down Expand Up @@ -164,24 +194,4 @@ public void onSuccess(Void result) {
}
});
}

private Map<String, Integer> convertToMap(List<Candidate> candidatesList, Collection<CandidateResult>
candidateResults) {
Map<String, Integer> resultsMap = new HashMap<>();
Map<String, Integer> results = new HashMap<>();

for (CandidateResult result : candidateResults) {
resultsMap.put(result.getCandidateName(), result.getNumberOfVotes());
}

for (Candidate candidate : candidatesList) {
if (resultsMap.containsKey(candidate.getName())) {
results.put(candidate.getName(), resultsMap.get(candidate.getName()));
} else {
results.put(candidate.getName(), 0);
}
}

return results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void onModify(Candidate candidate) {

@Override
public void onDelete() {
if (Window.confirm("Are you sure you want to delete " + candidate.getName())) {
if (Window.confirm("Are you sure you want to delete " + candidate.getName() + " ?")) {
dispatch.execute(adminService.removeCandidate(candidate.getName()), new AdminRestCallback<Void>() {
@Override
public void onSuccess(Void result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ interface MyProxy extends ProxyPlace<EditPresenter> {

@Override
public void prepareFromRequest(PlaceRequest request) {
String name = request.getParameter("name", "");
Long candidateId = Long.valueOf(request.getParameter("id", ""));

dispatch.execute(candidateService.getCandidateByName(name), new RestCallbackImpl<Candidate>() {
dispatch.execute(candidateService.getCandidateById(candidateId), new RestCallbackImpl<Candidate>() {
@Override
public void onSuccess(Candidate candidate) {
candidateToUpdate = candidate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,30 @@

package com.arcbees.bourseje.client.admin.event;

import com.arcbees.bourseje.shared.Candidate;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HasHandlers;

public class VoteEvent extends GwtEvent<VoteEventHandler> {
public static final Type<VoteEventHandler> TYPE = new Type<>();

private final Candidate candidate;

public VoteEvent(Candidate candidate) {
this.candidate = candidate;
}

@Override
public Type<VoteEventHandler> getAssociatedType() {
return TYPE;
}

public static void fire(HasHandlers source) {
source.fireEvent(new VoteEvent());
public static void fire(HasHandlers source, Candidate candidate) {
source.fireEvent(new VoteEvent(candidate));
}

@Override
protected void dispatch(VoteEventHandler handler) {
handler.onVote(this);
handler.onVote(this, candidate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

package com.arcbees.bourseje.client.admin.event;

import com.arcbees.bourseje.shared.Candidate;
import com.google.gwt.event.shared.EventHandler;

public interface VoteEventHandler extends EventHandler {
void onVote(VoteEvent voteEvent);
void onVote(VoteEvent voteEvent, Candidate candidate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ interface MyProxy extends ProxyPlace<WinnerPresenter> {

private final RestDispatch dispatch;
private final AdminService adminService;
private CandidateService candidateService;
private final CandidateService candidateService;

private CandidateResult candidateResult;

@Inject
WinnerPresenter(
Expand All @@ -71,15 +73,13 @@ interface MyProxy extends ProxyPlace<WinnerPresenter> {
protected void onReveal() {
dispatch.execute(adminService.getWinner(), new AdminRestCallback<CandidateResult>() {
@Override
public void onSuccess(final CandidateResult candidateResult) {
getCandidateByName(candidateResult);
public void onSuccess(final CandidateResult result) {
candidateResult = result;
}
});
}

private void getCandidateByName(final CandidateResult candidateResult) {
dispatch.execute(candidateService.getCandidateByName(candidateResult.getCandidateName()), new
AdminRestCallback<Candidate>() {
dispatch.execute(candidateService.getCandidateById(candidateResult.getCandidateId()),
new AdminRestCallback<Candidate>() {
@Override
public void onSuccess(Candidate candidate) {
setInView(candidate, candidateResult.getNumberOfVotes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public interface AdminService {
RestAction<Void> addCandidate(Candidate candidate);

@PUT
@Path(ResourcesPath.CANDIDATE + ResourcesPath.CANDIDATE_NAME)
RestAction<Void> updateCandidate(@PathParam(Parameters.NAME) String name, Candidate candidate);
@Path(ResourcesPath.CANDIDATE + ResourcesPath.CANDIDATE_ID)
RestAction<Void> updateCandidate(@PathParam(Parameters.ID) Long id, Candidate candidate);

@DELETE
@Path(ResourcesPath.CANDIDATE + ResourcesPath.CANDIDATE_NAME)
RestAction<Void> removeCandidate(@PathParam(Parameters.NAME) String name);
@Path(ResourcesPath.CANDIDATE + ResourcesPath.CANDIDATE_ID)
RestAction<Void> removeCandidate(@PathParam(Parameters.ID) Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

import java.util.List;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

import com.arcbees.bourseje.shared.Candidate;
import com.arcbees.bourseje.shared.Parameters;
Expand All @@ -35,6 +33,6 @@ public interface CandidateService {
RestAction<List<Candidate>> getCandidates();

@GET
@Path(ResourcesPath.CANDIDATE_NAME)
RestAction<Candidate> getCandidateByName(@PathParam(Parameters.NAME) String name);
@Path(ResourcesPath.CANDIDATE_ID)
RestAction<Candidate> getCandidateById(@PathParam(Parameters.ID) Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@

import com.arcbees.bourseje.client.NameTokens;
import com.arcbees.bourseje.client.RestCallbackImpl;
import com.arcbees.bourseje.client.admin.event.VoteEvent;
import com.arcbees.bourseje.client.api.CandidateService;
import com.arcbees.bourseje.client.api.VoteService;
import com.arcbees.bourseje.client.application.ApplicationPresenter;
import com.arcbees.bourseje.shared.Candidate;
import com.arcbees.bourseje.shared.CookieNames;
import com.arcbees.bourseje.shared.VoteItem;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Cookies;
import com.google.inject.Inject;
Expand Down Expand Up @@ -60,7 +58,7 @@ interface MyProxy extends ProxyPlace<ConfirmVotePresenter> {
private final VoteService voteService;
private final CandidateService candidateService;

private String name;
private String id;

@Inject
ConfirmVotePresenter(
Expand Down Expand Up @@ -88,12 +86,12 @@ public void prepareFromRequest(PlaceRequest request) {
return;
}

name = request.getParameter(NameTokens.PARAM_NAME, "noSelection");
setCandidateInView(name);
id = request.getParameter(NameTokens.PARAM_ID, "noSelection");
setCandidateInView(id);
}

private void setCandidateInView(String name) {
dispatch.execute(candidateService.getCandidateByName(name), new RestCallbackImpl<Candidate>() {
private void setCandidateInView(String id) {
dispatch.execute(candidateService.getCandidateById(Long.valueOf(id)), new RestCallbackImpl<Candidate>() {
@Override
public void onFailure(Throwable throwable) {
revealPlace(NameTokens.VOTE);
Expand All @@ -110,7 +108,7 @@ public void onSuccess(Candidate candidate) {

@Override
public void onConfirmClicked() {
VoteItem voteItem = new VoteItem(name);
VoteItem voteItem = new VoteItem(Long.valueOf(id));
dispatch.execute(voteService.vote(voteItem), new RestCallbackImpl<Void>() {
@Override
public void onError(Response response) {
Expand All @@ -131,8 +129,6 @@ public void onSuccess(Void aVoid) {
}

private void revealPlace(String nameToken) {
VoteEvent.fire(this);

PlaceRequest placeRequest = new PlaceRequest.Builder()
.nameToken(nameToken)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package com.arcbees.bourseje.client.application.vote;

import com.arcbees.bourseje.client.application.vote.candidate.CandidateVoteModule;
import com.gwtplatform.mvp.client.gin.AbstractPresenterModule;

public class VoteModule extends AbstractPresenterModule {
@Override
protected void configure() {
install(new CandidateVoteModule());

bindPresenter(VotePresenter.class, VotePresenter.MyView.class,
VoteView.class, VotePresenter.MyProxy.class);
}
Expand Down
Loading

0 comments on commit 3e68b2a

Please sign in to comment.