Yesoda Sanka 5 years ago
parent
commit
4c98e3d8f5
22 changed files with 660 additions and 0 deletions
  1. 8
    0
      pom.xml
  2. 33
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java
  3. 80
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java
  4. 49
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java
  5. 50
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java
  6. 79
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java
  7. 50
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java
  8. 9
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/repositories/OptionRepository.java
  9. 10
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/repositories/PollRepository.java
  10. 26
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/domain/repositories/VoteRepository.java
  11. 67
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dtos/ErrorDetail.java
  12. 24
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java
  13. 19
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dtos/RestExceptionHandler.java
  14. 23
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java
  15. 4
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/error/MethodAr1gumentNotValidException.java
  16. 4
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/error/RestExceptionHandler.java
  17. 33
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/error/ValidationError.java
  18. 56
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/error/Validationerrorhandler.java
  19. 19
    0
      src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java
  20. 3
    0
      src/main/resources/application.properties
  21. 4
    0
      src/main/resources/message.properties
  22. 10
    0
      src/main/resources/schema-h2.sql

+ 8
- 0
pom.xml View File

@@ -27,6 +27,14 @@
27 27
             <groupId>org.springframework.boot</groupId>
28 28
             <artifactId>spring-boot-starter-data-jpa</artifactId>
29 29
         </dependency>
30
+        <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
31
+        <dependency>
32
+            <groupId>org.thymeleaf</groupId>
33
+            <artifactId>thymeleaf</artifactId>
34
+            <version>3.0.11.RELEASE</version>
35
+        </dependency>
36
+
37
+
30 38
         <dependency>
31 39
             <groupId>org.springframework.boot</groupId>
32 40
             <artifactId>spring-boot-starter-test</artifactId>

+ 33
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java View File

@@ -0,0 +1,33 @@
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.domain.repositories.VoteRepository;
5
+import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
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
+
26
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
27
+        VoteResult voteResult = new VoteResult();
28
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
29
+
30
+        //TODO: Implement algorithm to count votes
31
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
32
+    }
33
+}

+ 80
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java View File

@@ -0,0 +1,80 @@
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.domain.repositories.PollRepository;
5
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
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.*;
10
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11
+
12
+import java.net.URI;
13
+
14
+/*
15
+Create a `PollController` class in the `controller` sub package.
16
+	* `PollController` signature should be `annotated` with `@RestController`
17
+	* PollController` has a `pollRepository` instance variable of type `PollRepository`
18
+	* Create a constructor that accepts a `PollRepository` argument and assigns its
19
+	* value to the `pollRepository` member variable.
20
+	* Mark the constructor with the `@Autowired` annotation.
21
+
22
+ */
23
+@RestController
24
+public class PollController {
25
+
26
+
27
+    PollRepository pollRepository;
28
+
29
+
30
+    @Autowired
31
+    public PollController(PollRepository pollRepository1) {
32
+        this.pollRepository = pollRepository1;
33
+
34
+    }
35
+
36
+    @RequestMapping(value = "/polls", method = RequestMethod.GET)
37
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
38
+        Iterable<Poll> allPolls = pollRepository.findAll();
39
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
40
+    }
41
+
42
+    @RequestMapping(value = "/polls", method = RequestMethod.POST)
43
+
44
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
45
+        poll = pollRepository.save(poll);
46
+
47
+        URI newPollUri = ServletUriComponentsBuilder
48
+                .fromCurrentRequest()
49
+                .path("/{id}")
50
+                .buildAndExpand(poll.getId())
51
+                .toUri();
52
+
53
+        return new ResponseEntity<>(null, HttpStatus.CREATED);
54
+    }
55
+
56
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET)
57
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
58
+        Poll p = pollRepository.findOne(pollId);
59
+        return new ResponseEntity<>(p, HttpStatus.OK);
60
+    }
61
+
62
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.PUT)
63
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
64
+        // Save the entity
65
+        Poll p = pollRepository.save(poll);
66
+        return new ResponseEntity<>(HttpStatus.OK);
67
+    }
68
+
69
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.DELETE)
70
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
71
+        pollRepository.delete(pollId);
72
+        return new ResponseEntity<>(HttpStatus.OK);
73
+    }
74
+
75
+    public void verifypoll() throws ResourceNotFoundException {
76
+        throw new ResourceNotFoundException();
77
+    }
78
+}
79
+
80
+

+ 49
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java View File

@@ -0,0 +1,49 @@
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.domain.repositories.VoteRepository;
5
+
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
+
14
+@RestController
15
+    public class VoteController {
16
+
17
+    private VoteRepository voteRepository;
18
+
19
+    @Autowired
20
+    public VoteController(VoteRepository voteRepository) {
21
+        this.voteRepository = voteRepository;
22
+    }
23
+
24
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
25
+
26
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
27
+            vote) {
28
+        vote = voteRepository.save(vote);
29
+        // Set the headers for the newly created resource
30
+
31
+        HttpHeaders responseHeaders = new HttpHeaders();
32
+
33
+        responseHeaders.setLocation(ServletUriComponentsBuilder.
34
+                fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
35
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
36
+    }
37
+
38
+    @RequestMapping(value = "/polls/votes", method = RequestMethod.GET)
39
+    public Iterable<Vote> getAllVotes() {
40
+        return voteRepository.findAll();
41
+    }
42
+
43
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.GET)
44
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
45
+        return voteRepository.findById(pollId);
46
+
47
+    }
48
+}
49
+

+ 50
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java View File

@@ -0,0 +1,50 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.*;
4
+
5
+/*
6
+ Create an `Option` class in the `domain` sub-package.
7
+* `Option` class signature is annotated with `@Entity`
8
+* `Option` has an `id` instance variable of type `Long`
9
+	* `id` should be `annotated` with
10
+		* `@Id`
11
+			* denotes primary key of this entity
12
+		* `@GeneratedValue`
13
+			* configures the way of increment of the specified `column(field)`
14
+		* `@Column(name = "OPTION_ID")`
15
+			* specifies mapped column for a persistent property or field
16
+			* without `@Column` specified, the framework assumes the field's variable-name is the persistent property name.
17
+
18
+* `Option` has a `value` instance variable of type `String`
19
+	* `value` should be `annotated` with
20
+		* `@Column(name = "OPTION_VALUE")`
21
+
22
+* Create a `getter` and `setter` for each of the respective instance variables.
23
+ */
24
+@Entity
25
+public class Option {
26
+
27
+    @Id
28
+    @GeneratedValue(strategy= GenerationType.IDENTITY )
29
+    @Column(name="OPTION_ID")
30
+   private long id;
31
+
32
+    @Column(name = "OPTION_VALUE")
33
+    private  String value;
34
+
35
+    public long getId() {
36
+        return id;
37
+    }
38
+
39
+    public void setId(long id) {
40
+        this.id = id;
41
+    }
42
+
43
+    public String getValue() {
44
+        return value;
45
+    }
46
+
47
+    public void setValue(String value) {
48
+        this.value = value;
49
+    }
50
+}

+ 79
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java View File

@@ -0,0 +1,79 @@
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
+/*
10
+Create a `Poll` class in the `domain` sub-package.
11
+* `Poll` class signature is annotated with `@Entity`
12
+* `Poll` has an `id` instance variable of type `Long`
13
+	* `id` should be `annotated` with
14
+		* `@Id`
15
+		* `@GeneratedValue`
16
+		* `Column(name = "POLL_ID")`
17
+
18
+* `Poll` has a `question` instance variable of type `String`
19
+	* `question` should be `annotated` with
20
+		* `@Column(name = "QUESTION")`
21
+
22
+* `Poll` has an `options` instance variable of type `Set` of `Option`
23
+	* `options` should be `annotated` with
24
+		* `@OneToMany(cascade = CascadeType.ALL)`
25
+		* `@JoinColumn(name = "POLL_ID")`
26
+		* `@OrderBy`
27
+
28
+* Create a `getter` and `setter` for each of the respective instance variables.
29
+ */
30
+@Entity
31
+public class Poll {
32
+
33
+
34
+    @Id
35
+    @GeneratedValue
36
+    @Column(name="POLL_ID")
37
+
38
+   private long id;
39
+    @Column(name = "QUESTION")
40
+
41
+   private  String question;
42
+
43
+    @OneToMany(cascade = CascadeType.ALL)
44
+		 @JoinColumn(name = "POLL_ID")
45
+		@OrderBy
46
+      private  Set<Option> options;
47
+
48
+
49
+    public long getId() {
50
+
51
+        return id;
52
+    }
53
+
54
+    public void setId(long id) {
55
+
56
+        this.id = id;
57
+    }
58
+
59
+    public String getQuestion() {
60
+
61
+        return question;
62
+    }
63
+    @NotEmpty
64
+
65
+    public void setQuestion(String question) {
66
+
67
+        this.question = question;
68
+    }
69
+    @Size(min=2, max = 6)
70
+    public Set<Option> getOptions() {
71
+        return options;
72
+    }
73
+
74
+    public void setOptions(Set<Option> options) {
75
+        this.options = options;
76
+    }
77
+
78
+
79
+}

+ 50
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java View File

@@ -0,0 +1,50 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.*;
4
+
5
+/*
6
+Create a `Vote` class in the `domain` sub-package.
7
+* `Vote` class signature is annotated with `@Entity`
8
+* `Vote` has an `id` instance variable of type `Long`
9
+	* `id` should be `annotated` with
10
+		* `@Id`
11
+		* `@GeneratedValue`
12
+		* `Column(name = "VOTE_ID")`
13
+
14
+* `Vote` has a `option` instance variable of type `Option`
15
+	* `option` should be `annotated` with
16
+		* `@ManyToOne`
17
+		* `@JoinColumn(name = "OPTION_ID")`
18
+
19
+* Create a `getter` and `setter` for each of the respective instance variables.
20
+
21
+
22
+ */
23
+@Entity
24
+public class Vote {
25
+    @Id
26
+    @GeneratedValue
27
+    @Column (name="VOTE_ID")
28
+    private Long id;
29
+
30
+    @ManyToOne
31
+            @JoinColumn (name="OPTION_ID")
32
+    Option option;
33
+
34
+
35
+    public Long getId() {
36
+        return id;
37
+    }
38
+
39
+    public void setId(Long id) {
40
+        this.id = id;
41
+    }
42
+
43
+    public Option getOption() {
44
+        return option;
45
+    }
46
+
47
+    public void setOption(Option option) {
48
+        this.option = option;
49
+    }
50
+}

+ 9
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/repositories/OptionRepository.java View File

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

+ 10
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/repositories/PollRepository.java View File

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

+ 26
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/repositories/VoteRepository.java View File

@@ -0,0 +1,26 @@
1
+package io.zipcoder.tc_spring_poll_application.domain.repositories;
2
+/*
3
+Create a `VoteRepository` interface in the `repositories` subpackage.
4
+* `VoteRepository` is a subclass of `CrudRepository<Vote, Long>`
5
+ */
6
+
7
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
8
+import org.springframework.data.jpa.repository.Query;
9
+import org.springframework.data.repository.CrudRepository;
10
+import org.springframework.stereotype.Repository;
11
+
12
+@Repository
13
+public interface VoteRepository extends CrudRepository <Vote,Long> {
14
+    
15
+        @Query(value = "SELECT v.* " +
16
+                "FROM Option o, Vote v " +
17
+                "WHERE o.POLL_ID = ?1 " +
18
+                "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
19
+        public Iterable<Vote> findVotesByPoll(Long pollId);
20
+
21
+    Iterable<Vote> findById(Long pollId);
22
+}
23
+
24
+
25
+
26
+

+ 67
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/ErrorDetail.java View File

@@ -0,0 +1,67 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
2
+/*
3
+String title`: a brief title of the error condition, eg: "Validation Failure" or "Internal Server Error"
4
+- `int status`: the HTTP status code for the current request; redundant but useful for client-side error handling
5
+- `String detail`: A short, human-readable description of the error that may be presented to a user
6
+- `long timeStamp`: the time in milliseconds when the error occurred
7
+- `String developerMessage`: detailed information such as exception class name or a stack trace
8
+useful for developers to debug
9
+
10
+ */
11
+
12
+import io.zipcoder.tc_spring_poll_application.error.ValidationError;
13
+
14
+import java.util.List;
15
+import java.util.Map;
16
+
17
+public class ErrorDetail {
18
+    String title;
19
+    int status;
20
+    String detail;
21
+    Long timeStamp;
22
+    String developerMessage;
23
+
24
+    public String getTitle() {
25
+        return title;
26
+    }
27
+
28
+    public void setTitle(String title) {
29
+        this.title = title;
30
+    }
31
+
32
+    public int getStatus() {
33
+        return status;
34
+    }
35
+
36
+    public void setStatus(int status) {
37
+        this.status = status;
38
+    }
39
+
40
+    public String getDetail() {
41
+        return detail;
42
+    }
43
+
44
+    public void setDetail(String detail) {
45
+        this.detail = detail;
46
+    }
47
+
48
+    public Long getTimeStamp() {
49
+        return timeStamp;
50
+    }
51
+
52
+    public void setTimeStamp(Long timeStamp) {
53
+        this.timeStamp = timeStamp;
54
+    }
55
+
56
+    public String getDeveloperMessage() {
57
+        return developerMessage;
58
+    }
59
+
60
+    public void setDeveloperMessage(String developerMessage) {
61
+        this.developerMessage = developerMessage;
62
+    }
63
+
64
+    public Map<String, List<ValidationError>> getErrors() {
65
+        return null;
66
+    }
67
+}

+ 24
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java View File

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

+ 19
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/RestExceptionHandler.java View File

@@ -0,0 +1,19 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
2
+
3
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
4
+import org.springframework.http.HttpStatus;
5
+import org.springframework.http.ResponseEntity;
6
+import org.springframework.web.bind.annotation.ControllerAdvice;
7
+import org.springframework.web.bind.annotation.ExceptionHandler;
8
+
9
+import javax.servlet.http.HttpServletRequest;
10
+
11
+@ControllerAdvice
12
+public class RestExceptionHandler {
13
+
14
+    @ExceptionHandler(ResourceNotFoundException .class)
15
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
16
+        return new ResponseEntity<>(null,  HttpStatus.NOT_FOUND );
17
+    }
18
+    }
19
+

+ 23
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java View File

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

+ 4
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/error/MethodAr1gumentNotValidException.java View File

@@ -0,0 +1,4 @@
1
+package io.zipcoder.tc_spring_poll_application.error;
2
+
3
+public class MethodAr1gumentNotValidException extends Throwable {
4
+}

+ 4
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/error/RestExceptionHandler.java View File

@@ -0,0 +1,4 @@
1
+package io.zipcoder.tc_spring_poll_application.error;
2
+
3
+public @interface RestExceptionHandler {
4
+}

+ 33
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/error/ValidationError.java View File

@@ -0,0 +1,33 @@
1
+package io.zipcoder.tc_spring_poll_application.error;
2
+
3
+import org.springframework.web.bind.MethodArgumentNotValidException;
4
+
5
+import java.util.HashMap;
6
+import java.util.List;
7
+import java.util.Map;
8
+
9
+
10
+
11
+public class ValidationError {
12
+    String code;
13
+    String message;
14
+
15
+    Map<String, List<ValidationError>> errors = new HashMap<>();
16
+
17
+    public String getCode() {
18
+        return code;
19
+    }
20
+
21
+    public void setCode(String code) {
22
+        this.code = code;
23
+    }
24
+
25
+    public String getMessage() {
26
+        return message;
27
+    }
28
+
29
+    public void setMessage(String message) {
30
+        this.message = message;
31
+    }
32
+
33
+}

+ 56
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/error/Validationerrorhandler.java View File

@@ -0,0 +1,56 @@
1
+package io.zipcoder.tc_spring_poll_application.error;
2
+
3
+import io.zipcoder.tc_spring_poll_application.dtos.ErrorDetail;
4
+import org.apache.tomcat.util.descriptor.XmlErrorHandler;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.context.MessageSource;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.ResponseEntity;
9
+import org.springframework.validation.FieldError;
10
+import org.springframework.web.bind.MethodArgumentNotValidException;
11
+import org.springframework.web.bind.annotation.ExceptionHandler;
12
+
13
+import javax.servlet.http.HttpServletRequest;
14
+import java.util.ArrayList;
15
+import java.util.List;
16
+
17
+//@RestExceptionHandler
18
+public class Validationerrorhandler<RestExceptionHandler> {
19
+    @Autowired
20
+
21
+    private  MessageSource  messageSource ;
22
+    @ExceptionHandler(MethodAr1gumentNotValidException.class)
23
+
24
+
25
+
26
+
27
+    public ResponseEntity<?>
28
+    handleValidationError(  MethodArgumentNotValidException manve,
29
+                            HttpServletRequest request){
30
+
31
+
32
+
33
+        List<FieldError> fieldErrors =  manve .getBindingResult().getFieldErrors();
34
+        for(FieldError fe : fieldErrors) {
35
+
36
+            ErrorDetail  errorDetail=new ErrorDetail() ;
37
+
38
+            List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
39
+
40
+            if(validationErrorList == null) {
41
+                validationErrorList = new ArrayList<>();
42
+
43
+                errorDetail.getErrors().put(fe.getField(), validationErrorList);
44
+            }
45
+
46
+            ValidationError validationError = new ValidationError();
47
+            validationError.setCode(fe.getCode());
48
+            validationError.setMessage(messageSource.getMessage(fe, null));
49
+            validationErrorList.add(validationError);
50
+        }
51
+        ResponseEntity responseEntity = new ResponseEntity<>( HttpStatus.BAD_REQUEST);
52
+        return responseEntity;
53
+    }
54
+    }
55
+
56
+

+ 19
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java View File

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

+ 3
- 0
src/main/resources/application.properties View File

@@ -0,0 +1,3 @@
1
+spring.profiles.active=h2
2
+
3
+spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect

+ 4
- 0
src/main/resources/message.properties View File

@@ -0,0 +1,4 @@
1
+spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle
2
+spring.datasource.platform=h2
3
+spring.jpa.hibernate.ddl-auto=none
4
+spring.datasource.continue-on-error=true

+ 10
- 0
src/main/resources/schema-h2.sql View File

@@ -0,0 +1,10 @@
1
+CREATE TABLE POLL (
2
+poll_id  NUMBER(10,0) NOT NULL AUTO_INCREMENT,
3
+question  VARCHAR2(255) DEFAULT NULL,
4
+PRIMARY KEY (ID));
5
+
6
+CREATE  TABLE OPTION(option_id  NUMBER(20,0)NOT NULL AUTO_INCREMENT ,option_value  VARCHAR (255),poll_id  NUMBER (20,0) );
7
+
8
+
9
+insert into poll (poll_id, question) values (1, 'What is your favorite color?');
10
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);