Skip to content
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
30 changes: 30 additions & 0 deletions src/main/java/dtos/OptionCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dtos;

public class OptionCount {
private Long optionId;
private int count;

public OptionCount() {
}

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;
}
}
32 changes: 32 additions & 0 deletions src/main/java/dtos/VoteResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dtos;

import java.util.Collection;
public class VoteResult {

private int totalVotes;
private Collection<OptionCount> results;

public VoteResult() {
}

public VoteResult(int totalVotes, Collection<OptionCount> results) {
this.totalVotes = totalVotes;
this.results = results;
}

public int getTotalVotes() {
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,31 @@
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 org.springframework.web.bind.annotation.RestController;

@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);

return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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.PollRespository;
import org.springframework.beans.factory.annotation.Autowired;
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 {

PollRespository pollRespository;

public PollController() {
}

@Autowired
public PollController(PollRespository pollRespository) {
this.pollRespository = pollRespository;
}

@Valid
@RequestMapping(value = "/polls", method = RequestMethod.POST)
public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
poll = pollRespository.save(poll);
URI newPollUri = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(poll.getId())
.toUri();
return new ResponseEntity<>(newPollUri, HttpStatus.CREATED);
}

@RequestMapping(value = "/polls", method = RequestMethod.GET)
public ResponseEntity<Iterable<Poll>> getAllPolls() {
Iterable<Poll> allPolls = pollRespository.findAll();
return new ResponseEntity<>(allPolls, HttpStatus.OK);
}

@RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET)
public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
verifyPoll(pollId);
Poll p = pollRespository.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
verifyPoll(pollId);
Poll p = pollRespository.save(poll);
return new ResponseEntity<>(HttpStatus.OK);
}

@RequestMapping(value = "/polls/{pollId}", method = RequestMethod.DELETE)
public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
verifyPoll(pollId);
pollRespository.delete(pollId);
return new ResponseEntity<>(HttpStatus.OK);
}

public void verifyPoll(Long PollId) {
if (pollRespository.findOne(PollId) == null){
throw new ResourceNotFoundException("Not Found");
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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<Vote> getAllVotes() {
return voteRepository.findAll();
}
@RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
public Iterable<Vote> getVote(@PathVariable Long pollId) {
return voteRepository.findVotesByPoll(pollId);
}


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

import javax.persistence.*;

@Entity
public class Option {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "OPTION_ID")
private Long id;


@Column(name = "OPTION_VALUE")
private String value;

public Option(){}

public Option(Long id, String value) {
this.id = id;
this.value = 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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.AUTO)
@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<Option> options;

public Poll(){}

public Poll(Long id, String question, Set<Option> options) {
this.id = id;
this.question = question;
this.options = options;
}

public Long getId() {
return id;
}

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

public String getQuestion() {
return question;
}

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

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

public void setOptions(Set<Option> options) {
this.options = options;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.zipcoder.tc_spring_poll_application.domain;

import javax.persistence.*;

@Entity
public class Vote {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "VOTE_ID")
private Long id;

@ManyToOne
@JoinColumn(name = "OPTION_ID")
private Option option;

public Vote(){}

public Vote(Long id, Option option) {
this.id = id;
this.option = option;
}

public Long getId() {
return id;
}

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

public Option getOption() {
return option;
}

public void setOption(Option option) {
this.option = option;
}
}
Loading