diff --git a/pom.xml b/pom.xml index ed84c9b..55f5e1b 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ io.zipcoder spring-demo 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + org.springframework.boot spring-boot-starter-parent diff --git a/src/main/java/dtos/OptionCount.java b/src/main/java/dtos/OptionCount.java new file mode 100644 index 0000000..5965d67 --- /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; + } +} diff --git a/src/main/java/dtos/VoteResult.java b/src/main/java/dtos/VoteResult.java new file mode 100644 index 0000000..b3d873d --- /dev/null +++ b/src/main/java/dtos/VoteResult.java @@ -0,0 +1,24 @@ +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; + } +} diff --git a/src/main/java/dtos/error/ErrorDetail.java b/src/main/java/dtos/error/ErrorDetail.java new file mode 100644 index 0000000..b331854 --- /dev/null +++ b/src/main/java/dtos/error/ErrorDetail.java @@ -0,0 +1,72 @@ +package dtos.error; + +import java.util.List; +import java.util.Map; + +public class ErrorDetail { + private String title; + private int status; + private String detail; + private long timeStamp; + private String developerMessage; + private Map> errors; + + public ErrorDetail() { + } + + public ErrorDetail(String title, int status, String detail, long timeStamp, String developerMessage) { + this.title = title; + this.status = status; + this.detail = detail; + this.timeStamp = timeStamp; + this.developerMessage = developerMessage; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public String getDetail() { + return detail; + } + + public void setDetail(String detail) { + this.detail = detail; + } + + public long getTimeStamp() { + return timeStamp; + } + + public void setTimeStamp(long timeStamp) { + this.timeStamp = timeStamp; + } + + public String getDeveloperMessage() { + return developerMessage; + } + + public void setDeveloperMessage(String developerMessage) { + this.developerMessage = developerMessage; + } + + public Map> getErrors() { + return errors; + } + + public void setErrors(Map> errors) { + this.errors = errors; + } +} diff --git a/src/main/java/dtos/error/ValidationError.java b/src/main/java/dtos/error/ValidationError.java new file mode 100644 index 0000000..04adeaf --- /dev/null +++ b/src/main/java/dtos/error/ValidationError.java @@ -0,0 +1,22 @@ +package dtos.error; + +public class ValidationError { + private String code; + private String message; + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} 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..3b9b7c3 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java @@ -0,0 +1,54 @@ +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.ArrayList; +import java.util.Collection; +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 + Integer allVotesCount = 0; + OptionCount optionCount; + Map voteCounts = new HashMap<>(); + for (Vote element : allVotes) { + allVotesCount++; + optionCount = voteCounts.get(element.getOption().getId()); + if (optionCount == null) { + optionCount = new OptionCount(); + optionCount.setOptionId(element.getOption().getId()); + voteCounts.put(element.getOption().getId(), optionCount); + } + optionCount.setCount(optionCount.getCount() + 1); + } + voteResult.setTotalVotes(allVotesCount); + voteResult.setResults(voteCounts.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..e4b27ec --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java @@ -0,0 +1,75 @@ +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.validation.Valid; +import java.net.URI; + +@RestController +public class PollController { + + private PollRepository pollRepository; + + @Autowired + public PollController(PollRepository repo) { + this.pollRepository = repo; + } + + @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(@RequestBody @Valid 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<>(httpHeaders, HttpStatus.CREATED); + } + + @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET) + public ResponseEntity getPoll(@PathVariable Long pollId) { + verifyPoll(pollId); + Poll p = pollRepository.findOne(pollId); + return new ResponseEntity<> (p, HttpStatus.OK); + } + + @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT) + public ResponseEntity updatePoll(@RequestBody @Valid Poll poll, @PathVariable Long pollId) { + verifyPoll(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) { + verifyPoll(pollId); + pollRepository.delete(pollRepository.findOne(pollId)); + return new ResponseEntity<>(HttpStatus.OK); + } + + + public void verifyPoll(Long pollId) throws ResourceNotFoundException { + if(pollRepository.findOne(pollId) == null){ + throw new ResourceNotFoundException("Poll " + pollId + " does not exist"); + } + } + +} 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..5cb6411 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java @@ -0,0 +1,41 @@ +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.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 getAllVotes() { + return voteRepository.findAll(); + } + + @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET) + public Iterable getVote(@PathVariable Long pollId) { + return voteRepository.findVotesByPoll(pollId); + } +} 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..55d084a --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java @@ -0,0 +1,30 @@ +package io.zipcoder.tc_spring_poll_application.domain; + +import javax.persistence.*; + +@Entity +public class Option { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @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; + } +} 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..4eb4084 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java @@ -0,0 +1,49 @@ +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(strategy = GenerationType.IDENTITY) + @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