#16 Seth-Abrams

開啟中
Seth-Abrams 請求將 10 次程式碼提交從 Seth-Abrams/SpringQuickPoll:master 合併至 master
共有 18 個檔案被更改,包括 527 行新增1 行删除
  1. 1
    1
      README.md
  2. 36
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java
  3. 75
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java
  4. 41
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java
  5. 34
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java
  6. 51
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java
  7. 32
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java
  8. 22
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java
  9. 24
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java
  10. 75
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ErrorDetail.java
  11. 57
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/RestExceptionHandler.java
  12. 25
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ValidationError.java
  13. 19
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java
  14. 7
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java
  15. 7
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java
  16. 14
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java
  17. 5
    0
      src/main/resources/import.sql
  18. 2
    0
      src/main/resources/message.properties

+ 1
- 1
README.md 查看文件

@@ -332,7 +332,7 @@ public interface VoteRepository extends CrudRepository<Vote, Long> {
332 332
 * At runtime, Spring Data JPA replaces the `?1` placeholder with the passed-in `pollId` parameter value.
333 333
 
334 334
 
335
-### Part 3.2.3 - Modify `VoteController`
335
+### Part 3.2.3 - Modify `VoteCo*_****_*ntroller`
336 336
 
337 337
 * Create a `getAllVotes` method in the `VoteController`
338 338
 

+ 36
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java 查看文件

@@ -0,0 +1,36 @@
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.dtos.VoteResult;
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
+import java.util.ArrayList;
15
+
16
+@RestController
17
+    public class ComputeResultController {
18
+
19
+        private VoteRepository voteRepository;
20
+
21
+        @Autowired
22
+        public ComputeResultController(VoteRepository voteRepository) {
23
+            this.voteRepository = voteRepository;
24
+        }
25
+
26
+        @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
27
+        public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
28
+            VoteResult voteResult = new VoteResult();
29
+            Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
30
+
31
+            //TODO: Implement algorithm to count votes
32
+            return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
33
+        }
34
+}
35
+
36
+

+ 75
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java 查看文件

@@ -0,0 +1,75 @@
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.exception.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
+import javax.validation.Valid;
14
+import java.net.URI;
15
+
16
+@RestController
17
+public class PollController {
18
+
19
+    private PollRepository pollRepository;
20
+
21
+    @Autowired
22
+    public PollController(PollRepository pollRepository) {
23
+        this.pollRepository = pollRepository;
24
+    }
25
+
26
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
27
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
28
+        Iterable<Poll> allPolls = pollRepository.findAll();
29
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
30
+    }
31
+
32
+    @RequestMapping (method=RequestMethod.POST)
33
+    public ResponseEntity<?> createPoll(@RequestBody @Valid Poll poll) {
34
+        URI newPollUri = ServletUriComponentsBuilder
35
+                .fromCurrentRequest()
36
+                .path("/{id}")
37
+                .buildAndExpand(poll.getId())
38
+                .toUri();
39
+        HttpHeaders header = new HttpHeaders();
40
+        header.setLocation(newPollUri);
41
+        poll = pollRepository.save(poll);
42
+        return new ResponseEntity<>(header, HttpStatus.CREATED);
43
+    }
44
+
45
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
46
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
47
+        verifyPoll(pollId);
48
+        Poll p = pollRepository.findOne(pollId);
49
+        return new ResponseEntity<> (p, HttpStatus.OK);
50
+    }
51
+
52
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
53
+    public ResponseEntity<?> updatePoll(@RequestBody @Valid Poll poll, @PathVariable Long pollId) {
54
+        // Save the entity
55
+        verifyPoll(pollId);
56
+        Poll p = pollRepository.save(poll);
57
+        return new ResponseEntity<>(HttpStatus.OK);
58
+    }
59
+
60
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
61
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
62
+        verifyPoll(pollId);
63
+        pollRepository.delete(pollId);
64
+        return new ResponseEntity<>(HttpStatus.OK);
65
+    }
66
+
67
+   // @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET)
68
+    public void verifyPoll(Long pollId) throws ResourceNotFoundException{
69
+
70
+        if(pollRepository.findOne(pollId) == null){
71
+            throw new ResourceNotFoundException();
72
+        }
73
+    }
74
+
75
+}

+ 41
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java 查看文件

@@ -0,0 +1,41 @@
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.ResponseEntity;
9
+import org.springframework.web.bind.annotation.*;
10
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11
+
12
+@RestController
13
+public class VoteController {
14
+
15
+    private VoteRepository voteRepository;
16
+
17
+    @Autowired
18
+    public VoteController(VoteRepository voteRepository) {
19
+        this.voteRepository = voteRepository;
20
+    }
21
+
22
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
23
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote vote) {
24
+        vote = voteRepository.save(vote);
25
+        // Set the headers for the newly created resource
26
+        HttpHeaders responseHeaders = new HttpHeaders();
27
+        responseHeaders.setLocation(ServletUriComponentsBuilder.
28
+                fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
29
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
30
+    }
31
+
32
+    @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
33
+    public Iterable<Vote> getAllVotes() {
34
+        return voteRepository.findAll();
35
+    }
36
+
37
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
38
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
39
+        return voteRepository.findVotesByPoll(pollId);
40
+    }
41
+} 

+ 34
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java 查看文件

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

+ 51
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java 查看文件

@@ -0,0 +1,51 @@
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
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
+
28
+    public long getId() {
29
+        return id;
30
+    }
31
+
32
+    public void setId(long id) {
33
+        this.id = id;
34
+    }
35
+
36
+    public String getQuestion() {
37
+        return question;
38
+    }
39
+
40
+    public void setQuestion(String question) {
41
+        this.question = question;
42
+    }
43
+
44
+    public Set<Option> getOptions() {
45
+        return options;
46
+    }
47
+
48
+    public void setOptions(Set<Option> options) {
49
+        this.options = options;
50
+    }
51
+}

+ 32
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java 查看文件

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

+ 22
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java 查看文件

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

+ 24
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java 查看文件

@@ -0,0 +1,24 @@
1
+package io.zipcoder.tc_spring_poll_application.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
+}

+ 75
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ErrorDetail.java 查看文件

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

+ 57
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/RestExceptionHandler.java 查看文件

@@ -0,0 +1,57 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos.error;
2
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
3
+import javafx.concurrent.Task;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.context.MessageSource;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.validation.FieldError;
9
+import org.springframework.web.bind.MethodArgumentNotValidException;
10
+import org.springframework.web.bind.annotation.ControllerAdvice;
11
+import org.springframework.web.bind.annotation.ExceptionHandler;
12
+
13
+import javax.servlet.http.HttpServletRequest;
14
+import java.util.*;
15
+
16
+@ControllerAdvice
17
+public class RestExceptionHandler {
18
+
19
+    @Autowired
20
+    MessageSource messageSource;
21
+
22
+    @ExceptionHandler(ResourceNotFoundException.class)
23
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
24
+        ErrorDetail errorDetail = new ErrorDetail();
25
+        errorDetail.setTimestamp(new Date().getTime());
26
+        errorDetail.setDeveloperMessage(rnfe.getMessage());
27
+        errorDetail.setDetail(rnfe.getStackTrace().toString());
28
+
29
+
30
+        return new ResponseEntity<>(errorDetail, HttpStatus.NOT_FOUND);
31
+    }
32
+
33
+    @ExceptionHandler(MethodArgumentNotValidException.class)
34
+    public ResponseEntity<?>
35
+    handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request){
36
+        ErrorDetail ed = new ErrorDetail();
37
+
38
+        ed.setTimeStamp(new Date().getTime());
39
+        ed.setTitle("Validation Failure");
40
+        ed.setStatus(404);
41
+        ed.setDetail("Unable to complete request.");
42
+        ed.setDeveloperMessage(Arrays.toString(manve.getStackTrace()));
43
+
44
+
45
+        List<FieldError> fieldErrors =  manve.getBindingResult().getFieldErrors();
46
+        for(FieldError fe : fieldErrors) {
47
+
48
+            List<ValidationError> validationErrorList = ed.getErrors().computeIfAbsent(fe.getField(), k -> new ArrayList<>());
49
+            ValidationError validationError = new ValidationError();
50
+            validationError.setCode(fe.getCode());
51
+            validationError.setMessage(messageSource.getMessage(fe,null));
52
+            validationErrorList.add(validationError);
53
+        }
54
+
55
+        return new ResponseEntity<>(ed, HttpStatus.BAD_REQUEST);
56
+    }
57
+}

+ 25
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ValidationError.java 查看文件

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

+ 19
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java 查看文件

@@ -0,0 +1,19 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
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 查看文件

@@ -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 查看文件

@@ -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
+}

+ 14
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java 查看文件

@@ -0,0 +1,14 @@
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
+    @Query(value = "SELECT v.* " +
9
+            "FROM Option o, Vote v " +
10
+            "WHERE o.POLL_ID = ?1 " +
11
+            "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
12
+    public Iterable<Vote> findVotesByPoll(Long pollId);
13
+}
14
+

+ 5
- 0
src/main/resources/import.sql 查看文件

@@ -0,0 +1,5 @@
1
+insert into poll (poll_id, question) values (3, 'Favorite season?');
2
+insert into option (option_id, option_value, poll_id) values (11, 'Winter', 3);
3
+insert into option (option_id, option_value, poll_id) values (12, 'Spring', 3);
4
+insert into option (option_id, option_value, poll_id) values (13, 'Summer', 3);
5
+insert into option (option_id, option_value, poll_id) values (14, 'Autumn', 3);

+ 2
- 0
src/main/resources/message.properties 查看文件

@@ -0,0 +1,2 @@
1
+NotEmpty.poll.question=Question is a required field
2
+Size.poll.options=Options must be greater than {2} and less than {1}