Explorar el Código

completed file

Elliott Stansbury hace 5 años
padre
commit
639a43820a

+ 23
- 0
src/main/java/dtos/OptionCount.java Ver fichero

@@ -0,0 +1,23 @@
1
+package dtos;
2
+
3
+public class OptionCount {
4
+    private Long optionId;
5
+    private int count;
6
+
7
+
8
+    public Long getOptionId() {
9
+        return optionId;
10
+    }
11
+
12
+    public void setOptionId(Long optionId) {
13
+        this.optionId = optionId;
14
+    }
15
+
16
+    public int getCount() {
17
+        return count;
18
+    }
19
+
20
+    public void setCount(int count) {
21
+        this.count = count;
22
+    }
23
+}

+ 24
- 0
src/main/java/dtos/VoteResult.java Ver fichero

@@ -0,0 +1,24 @@
1
+package dtos;
2
+
3
+import java.util.Collection;
4
+
5
+public class VoteResult {
6
+    private int totalVotes;
7
+    private Collection<OptionCount> results;
8
+
9
+    public int getTotalVotes() {
10
+        return totalVotes;
11
+    }
12
+
13
+    public void setTotalVotes(int totalVotes) {
14
+        this.totalVotes = totalVotes;
15
+    }
16
+
17
+    public Collection<OptionCount> getResults() {
18
+        return results;
19
+    }
20
+
21
+    public void setResults(Collection<OptionCount> results) {
22
+        this.results = results;
23
+    }
24
+}

+ 36
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Ver fichero

@@ -0,0 +1,36 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import dtos.VoteResult;
4
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
5
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.ResponseEntity;
9
+import org.springframework.web.bind.annotation.RequestMapping;
10
+import org.springframework.web.bind.annotation.RequestMethod;
11
+import org.springframework.web.bind.annotation.RequestParam;
12
+import org.springframework.web.bind.annotation.RestController;
13
+
14
+@RestController
15
+public class ComputeResultController {
16
+
17
+    private VoteRepository voteRepository;
18
+
19
+    @Autowired
20
+    public ComputeResultController(VoteRepository voteRepository){
21
+        this.voteRepository = voteRepository;
22
+    }
23
+
24
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
25
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId){
26
+        VoteResult voteResult = new VoteResult();
27
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
28
+        int count = 0;
29
+        for(Vote vote: allVotes){
30
+            count++;
31
+        }
32
+        voteResult.setTotalVotes(count);
33
+
34
+        return new ResponseEntity<>(voteResult, HttpStatus.OK);
35
+    }
36
+}

+ 95
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Ver fichero

@@ -0,0 +1,95 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
+import io.zipcoder.tc_spring_poll_application.exceptions.ResourceNotFoundException;
5
+import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.http.HttpHeaders;
8
+import org.springframework.http.HttpStatus;
9
+import org.springframework.http.ResponseEntity;
10
+import org.springframework.web.bind.annotation.*;
11
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
12
+
13
+@RestController
14
+public class PollController {
15
+    private PollRepository pollRepository;
16
+    private Poll poll;
17
+
18
+    @Autowired
19
+    public PollController(PollRepository pollRepository) {
20
+        this.pollRepository = pollRepository;
21
+    }
22
+
23
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
24
+    public ResponseEntity<Iterable<Poll>> getAllPolls(){
25
+        Iterable<Poll> allPolls = pollRepository.findAll();
26
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
27
+    }
28
+
29
+    @RequestMapping(value="/polls", method= RequestMethod.POST)
30
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll){
31
+        poll = pollRepository.save(poll);
32
+        HttpHeaders headers = new HttpHeaders();
33
+        headers.setLocation( ServletUriComponentsBuilder
34
+                        .fromCurrentRequest()
35
+                        .path("/{id}")
36
+                        .buildAndExpand(poll.getId())
37
+                        .toUri());
38
+        return new ResponseEntity<>(null, HttpStatus.CREATED);
39
+    }
40
+
41
+    //I need to go in and modify this createPoll method so that it does something with the URI from part 3.1.4
42
+
43
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET)
44
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId){
45
+
46
+        verifyPoll(pollId);
47
+        //verifies that the pollId exist in the repository
48
+
49
+        Poll p = pollRepository.findOne(pollId);
50
+        return new ResponseEntity<>(p,HttpStatus.OK);
51
+    }
52
+
53
+    //updates the poll
54
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.PUT)
55
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId){
56
+        // Save the entity
57
+
58
+        verifyPoll(pollId);
59
+        //verifies that the pollId exist in the repository
60
+
61
+
62
+        Poll p = pollRepository.save(poll);
63
+        return new ResponseEntity<>(HttpStatus.OK);
64
+    }
65
+
66
+
67
+    //Deletes the poll
68
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.DELETE)
69
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId){
70
+
71
+        verifyPoll(pollId);
72
+        //verifies that the pollId exist in the repository
73
+
74
+
75
+        pollRepository.delete(pollId);
76
+        return new ResponseEntity<>(HttpStatus.OK);
77
+    }
78
+
79
+    public void verifyPoll(Long pollId){
80
+
81
+//        this.poll.setId(pollId);
82
+
83
+        try{
84
+            if(pollRepository.findOne(pollId) == null){
85
+                throw new ResourceNotFoundException();
86
+            }
87
+            else{
88
+                System.out.println("It Works.");
89
+
90
+            }
91
+        }catch (ResourceNotFoundException e){
92
+            System.out.println("Resource Not Found");
93
+        }
94
+    }
95
+}

+ 45
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Ver fichero

@@ -0,0 +1,45 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpHeaders;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.RequestEntity;
9
+import org.springframework.http.ResponseEntity;
10
+import org.springframework.web.bind.annotation.*;
11
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
12
+
13
+import java.net.URI;
14
+
15
+@RestController
16
+public class VoteController {
17
+
18
+    private VoteRepository voteRepository;
19
+
20
+    @Autowired
21
+    public VoteController(VoteRepository voteRepository) {
22
+        this.voteRepository = voteRepository;
23
+    }
24
+
25
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
26
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote vote) {
27
+        vote = voteRepository.save(vote);
28
+        HttpHeaders responseHeaders = new HttpHeaders();
29
+        responseHeaders.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
30
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
31
+    }
32
+
33
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.GET)
34
+    public ResponseEntity<?> getVote(@PathVariable Long pollId){
35
+        Vote v = voteRepository.findOne(pollId);
36
+        return new ResponseEntity<>(v,HttpStatus.OK);
37
+    }
38
+
39
+    @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
40
+    public Iterable<Vote> getAllVotes() {
41
+        return voteRepository.findAll();
42
+    }
43
+
44
+
45
+}

+ 31
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Ver fichero

@@ -0,0 +1,31 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.*;
4
+
5
+@Entity
6
+public class Option {
7
+
8
+    @Id
9
+    @GeneratedValue(strategy = GenerationType.AUTO)
10
+    @Column(name = "OPTION_ID")
11
+    private Long id;
12
+
13
+    @Column(name = "OPTION_VALUE")
14
+    private String value;
15
+
16
+    public Long getId() {
17
+        return id;
18
+    }
19
+
20
+    public void setId(Long id) {
21
+        this.id = id;
22
+    }
23
+
24
+    public String getValue() {
25
+        return value;
26
+    }
27
+
28
+    public void setValue(String value) {
29
+        this.value = value;
30
+    }
31
+}

+ 50
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Ver fichero

@@ -0,0 +1,50 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import org.hibernate.validator.constraints.NotEmpty;
4
+
5
+import javax.persistence.*;
6
+import javax.validation.constraints.Size;
7
+import java.util.Set;
8
+
9
+@Entity
10
+public class Poll {
11
+
12
+    @Id
13
+    @GeneratedValue(strategy = GenerationType.AUTO)
14
+    @Column(name = "POLL_ID")
15
+    private Long id;
16
+
17
+    @Column(name = "QUESTION")
18
+    @NotEmpty
19
+    private String question;
20
+
21
+    @OneToMany(cascade = CascadeType.ALL)
22
+    @JoinColumn(name = "POLL_ID")
23
+    @OrderBy
24
+    @Size(min=2, max=6)
25
+    private Set<Option> options;
26
+
27
+    public Long getId() {
28
+        return id;
29
+    }
30
+
31
+    public void setId(Long id) {
32
+        this.id = id;
33
+    }
34
+
35
+    public String getQuestion() {
36
+        return question;
37
+    }
38
+
39
+    public void setQuestion(String question) {
40
+        this.question = question;
41
+    }
42
+
43
+    public Set<Option> getOptions() {
44
+        return options;
45
+    }
46
+
47
+    public void setOptions(Set<Option> options) {
48
+        this.options = options;
49
+    }
50
+}

+ 31
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Ver fichero

@@ -0,0 +1,31 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.*;
4
+
5
+@Entity
6
+public class Vote {
7
+    @Id
8
+    @GeneratedValue(strategy = GenerationType.AUTO)
9
+    @Column(name = "VOTE_ID")
10
+    private Long id;
11
+
12
+    @ManyToOne
13
+    @JoinColumn(name = "OPTION_ID")
14
+    private Option option;
15
+
16
+    public Long getId() {
17
+        return id;
18
+    }
19
+
20
+    public void setId(Long id) {
21
+        this.id = id;
22
+    }
23
+
24
+    public Option getOption() {
25
+        return option;
26
+    }
27
+
28
+    public void setOption(Option option) {
29
+        this.option = option;
30
+    }
31
+}

+ 64
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ErrorDetail.java Ver fichero

@@ -0,0 +1,64 @@
1
+package io.zipcoder.tc_spring_poll_application.dto.error;
2
+
3
+import java.util.List;
4
+import java.util.Map;
5
+
6
+public class ErrorDetail {
7
+
8
+    private String title;
9
+    private int status;
10
+    private String detail;
11
+    private long timeStamp;
12
+    private String developerMessage;
13
+    private Map<String, List<ValidationError>> errors;
14
+
15
+    public Map<String, List<ValidationError>> getErrors() {
16
+        return errors;
17
+    }
18
+
19
+    public void setErrors(Map<String, List<ValidationError>> errors) {
20
+        this.errors = errors;
21
+    }
22
+
23
+    public String getTitle() {
24
+        return title;
25
+    }
26
+
27
+    public void setTitle(String title) {
28
+        this.title = title;
29
+    }
30
+
31
+    public int getStatus() {
32
+        return status;
33
+    }
34
+
35
+    public void setStatus(int status) {
36
+        this.status = status;
37
+    }
38
+
39
+    public String getDetail() {
40
+        return detail;
41
+    }
42
+
43
+    public void setDetail(String detail) {
44
+        this.detail = detail;
45
+    }
46
+
47
+    public long getTimeStamp() {
48
+        return timeStamp;
49
+    }
50
+
51
+    public void setTimeStamp(long timeStamp) {
52
+        this.timeStamp = timeStamp;
53
+    }
54
+
55
+    public String getDeveloperMessage() {
56
+        return developerMessage;
57
+    }
58
+
59
+    public void setDeveloperMessage(String developerMessage) {
60
+        this.developerMessage = developerMessage;
61
+    }
62
+
63
+
64
+}

+ 47
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/RestExceptionHandler.java Ver fichero

@@ -0,0 +1,47 @@
1
+package io.zipcoder.tc_spring_poll_application.dto.error;
2
+
3
+import io.zipcoder.tc_spring_poll_application.exceptions.ResourceNotFoundException;
4
+import org.springframework.http.HttpStatus;
5
+import org.springframework.http.ResponseEntity;
6
+import org.springframework.validation.FieldError;
7
+import org.springframework.web.bind.MethodArgumentNotValidException;
8
+import org.springframework.web.bind.annotation.ControllerAdvice;
9
+import org.springframework.web.bind.annotation.ExceptionHandler;
10
+
11
+import javax.servlet.http.HttpServletRequest;
12
+import java.util.ArrayList;
13
+import java.util.Date;
14
+import java.util.List;
15
+
16
+@ControllerAdvice
17
+public class RestExceptionHandler {
18
+
19
+    private ValidationError messageSource;
20
+
21
+    @ExceptionHandler(ResourceNotFoundException.class)
22
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request){
23
+        ErrorDetail errorDetail = new ErrorDetail();
24
+        errorDetail.setDeveloperMessage(rnfe.getMessage());
25
+        errorDetail.setTimeStamp(new Date().getTime());
26
+        errorDetail.setDetail(rnfe.getLocalizedMessage());
27
+        return new ResponseEntity<>(errorDetail, HttpStatus.NOT_FOUND);
28
+    }
29
+
30
+    @ExceptionHandler(MethodArgumentNotValidException.class)
31
+    public ResponseEntity<?> handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request){
32
+        ErrorDetail errorDetail = new ErrorDetail();
33
+        List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
34
+        for(FieldError fe : fieldErrors){
35
+            List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
36
+            if(validationErrorList == null){
37
+                validationErrorList = new ArrayList<>();
38
+                errorDetail.getErrors().put(fe.getField(), validationErrorList);
39
+            }
40
+            ValidationError validationError = new ValidationError();
41
+            validationError.setCode(fe.getCode());
42
+//          this method isnt working from part 5.6
43
+            validationErrorList.add(validationError);
44
+        }
45
+        return new ResponseEntity<>(errorDetail, HttpStatus.BAD_REQUEST);
46
+    }
47
+}

+ 23
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ValidationError.java Ver fichero

@@ -0,0 +1,23 @@
1
+package io.zipcoder.tc_spring_poll_application.dto.error;
2
+
3
+public class ValidationError {
4
+
5
+    private String code;
6
+    private String message;
7
+
8
+    public String getCode() {
9
+        return code;
10
+    }
11
+
12
+    public void setCode(String code) {
13
+        this.code = code;
14
+    }
15
+
16
+    public String getMessage() {
17
+        return message;
18
+    }
19
+
20
+    public void setMessage(String message) {
21
+        this.message = message;
22
+    }
23
+}

+ 19
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exceptions/ResourceNotFoundException.java Ver fichero

@@ -0,0 +1,19 @@
1
+package io.zipcoder.tc_spring_poll_application.exceptions;
2
+
3
+import org.springframework.http.HttpStatus;
4
+import org.springframework.web.bind.annotation.ResponseStatus;
5
+
6
+@ResponseStatus(HttpStatus.NOT_FOUND)
7
+public class ResourceNotFoundException extends RuntimeException {
8
+
9
+    public ResourceNotFoundException() {
10
+    }
11
+
12
+    public ResourceNotFoundException(String message) {
13
+        super(message);
14
+    }
15
+
16
+    public ResourceNotFoundException(String message, Throwable cause) {
17
+        super(message, cause);
18
+    }
19
+}

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Ver fichero

@@ -0,0 +1,7 @@
1
+package io.zipcoder.tc_spring_poll_application.repositories;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
4
+import org.springframework.data.repository.CrudRepository;
5
+
6
+public interface OptionRepository extends CrudRepository<Option, Long> {
7
+}

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Ver fichero

@@ -0,0 +1,7 @@
1
+package io.zipcoder.tc_spring_poll_application.repositories;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
+import org.springframework.data.repository.CrudRepository;
5
+
6
+public interface PollRepository extends CrudRepository<Poll, Long> {
7
+}

+ 15
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Ver fichero

@@ -0,0 +1,15 @@
1
+package io.zipcoder.tc_spring_poll_application.repositories;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import org.springframework.data.jpa.repository.Query;
5
+import org.springframework.data.repository.CrudRepository;
6
+
7
+public interface VoteRepository extends CrudRepository<Vote, Long> {
8
+
9
+    @Query(value = "SELECT v.* " +
10
+            "FROM Option o, Vote v " +
11
+            "WHERE o.POLL_ID = ?1 " +
12
+            "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
13
+    public Iterable<Vote> findVotesByPoll(Long pollId);
14
+
15
+}