Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Complete #33

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 5 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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>io.zipcoder</groupId>
<artifactId>spring-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.zipcoder.tc_spring_poll_application.controller;
import io.zipcoder.tc_spring_poll_application.domain.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.*;
import javax.validation.Valid;
import java.net.URI;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;


@RestController
public class PollController {
private PollRepository pollRepository;


@Autowired
public PollController(PollRepository pollRepository) {
this.pollRepository = pollRepository;
}

@RequestMapping(value = "/polls", method = RequestMethod.GET)
public ResponseEntity<Iterable<Poll>> getAllPolls(Pageable P) {
Iterable<Poll> allPolls = pollRepository.findAll((Iterable<Long>) P);
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<>(null, HttpStatus.CREATED);
}

@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/{pollId}", method=RequestMethod.PUT)
public ResponseEntity<?> updatePoll(@RequestBody @Valid 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);
}

public void verifyPoll(Long pollId){
if(!pollRepository.exists(pollId)) {
throw new ResourceNotFoundException();
}
}






}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.zipcoder.tc_spring_poll_application.controller;
import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
import org.springframework.beans.factory.annotation.*;
import org.springframework.http.ResponseEntity;
import io.zipcoder.tc_spring_poll_application.domain.Vote;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.http.HttpStatus;


@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,31 @@
package io.zipcoder.tc_spring_poll_application.domain;

import javax.persistence.*;

@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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.zipcoder.tc_spring_poll_application.domain;

import javax.persistence.*;
import java.util.Set;
import javax.validation.constraints.*;
import org.hibernate.validator.constraints.NotEmpty;

@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<Option> 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,32 @@
package io.zipcoder.tc_spring_poll_application.domain;

import javax.persistence.*;


@Entity
public class Vote {
@Id
@GeneratedValue
@Column(name = "VOTE_ID")
private Long id;

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

public Long getId() {
return id;
}

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

public io.zipcoder.tc_spring_poll_application.domain.Option getOption() {
return option;
}

public void setOption(io.zipcoder.tc_spring_poll_application.domain.Option option) {
this.option = option;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.zipcoder.tc_spring_poll_application.dtos;
import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.http.ResponseEntity;
import io.zipcoder.tc_spring_poll_application.domain.*;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
import java.util.ArrayList;

@RestController
public class ComputeResultController {
@Autowired
private VoteRepository voteRepository;

private PollRepository pollRepository;


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);
io.zipcoder.tc_spring_poll_application.domain.Poll poll = pollRepository.findOne(pollId);
voteResult.setResults(new java.util.ArrayList<OptionCount>());
for (Option o : poll.getOptions()) {
OptionCount optionCount = new OptionCount();
optionCount.setOptionId(o.getId());
optionCount.setCount(Math.toIntExact(((ArrayList<Vote>) allVotes).stream().filter(v -> v.getOption().equals(o)).count()));
voteResult.getResults().add(optionCount);
}

return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.zipcoder.tc_spring_poll_application.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.zipcoder.tc_spring_poll_application.dtos;
import java.util.Collection;

public class VoteResult {

private int totalVotes;
private Collection<OptionCount> 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;
}

}
Loading