Last active
May 13, 2017 01:51
-
-
Save garystafford/ddee1a22b30689560f5f1e37600b2169 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Consumes a new candidate message, deserializes, and save to MongoDB | |
* @param candidateMessage | |
*/ | |
@RabbitListener(queues = "#{candidateQueue.name}") | |
public void getCandidateMessage(String candidateMessage) { | |
ObjectMapper objectMapper = new ObjectMapper(); | |
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | |
TypeReference<Candidate> mapType = new TypeReference<Candidate>() {}; | |
Candidate candidate = null; | |
try { | |
candidate = objectMapper.readValue(candidateMessage, mapType); | |
} catch (IOException e) { | |
logger.info(String.valueOf(e)); | |
} | |
candidateRepository.save(candidate); | |
logger.debug("Candidate {} saved to MongoDB", candidate.toString()); | |
} | |
/** | |
* Retrieves candidates from MongoDB and transforms to voter view | |
* @param election | |
* @return List of candidates | |
*/ | |
public List<CandidateVoterView> getCandidatesQueueDb(String election) { | |
Aggregation aggregation = Aggregation.newAggregation( | |
Aggregation.match(Criteria.where("election").is(election)), | |
project("firstName", "lastName", "politicalParty", "election") | |
.andExpression("concat(firstName,' ', lastName)") | |
.as("fullName"), | |
sort(Sort.Direction.ASC, "lastName") | |
); | |
AggregationResults<CandidateVoterView> groupResults | |
= mongoTemplate.aggregate(aggregation, Candidate.class, CandidateVoterView.class); | |
return groupResults.getMappedResults(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment