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

Skip to content

fin #44

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 1 commit into
base: master
Choose a base branch
from
Open

fin #44

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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
# Part 1 - Domain Implementation

* _Domain objects_ are the backbone for an application and contain the [business logic](https://en.wikipedia.org/wiki/Business_logic).
* Create a sub package of `io.zipcoder.tc_spring_poll_application` named `domain`.
* Create a sub package of `io.zipcoder.tc_spring_poll_application` named `io.zipcoder.tc_spring_poll_application.domain`.


## Part 1.1 - Create class `Option`

* Create an `Option` class in the `domain` sub-package.
* Create an `Option` class in the `io.zipcoder.tc_spring_poll_application.domain` sub-package.
* `Option` class signature is annotated with `@Entity`
* `Option` has an `id` instance variable of type `Long`
* `id` should be `annotated` with
Expand All @@ -32,7 +32,7 @@

## Part 1.2 - Create class `Poll`

* Create a `Poll` class in the `domain` sub-package.
* Create a `Poll` class in the `io.zipcoder.tc_spring_poll_application.domain` sub-package.
* `Poll` class signature is annotated with `@Entity`
* `Poll` has an `id` instance variable of type `Long`
* `id` should be `annotated` with
Expand All @@ -55,7 +55,7 @@

## Part 1.3 - Create class `Vote`

* Create a `Vote` class in the `domain` sub-package.
* Create a `Vote` class in the `io.zipcoder.tc_spring_poll_application.domain` sub-package.
* `Vote` class signature is annotated with `@Entity`
* `Vote` has an `id` instance variable of type `Long`
* `id` should be `annotated` with
Expand All @@ -75,7 +75,7 @@

* _Repositories_ or [Data Access Objects (DAO)](https://en.wikipedia.org/wiki/Data_access_object), provide an abstraction for interacting with _datastores_.
* Typically DAOs include an interface that provides a set of finder methods such as `findById`, `findAll`, for retrieving data, and methods to persist and delete data.
* It is customary to have one `Repository` per `domain` object.
* It is customary to have one `Repository` per `io.zipcoder.tc_spring_poll_application.domain` object.
* Create a sub-package of `io.zipcoder.tc_spring_poll_application` named `repositories`.


Expand All @@ -98,7 +98,7 @@

# Part 3 - Controller Implementation

* _Controllers_ provides all of the necessary [endpoints](https://en.wikipedia.org/wiki/Web_API#Endpoints) to access and manipulate respective domain objects.
* _Controllers_ provides all of the necessary [endpoints](https://en.wikipedia.org/wiki/Web_API#Endpoints) to access and manipulate respective io.zipcoder.tc_spring_poll_application.domain objects.
* REST resources are identified using URI endpoints.
* Create a sub package of `io.zipcoder.tc_spring_poll_application` named `controller`.

Expand Down Expand Up @@ -357,7 +357,7 @@ public Iterable<Vote> getVote(@PathVariable Long pollId) {
# Part 4 - Data Transfer Object (DTO) Implementation

* The final piece remaining for us is the implementation of the ComputeResult resource.
* Because we don’t have any domain objects that can directly help generate this resource representation, we implement two Data Transfer Objects or DTOs—OptionCount and VoteResult
* Because we don’t have any io.zipcoder.tc_spring_poll_application.domain objects that can directly help generate this resource representation, we implement two Data Transfer Objects or DTOs—OptionCount and VoteResult
* Create a sub package of `java` named `dtos`


Expand Down Expand Up @@ -506,7 +506,7 @@ public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundExcepti



## Part 5.4 - Validating domain entities
## Part 5.4 - Validating io.zipcoder.tc_spring_poll_application.domain entities

Now it's time to make sure that all objects persisted to the database actually contain valid values. Use the `org.hibernate.validator.constraints.NotEmpty` and `javax.validation.constraints.Size` and `javax.validation.Valid` annotations for validation.

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>io.zipcoder.tc_spring_poll_application.QuickPollApplication</start-class>
Expand Down Expand Up @@ -45,5 +46,4 @@
</dependency>
</dependencies>


</project>
31 changes: 31 additions & 0 deletions src/main/java/dtos/ComputeResultController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dtos;

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

//TODO: Implement algorithm to count votes
return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
}
}
22 changes: 22 additions & 0 deletions src/main/java/dtos/OptionCount.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 23 additions & 0 deletions src/main/java/dtos/VoteResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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.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 {

PollRepository pollRepository;

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

@RequestMapping(value="/polls", method= RequestMethod.GET)
public ResponseEntity<Iterable<Poll>> getAllPolls() {
Iterable<Poll> 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);

URI newPollUri = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(poll.getId())
.toUri();

return new ResponseEntity<>(newPollUri, 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(@Valid @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);
}

void verifyPoll(@PathVariable Long pollId) {
if (pollRepository.findOne(pollId) == null) {
throw new ResourceNotFoundException();
}
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 (Iterable<Vote>) voteRepository.findOne(pollId);
}

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

import javax.persistence.*;

@Entity
public class Option {

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

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

public Option() {
}

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

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.core.annotation.Order;

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")
Long id;

@Column(name = "QUESTION")
@NotEmpty
String question;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "POLL_ID")
@OrderBy
@Size(min = 2, max = 6)
Set<Option> options;

public Poll() {
}

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;
}
}
Loading