diff --git a/src/main/Resources/import.sql b/src/main/Resources/import.sql new file mode 100644 index 0000000..9c80879 --- /dev/null +++ b/src/main/Resources/import.sql @@ -0,0 +1,16 @@ +insert into poll (poll_id, question) values (1, 'Name your favorite superheroes!'); +insert into option (option_id, option_value, poll_id) values (1, 'SpiderMan', 1); +insert into option (option_id, option_value, poll_id) values (2, 'Batman', 1); +insert into option (option_id, option_value, poll_id) values (3, 'Iron Man', 1); +insert into option (option_id, option_value, poll_id) values (4, 'Incredible Hulk', 1); +insert into option (option_id, option_value, poll_id) values (5, 'The Flash', 1); +insert into option (option_id, option_value, poll_id) values (6, 'Green Arrow', 1); +insert into option (option_id, option_value, poll_id) values (7, 'Wolverine', 1); +insert into option (option_id, option_value, poll_id) values (8, 'Daredevil', 1); +insert into option (option_id, option_value, poll_id) values (9, 'Thor', 1); +insert into option (option_id, option_value, poll_id) values (10, 'Superman', 1); +insert into option (option_id, option_value, poll_id) values (11, 'Vision', 1); +insert into option (option_id, option_value, poll_id) values (12, 'Doctor Strange', 1); +insert into option (option_id, option_value, poll_id) values (13, 'Martian Manhunter', 1); +insert into option (option_id, option_value, poll_id) values (14, 'Vision', 1); +insert into option (option_id, option_value, poll_id) values (15, 'Gio', 1); \ No newline at end of file diff --git a/src/main/Resources/messages.properties b/src/main/Resources/messages.properties new file mode 100644 index 0000000..71c19f4 --- /dev/null +++ b/src/main/Resources/messages.properties @@ -0,0 +1,3 @@ + +NotEmpty.poll.question=Question is a required field +Size.poll.options=Options must be greater than {2} and less than {1} \ No newline at end of file diff --git a/src/main/java/dtos/OptionCount.java b/src/main/java/dtos/OptionCount.java new file mode 100644 index 0000000..c0984a9 --- /dev/null +++ b/src/main/java/dtos/OptionCount.java @@ -0,0 +1,22 @@ +package dtos; + +public class OptionCount { + private Long optionId; + private int 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; + } +} \ No newline at end of file diff --git a/src/main/java/dtos/VoteResult.java b/src/main/java/dtos/VoteResult.java new file mode 100644 index 0000000..bd3cd39 --- /dev/null +++ b/src/main/java/dtos/VoteResult.java @@ -0,0 +1,23 @@ +package dtos; + +import java.util.Collection; +public class VoteResult { + private int totalVotes; + private Collection results; + + public int getTotalVotes() { + return totalVotes; + } + + public void setTotalVotes(int totalVotes) { + this.totalVotes = totalVotes; + } + + public Collection getResults() { + return results; + } + + public void setResults(Collection results) { + this.results = results; + } +} \ No newline at end of file diff --git a/src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java new file mode 100644 index 0000000..fa72e44 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java @@ -0,0 +1,50 @@ +package io.zipcoder.tc_spring_poll_application.controller; + +import dtos.OptionCount; +import dtos.VoteResult; +import io.zipcoder.tc_spring_poll_application.domain.Vote; +import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository; +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.util.HashMap; +import java.util.Map; + +@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 allVotes = voteRepository.findVotesByPoll(pollId); + + //TODO: Implement algorithm to count votes + int totalVotes = 0; + Map tempMap = new HashMap(); + for(Vote v : allVotes) { + totalVotes ++; + OptionCount optionCount = tempMap.get(v.getOption().getId()); + if(optionCount == null) { + optionCount = new OptionCount(); + optionCount.setOptionId(v.getOption().getId()); + tempMap.put(v.getOption().getId(), optionCount); + } + optionCount.setCount(optionCount.getCount()+1); + } + voteResult.setTotalVotes(totalVotes); + voteResult.setResults(tempMap.values()); + return new ResponseEntity(voteResult, HttpStatus.OK); + } +} \ No newline at end of file diff --git a/src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java new file mode 100644 index 0000000..d99c265 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java @@ -0,0 +1,73 @@ +package io.zipcoder.tc_spring_poll_application.controller; + +import io.zipcoder.tc_spring_poll_application.domain.Poll; +import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException; +import io.zipcoder.tc_spring_poll_application.repositories.PollRepository; +import org.springframework.beans.factory.annotation.Autowired; +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.inject.Inject; +import javax.validation.Valid; +import java.net.URI; + + +@RestController +public class PollController { + + + private PollRepository pollRepository; + + @Autowired + public PollController(PollRepository pollRepository) { + this.pollRepository = pollRepository; + } + + @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET) + public ResponseEntity getPoll(@PathVariable Long pollId) { + Poll p = pollRepository.findOne(pollId); + return new ResponseEntity<> (p, HttpStatus.OK); + } + + @RequestMapping(value="/polls", method= RequestMethod.GET) + public ResponseEntity> getAllPolls() { + Iterable allPolls = pollRepository.findAll(); + return new ResponseEntity<>(allPolls, HttpStatus.OK); + } + + @RequestMapping(value="/polls", method=RequestMethod.POST) + public ResponseEntity createPoll(@Valid @RequestBody Poll poll) { + poll = pollRepository.save(poll); + HttpHeaders responseHeaders = new HttpHeaders(); + URI newPollUri = ServletUriComponentsBuilder + .fromCurrentRequest() + .path("/{id}") + .buildAndExpand(poll.getId()) + .toUri(); + responseHeaders.setLocation(newPollUri); + return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED); + } + + @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); + } + + @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE) + public ResponseEntity deletePoll(@PathVariable Long pollId) { + pollRepository.delete(pollId); + return new ResponseEntity<>(HttpStatus.OK); + } + + protected void verifyPoll(Long pollId) throws ResourceNotFoundException { + Poll poll = pollRepository.findOne(pollId); + if (poll == null) { + throw new ResourceNotFoundException("Poll with id " + pollId + " not found"); + } + } +} \ No newline at end of file diff --git a/src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java new file mode 100644 index 0000000..c73cedd --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java @@ -0,0 +1,46 @@ +package io.zipcoder.tc_spring_poll_application.controller; + +import io.zipcoder.tc_spring_poll_application.domain.Vote; +import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository; +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; + +import javax.inject.Inject; + +@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 getAllVotes() { + return voteRepository.findAll(); + } + + @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET) + public Iterable getVote(@PathVariable Long pollId) { + return voteRepository.findById(pollId); + } +} \ No newline at end of file diff --git a/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java new file mode 100644 index 0000000..d7ef7ce --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java @@ -0,0 +1,35 @@ +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 void setId(Long id) { + this.id = id; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} \ No newline at end of file diff --git a/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java new file mode 100644 index 0000000..0db3670 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java @@ -0,0 +1,51 @@ +package io.zipcoder.tc_spring_poll_application.domain; + +import org.hibernate.validator.constraints.NotEmpty; + +import javax.persistence.*; +import javax.validation.constraints.Size; +import java.util.Set; + +@Entity +public class Poll { + + @Id + @GeneratedValue + @Column (name = "POLL_ID") + private long id; + + @Column (name = "QUESTION") + @NotEmpty + private String question; + + @OneToMany (cascade = CascadeType.ALL) + @JoinColumn (name = "POLL_ID") + @OrderBy + @Size (min = 2, max = 6) + private Set