diff --git a/pom.xml b/pom.xml index ed84c9b..2395244 100644 --- a/pom.xml +++ b/pom.xml @@ -46,4 +46,4 @@ - + \ No newline at end of file diff --git a/src/main/java/Dtos/Error/ErrorDetail.java b/src/main/java/Dtos/Error/ErrorDetail.java new file mode 100644 index 0000000..36f56e4 --- /dev/null +++ b/src/main/java/Dtos/Error/ErrorDetail.java @@ -0,0 +1,63 @@ +package Dtos.Error; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ErrorDetail { + String title; + int status; + String detail; + long timeStamp; + String developerMessage; + private Map> errors = new HashMap<>(); + + public void setTitle(String title) { + this.title = title; + } + + public void setStatus(int status) { + this.status = status; + } + + public void setDetail(String detail) { + this.detail = detail; + } + + public void setTimeStamp(long timeStamp) { + this.timeStamp = timeStamp; + } + + public void setDeveloperMessage(String developerMessage) { + this.developerMessage = developerMessage; + } + + public String getTitle() { + return title; + } + + public int getStatus() { + return status; + } + + public String getDetail() { + return detail; + } + + public long getTimeStamp() { + return timeStamp; + } + + public String getDeveloperMessage() { + return developerMessage; + } + + public Map> getErrors() { + return errors; + } + + public void setErrors(Map> errors) { + this.errors = errors; + } + +} diff --git a/src/main/java/Dtos/Error/RestExceptionHandler.java b/src/main/java/Dtos/Error/RestExceptionHandler.java new file mode 100644 index 0000000..651e539 --- /dev/null +++ b/src/main/java/Dtos/Error/RestExceptionHandler.java @@ -0,0 +1,72 @@ +package Dtos.Error; + +import io.zipcoder.tc_spring_poll_application.Exception.ResourceNotFoundException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.MessageSource; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +import javax.servlet.http.HttpServletRequest; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +@ControllerAdvice +public class RestExceptionHandler extends ResponseEntityExceptionHandler { + @Autowired + private MessageSource messageSource; + + @ExceptionHandler(ResourceNotFoundException.class) + @ResponseStatus + public ResponseEntity handlerResourceNotFoundException( + ResourceNotFoundException rnfe, HttpServletRequest request) { + ErrorDetail errorDetail = new ErrorDetail(); + errorDetail.setTimeStamp(new Date().getTime()); + errorDetail.setStatus(HttpStatus.NOT_FOUND.value()); + errorDetail.setTitle("Resource Not Found"); + errorDetail.setDetail(rnfe.getMessage()); + errorDetail.setDeveloperMessage(rnfe.getClass().getName()); + + return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public @ResponseBody + ErrorDetail handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request) { + ErrorDetail errorDetail = new ErrorDetail(); + errorDetail.setTitle("Validation Failed"); + errorDetail.setStatus(HttpStatus.BAD_REQUEST.value()); + errorDetail.setDetail("Input validation failed"); + errorDetail.setTimeStamp(new Date().getTime()); + errorDetail.setDeveloperMessage(manve.getClass().getName()); + + String requestPath = (String) request.getAttribute("javax.servlet.error.request_uri"); + + if (requestPath == null) { + requestPath = request.getRequestURI(); + } + + List fieldErrors = manve.getBindingResult().getFieldErrors(); + for (FieldError fe : fieldErrors) { + List validationErrorList = errorDetail.getErrors().get(fe.getField()); + if (validationErrorList == null) { + validationErrorList = new ArrayList(); + errorDetail.getErrors().put(fe.getField(), validationErrorList); + } + ValidationError validationError = new ValidationError(); + validationError.setCode(fe.getCode()); + validationError.setMessage(messageSource.getMessage(fe, null)); + validationErrorList.add(validationError); + + } + return errorDetail; + } +} \ No newline at end of file diff --git a/src/main/java/Dtos/Error/ValidationError.java b/src/main/java/Dtos/Error/ValidationError.java new file mode 100644 index 0000000..82ebd5b --- /dev/null +++ b/src/main/java/Dtos/Error/ValidationError.java @@ -0,0 +1,23 @@ +package Dtos.Error; + +public class ValidationError { + String code; + String message; + + public void setCode(String code) { + this.code = code; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getCode() { + return code; + } + + public String getMessage() { + return message; + } + +} diff --git a/src/main/java/Dtos/OptionCount.java b/src/main/java/Dtos/OptionCount.java new file mode 100644 index 0000000..8fecab5 --- /dev/null +++ b/src/main/java/Dtos/OptionCount.java @@ -0,0 +1,23 @@ +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..023ce2d --- /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/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..a51e62d --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/Controller/ComputeResultController.java @@ -0,0 +1,33 @@ +package io.zipcoder.tc_spring_poll_application.Controller; + +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 javax.persistence.Entity; + +@Entity +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 + return new ResponseEntity(voteResult, HttpStatus.OK); + } + +} 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..e774b4f --- /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 pollRepository) { + this.pollRepository = pollRepository; + } + + @Valid + @RequestMapping(value = "/polls", method = RequestMethod.GET) + public ResponseEntity> getAllPolls() { + Iterable allPolls = pollRepository.findAll(); + return new ResponseEntity<>(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 httpResult = new HttpHeaders(); + httpResult.setLocation(newPollUri); + return new ResponseEntity<>(httpResult, 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(Long pollId) throws ResourceNotFoundException { + Poll poll = pollRepository.findOne(pollId); + if (poll == null) { + throw new ResourceNotFoundException(); + } + } + +} \ 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..1adcc99 --- /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..aafda53 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Option.java @@ -0,0 +1,39 @@ +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") + Long id; + @Column(name = "OPTION_VALUE") + String value; + + public void setId(Long id) { + this.id = id; + } + + public void setValue(String value) { + this.value = value; + } + + public Long getId() { + return id; + } + + public String getValue() { + return 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..b59c88b --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Poll.java @@ -0,0 +1,48 @@ +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") + Long id; + + @Column(name = "QUESTION") + @NotEmpty + String question; + + @JoinColumn(name = "POLL_ID") + @OneToMany(cascade = CascadeType.ALL) + @OrderBy + @Size(min=2, max = 6) + Set