Skip to content

completed #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/dtos/OptionCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dtos;

public class OptionCount {
private Long optionId;
private int count;
public OptionCount(Long optionId, int count){
this.optionId = optionId;
this.count = count;
}

public Long getOptionId() {
return optionId;
}

public void setOptionId(Long optionId) {
this.optionId = optionId;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}
}
31 changes: 31 additions & 0 deletions src/main/java/dtos/VoteResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dtos;

import io.zipcoder.tc_spring_poll_application.domain.Option;

import java.util.Collection;

public class VoteResult {
private int totalVotes;
private Collection<OptionCount> results;

public int getTotalVotes() {
int count = 0;
for(OptionCount optionCount : results){
count = count + optionCount.getCount();
}
totalVotes = count;
return totalVotes;
}

public void setTotalVotes(int totalVotes) {
this.totalVotes = totalVotes;
}

public Collection<OptionCount> getResults() {
return results;
}

public void setResults(Collection<OptionCount> results) {
this.results = results;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.zipcoder.tc_spring_poll_application.Controller;

import dtos.OptionCount;
import dtos.VoteResult;
import io.zipcoder.tc_spring_poll_application.Repositories.VoteRepository;
import io.zipcoder.tc_spring_poll_application.domain.Option;
import io.zipcoder.tc_spring_poll_application.domain.Vote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.util.*;

@RestController
public class ComputeResultController {
private VoteRepository voteRepository;



@Autowired
public ComputeResultController(VoteRepository voteRepository) {
this.voteRepository = voteRepository;
}

@RequestMapping(value = "/computeresult", method = RequestMethod.GET)
public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
VoteResult voteResult = new VoteResult();
Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
//now we are creating a map of option id and total vote count
Map<Long, Integer> map = new HashMap<>();
Long optionId;
for (Vote vote : allVotes) {
optionId = vote.getOption().getId();
int updateCount = 0;
if (!map.containsKey(optionId))
map.put(optionId, 1);
else {
updateCount = map.get(optionId) + 1;
map.put(optionId, updateCount);
}
}
Set<OptionCount> optionCounts = new HashSet<>();
for (Long id : map.keySet()) {
optionCounts.add(new OptionCount(id, map.get(id)));
}
voteResult.setResults(optionCounts);
voteResult.setTotalVotes(voteResult.getTotalVotes());




//TODO: Implement algorithm to count votes
return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.zipcoder.tc_spring_poll_application.Controller;

import org.springframework.web.bind.annotation.RestController;

@RestController
public class OptionController {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package io.zipcoder.tc_spring_poll_application.Controller;

import io.zipcoder.tc_spring_poll_application.Repositories.PollRepository;
import io.zipcoder.tc_spring_poll_application.domain.Poll;
import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.validation.Valid;

import java.net.URI;

@RestController
public class PollController {
@Autowired
PollRepository pollRepository;
//Poll poll;

@Autowired
public PollController(PollRepository pollRepository) {
this.pollRepository = pollRepository;
}
@Valid
@RequestMapping(value = "/polls", method = RequestMethod.GET)
public ResponseEntity<Iterable<Poll>> getAllPolls(Pageable pageable) {
Page<Poll> allPolls = pollRepository.findAll(pageable);
return new ResponseEntity<Iterable<Poll>>(allPolls, HttpStatus.OK);
}
@Valid
@RequestMapping(value = "/polls", method = RequestMethod.POST)
public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
poll = pollRepository.save(poll);
URI newPollUri = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(poll.getId())
.toUri();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setLocation(newPollUri);
return new ResponseEntity<>(newPollUri, HttpStatus.CREATED);
}
@Valid
@RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET)
public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
Poll p = pollRepository.findOne(pollId);
return new ResponseEntity<>(p, HttpStatus.OK);
}
@Valid
@RequestMapping(value = "/polls/{pollId}", method = RequestMethod.PUT)
public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
// Save the entity
Poll p = pollRepository.save(poll);
return new ResponseEntity<>(HttpStatus.OK);
}
@Valid
@RequestMapping(value = "/polls/{pollId}", method = RequestMethod.DELETE)
public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
pollRepository.delete(pollId);
return new ResponseEntity<>(HttpStatus.OK);
}
public void verifyPoll(@PathVariable Long pollId) {
if(!pollRepository.exists(pollId))
throw new ResourceNotFoundException();
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.zipcoder.tc_spring_poll_application.Controller;

import io.zipcoder.tc_spring_poll_application.Repositories.VoteRepository;
import io.zipcoder.tc_spring_poll_application.domain.Vote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

@RestController
public class VoteController {
private VoteRepository voteRepository;

@Autowired
public VoteController(VoteRepository voteRepository) {
this.voteRepository = voteRepository;
}

@RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
vote) {
vote = voteRepository.save(vote);
// Set the headers for the newly created resource
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setLocation(ServletUriComponentsBuilder.
fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
}
@RequestMapping(value="/polls/votes", method=RequestMethod.GET)
public Iterable<Vote> getAllVotes() {
return voteRepository.findAll();
}
@RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
public Iterable<Vote> getVote(@PathVariable Long pollId) {
return voteRepository.findById(pollId);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.zipcoder.tc_spring_poll_application.Repositories;

import io.zipcoder.tc_spring_poll_application.domain.Option;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface OptionRepository extends CrudRepository<Option, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.zipcoder.tc_spring_poll_application.Repositories;


import io.zipcoder.tc_spring_poll_application.domain.Poll;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PollRepository extends PagingAndSortingRepository<Poll, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.zipcoder.tc_spring_poll_application.Repositories;

import io.zipcoder.tc_spring_poll_application.domain.Vote;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface VoteRepository extends CrudRepository<Vote, Long> {
@Query(value = "SELECT v.* " +
"FROM Option o, Vote v " +
"WHERE o.POLL_ID = ?1 " +
"AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
public Iterable<Vote> findVotesByPoll(Long pollId);

Iterable<Vote> findById(Long pollId);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.zipcoder.tc_spring_poll_application.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Option {
@Id
@GeneratedValue
@Column(name = "OPTION_ID")
private Long id;
@Column(name = "OPTION_VALUE")
private String value;

public Long getId() {
return id;
}

public String getValue() {
return value;
}

public void setId(Long id) {
this.id = id;
}

public void setValue(String value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.zipcoder.tc_spring_poll_application.domain;

import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.Set;


@Entity
public class Poll {
@Id
@GeneratedValue
@Column(name = "POLL_ID")
private Long id;
@NotEmpty
@Column(name = "QUESTION")
private String question;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "POLL_ID")
@OrderBy
@Size(min=2, max = 6)
Set<Option> options;

public Long getId() {
return id;
}

public String getQuestion() {
return question;
}

public Set<Option> getOptions() {
return options;
}

public void setId(Long id) {
this.id = id;
}

public void setQuestion(String question) {
this.question = question;
}

public void setOptions(Set<Option> options) {
this.options = options;
}
}
Loading