ソースを参照

Merge 8a55a2c661d2d578adaba65dd52efdc6e0d97225 into f91a622cc731197181dd1b1e4b17c9bcb450ae8a

Madeline Bowe 8 年 前
コミット
47bd49daca
コミット者のEメールアドレスに関連付けられたアカウントが存在しません
共有18 個のファイルを変更した626 個の追加0 個の削除を含む
  1. 5
    0
      pom.xml
  2. 75
    0
      src/main/Resources/import.sql
  3. 2
    0
      src/main/Resources/messages.properties
  4. 46
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java
  5. 82
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java
  6. 39
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java
  7. 45
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java
  8. 50
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java
  9. 32
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java
  10. 23
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dto/OptionCount.java
  11. 24
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dto/VoteResult.java
  12. 61
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ErrorDetail.java
  13. 78
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/RestExceptionHandler.java
  14. 22
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ValidationError.java
  15. 18
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java
  16. 7
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java
  17. 7
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java
  18. 10
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java

+ 5
- 0
pom.xml ファイルの表示

43
             <artifactId>javax.inject</artifactId>
43
             <artifactId>javax.inject</artifactId>
44
             <version>1</version>
44
             <version>1</version>
45
         </dependency>
45
         </dependency>
46
+        <dependency>
47
+            <groupId>javax.xml.bind</groupId>
48
+            <artifactId>jaxb-api</artifactId>
49
+            <version>2.3.0</version>
50
+        </dependency>
46
     </dependencies>
51
     </dependencies>
47
 
52
 
48
 
53
 

+ 75
- 0
src/main/Resources/import.sql ファイルの表示

1
+insert into poll (poll_id, question) values (1, 'What is your favorite color?');
2
+insert into option (option_id, option_value, POLL_ID) values (1, 'Red', 1);
3
+insert into option (option_id, option_value, POLL_ID) values (2, 'Blue', 1);
4
+insert into option (option_id, option_value, POLL_ID) values (3, 'Green', 1);
5
+
6
+insert into poll (poll_id, question) values (2, 'What is your favorite band?');
7
+insert into option (option_id, option_value, poll_id) values (1, 'RCHP', 1);
8
+insert into option (option_id, option_value, poll_id) values (2, 'Beatles', 1);
9
+insert into option (option_id, option_value, poll_id) values (3, 'Rolling Stones', 1);
10
+
11
+insert into poll (poll_id, question) values (3, 'What is your favorite animal?');
12
+insert into option (option_id, option_value, poll_id) values (1, 'Monkey', 1);
13
+insert into option (option_id, option_value, poll_id) values (2, 'Frog', 1);
14
+insert into option (option_id, option_value, poll_id) values (3, 'Starfish', 1);
15
+
16
+insert into poll (poll_id, question) values (4, 'What is your favorite state?');
17
+insert into option (option_id, option_value, poll_id) values (1, 'Delaware', 1);
18
+insert into option (option_id, option_value, poll_id) values (2, 'Maryland', 1);
19
+insert into option (option_id, option_value, poll_id) values (3, 'New Jersey', 1);
20
+
21
+insert into poll (poll_id, question) values (5, 'What is your favorite Beatles song?');
22
+insert into option (option_id, option_value, poll_id) values (1, 'Let it Be', 1);
23
+insert into option (option_id, option_value, poll_id) values (2, 'Yellow Submarine', 1);
24
+insert into option (option_id, option_value, poll_id) values (3, '', 1);
25
+
26
+insert into poll (poll_id, question) values (6, 'What is your favorite color?');
27
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
28
+insert into option (option_id, option_value, poll_id) values (2, 'Blue', 1);
29
+insert into option (option_id, option_value, poll_id) values (3, 'Green', 1);
30
+
31
+insert into poll (poll_id, question) values (7, 'What is your favorite color?');
32
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
33
+insert into option (option_id, option_value, poll_id) values (2, 'Blue', 1);
34
+insert into option (option_id, option_value, poll_id) values (3, 'Green', 1);
35
+
36
+insert into poll (poll_id, question) values (8, 'What is your favorite color?');
37
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
38
+insert into option (option_id, option_value, poll_id) values (2, 'Blue', 1);
39
+insert into option (option_id, option_value, poll_id) values (3, 'Green', 1);
40
+
41
+insert into poll (poll_id, question) values (9, 'What is your favorite color?');
42
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
43
+insert into option (option_id, option_value, poll_id) values (2, 'Blue', 1);
44
+insert into option (option_id, option_value, poll_id) values (3, 'Green', 1);
45
+
46
+insert into poll (poll_id, question) values (10, 'What is your favorite color?');
47
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
48
+insert into option (option_id, option_value, poll_id) values (2, 'Blue', 1);
49
+insert into option (option_id, option_value, poll_id) values (3, 'Green', 1);
50
+
51
+insert into poll (poll_id, question) values (11, 'What is your favorite color?');
52
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
53
+insert into option (option_id, option_value, poll_id) values (2, 'Blue', 1);
54
+insert into option (option_id, option_value, poll_id) values (3, 'Green', 1);
55
+
56
+insert into poll (poll_id, question) values (12, 'What is your favorite color?');
57
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
58
+insert into option (option_id, option_value, poll_id) values (2, 'Blue', 1);
59
+insert into option (option_id, option_value, poll_id) values (3, 'Green', 1);
60
+
61
+insert into poll (poll_id, question) values (13, 'What is your favorite color?');
62
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
63
+insert into option (option_id, option_value, poll_id) values (2, 'Blue', 1);
64
+insert into option (option_id, option_value, poll_id) values (3, 'Green', 1);
65
+
66
+insert into poll (poll_id, question) values (14, 'What is your favorite color?');
67
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
68
+insert into option (option_id, option_value, poll_id) values (2, 'Blue', 1);
69
+insert into option (option_id, option_value, poll_id) values (3, 'Green', 1);
70
+
71
+insert into poll (poll_id, question) values (15, 'What is your favorite color?');
72
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
73
+insert into option (option_id, option_value, poll_id) values (2, 'Blue', 1);
74
+insert into option (option_id, option_value, poll_id) values (3, 'Green', 1);
75
+

+ 2
- 0
src/main/Resources/messages.properties ファイルの表示

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

+ 46
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java ファイルの表示

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.dto.OptionCount;
5
+import io.zipcoder.tc_spring_poll_application.dto.VoteResult;
6
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
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 javax.inject.Inject;
15
+import java.util.HashMap;
16
+import java.util.Map;
17
+
18
+@RestController
19
+public class ComputeResultController {
20
+    @Inject
21
+    private VoteRepository voteRepository;
22
+
23
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
24
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
25
+        VoteResult voteResult = new VoteResult();
26
+        Iterable<Vote> allVotes = voteRepository.findByPoll(pollId);
27
+
28
+        int totalVotes = 0;
29
+
30
+        Map<Long, OptionCount> tempMap = new HashMap<Long, OptionCount>();
31
+        for(Vote v : allVotes) {
32
+            totalVotes ++;
33
+            // Get the OptionCount corresponding to this Option
34
+            OptionCount optionCount = tempMap.get(v.getOption().getId());
35
+            if(optionCount == null) {
36
+                optionCount = new OptionCount();
37
+                optionCount.setOptionId(v.getOption().getId());
38
+                tempMap.put(v.getOption().getId(), optionCount);
39
+            }
40
+            optionCount.setCount(optionCount.getCount()+1);
41
+        }
42
+        voteResult.setTotalVotes(totalVotes);
43
+        voteResult.setResults(tempMap.values());
44
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
45
+    }
46
+}

+ 82
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java ファイルの表示

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

+ 39
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java ファイルの表示

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

+ 45
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java ファイルの表示

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
+    @Column(name = "OPTION_ID")
12
+    @GeneratedValue
13
+    @Id
14
+    private Long id;
15
+
16
+    @Column(name = "POLL_ID")
17
+    private Long pollId;
18
+
19
+    @Column(name = "OPTION_VALUE")
20
+    private String value;
21
+
22
+    public Long getId() {
23
+        return id;
24
+    }
25
+
26
+    public void setId(Long id) {
27
+        this.id = id;
28
+    }
29
+
30
+    public String getValue() {
31
+        return value;
32
+    }
33
+
34
+    public void setValue(String value) {
35
+        this.value = value;
36
+    }
37
+
38
+    public Long getPollId() {
39
+        return pollId;
40
+    }
41
+
42
+    public void setPollId(Long pollId) {
43
+        this.pollId = pollId;
44
+    }
45
+}

+ 50
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java ファイルの表示

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
+    @Column(name = "POLL_ID")
13
+    @GeneratedValue
14
+    @Id
15
+    private Long id;
16
+
17
+    @Column(name = "QUESTION")
18
+    @NotEmpty
19
+    private String question;
20
+
21
+    @OrderBy
22
+    @JoinColumn(name = "OPTION_ID")
23
+    @OneToMany(cascade = CascadeType.ALL)
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
+}

+ 32
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java ファイルの表示

1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.*;
4
+
5
+@Entity
6
+public class Vote {
7
+
8
+    @Column(name = "VOTE_ID")
9
+    @GeneratedValue
10
+    @Id
11
+    private Long id;
12
+
13
+    @JoinColumn(name = "OPTION_ID")
14
+    @ManyToOne
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
+}

+ 23
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/OptionCount.java ファイルの表示

1
+package io.zipcoder.tc_spring_poll_application.dto;
2
+
3
+public class OptionCount {
4
+
5
+    private Long optionId;
6
+    private int count;
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/io/zipcoder/tc_spring_poll_application/dto/VoteResult.java ファイルの表示

1
+package io.zipcoder.tc_spring_poll_application.dto;
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
+}

+ 61
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ErrorDetail.java ファイルの表示

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
+    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 Map<String, List<ValidationError>> getErrors() {
15
+        return errors;
16
+    }
17
+
18
+    public void setErrors(Map<String, List<ValidationError>> errors) {
19
+        this.errors = errors;
20
+    }
21
+
22
+    public String getTitle() {
23
+        return title;
24
+    }
25
+
26
+    public void setTitle(String title) {
27
+        this.title = title;
28
+    }
29
+
30
+    public int getStatus() {
31
+        return status;
32
+    }
33
+
34
+    public void setStatus(int status) {
35
+        this.status = status;
36
+    }
37
+
38
+    public String getDetail() {
39
+        return detail;
40
+    }
41
+
42
+    public void setDetail(String detail) {
43
+        this.detail = detail;
44
+    }
45
+
46
+    public long getTimeStamp() {
47
+        return timeStamp;
48
+    }
49
+
50
+    public void setTimeStamp(long timeStamp) {
51
+        this.timeStamp = timeStamp;
52
+    }
53
+
54
+    public String getDeveloperMessage() {
55
+        return developerMessage;
56
+    }
57
+
58
+    public void setDeveloperMessage(String developerMessage) {
59
+        this.developerMessage = developerMessage;
60
+    }
61
+}

+ 78
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/RestExceptionHandler.java ファイルの表示

1
+package io.zipcoder.tc_spring_poll_application.dto.error;
2
+
3
+
4
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.context.MessageSource;
7
+import org.springframework.http.HttpHeaders;
8
+import org.springframework.http.HttpStatus;
9
+import org.springframework.http.ResponseEntity;
10
+import org.springframework.http.converter.HttpMessageNotReadableException;
11
+import org.springframework.validation.FieldError;
12
+import org.springframework.web.bind.MethodArgumentNotValidException;
13
+import org.springframework.web.bind.annotation.ControllerAdvice;
14
+import org.springframework.web.bind.annotation.ExceptionHandler;
15
+import org.springframework.web.bind.annotation.ResponseBody;
16
+import org.springframework.web.bind.annotation.ResponseStatus;
17
+import org.springframework.web.context.request.WebRequest;
18
+import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
19
+
20
+import javax.inject.Inject;
21
+import javax.servlet.http.HttpServletRequest;
22
+import java.util.ArrayList;
23
+import java.util.Date;
24
+import java.util.List;
25
+
26
+@ControllerAdvice
27
+public class RestExceptionHandler extends ResponseEntityExceptionHandler {
28
+
29
+    @Autowired
30
+    @Inject
31
+    private MessageSource messageSource;
32
+
33
+    @Override
34
+    protected ResponseEntity<Object> handleHttpMessageNotReadable(
35
+            HttpMessageNotReadableException ex, HttpHeaders headers,
36
+            HttpStatus status, WebRequest request) {
37
+            ErrorDetail errorDetail = new ErrorDetail();
38
+            errorDetail.setTimeStamp(new Date().getTime());
39
+            errorDetail.setStatus(status.value());
40
+            errorDetail.setTitle("Message Not Readable");
41
+            errorDetail.setDetail(ex.getMessage());
42
+            errorDetail.setDeveloperMessage(ex.getClass().getName());
43
+        return handleExceptionInternal(ex, errorDetail, headers, status, request);
44
+    }
45
+
46
+
47
+    @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST)
48
+    public @ResponseBody
49
+    ErrorDetail handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request) {
50
+        ErrorDetail errorDetail = new ErrorDetail();
51
+        // Populate errorDetail instance
52
+        errorDetail.setTimeStamp(new Date().getTime());
53
+        errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
54
+        String requestPath = (String) request.getAttribute("javax.servlet.error. request_uri");
55
+        if(requestPath == null) {
56
+            requestPath = request.getRequestURI();
57
+        }
58
+        errorDetail.setTitle("Validation Failed");
59
+        errorDetail.setDetail("Input validation failed");
60
+        errorDetail.setDeveloperMessage(manve.getClass().getName());
61
+        // Create ValidationError instances
62
+        List<FieldError> fieldErrors =  manve.getBindingResult().getFieldErrors();
63
+        for(FieldError fe : fieldErrors) {
64
+            List<ValidationError> validationErrorList = errorDetail.getErrors().
65
+                    get(fe.getField());
66
+            if(validationErrorList == null) {
67
+            }
68
+            validationErrorList = new ArrayList<ValidationError>();
69
+            errorDetail.getErrors().put(fe.getField(),
70
+                    validationErrorList);
71
+            ValidationError validationError = new ValidationError();
72
+            validationError.setCode(fe.getCode());
73
+            validationError.setMessage(messageSource.getMessage(fe, null));
74
+            validationErrorList.add(validationError);
75
+        }
76
+        return errorDetail;
77
+    }
78
+}

+ 22
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ValidationError.java ファイルの表示

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

+ 18
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java ファイルの表示

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
+    public ResourceNotFoundException(String message) {
12
+        super(message);
13
+    }
14
+
15
+    public ResourceNotFoundException(String message, Throwable cause) {
16
+        super(message, cause);
17
+    }
18
+}

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java ファイルの表示

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 ファイルの表示

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

+ 10
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java ファイルの表示

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.* from Option o, Vote v where o.POLL_ID = ?1 and v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
9
+    public Iterable<Vote> findByPoll(Long pollId);
10
+}