Trinh Tong пре 5 година
родитељ
комит
ed73a6a291

+ 30
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/RestExceptionHandler.java Прегледај датотеку

@@ -0,0 +1,30 @@
1
+package io.zipcoder.tc_spring_poll_application;
2
+
3
+import io.zipcoder.tc_spring_poll_application.error.ErrorDetail;
4
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
5
+import org.springframework.http.HttpStatus;
6
+import org.springframework.http.ResponseEntity;
7
+import org.springframework.web.bind.annotation.ControllerAdvice;
8
+import org.springframework.web.bind.annotation.ExceptionHandler;
9
+
10
+import javax.servlet.http.HttpServletRequest;
11
+import java.util.Arrays;
12
+import java.util.Date;
13
+
14
+@ControllerAdvice
15
+public class RestExceptionHandler {
16
+
17
+    @ExceptionHandler(ResourceNotFoundException.class)
18
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
19
+
20
+        ErrorDetail ed = new ErrorDetail();
21
+
22
+        ed.setTimeStamp(new Date().getTime());
23
+        ed.setTitle("Validation Failture");
24
+        ed.setStatus(404);
25
+        ed.setDetail("Unable to complete request or entity was not found.");
26
+        ed.setDeveloperMessage(Arrays.toString(rnfe.getStackTrace()));
27
+
28
+        return new ResponseEntity<>(ed, HttpStatus.NOT_FOUND);
29
+    }
30
+}

+ 28
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/OptionController.java Прегледај датотеку

@@ -0,0 +1,28 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
4
+import io.zipcoder.tc_spring_poll_application.repositories.OptionRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.RequestMapping;
9
+import org.springframework.web.bind.annotation.RequestMethod;
10
+import org.springframework.web.bind.annotation.RestController;
11
+
12
+@RestController
13
+public class OptionController {
14
+
15
+    private OptionRepository optionRepository;
16
+
17
+    @Autowired
18
+    public OptionController(OptionRepository optionRepository) {
19
+        this.optionRepository = optionRepository;
20
+    }
21
+
22
+    @RequestMapping(value="/options", method= RequestMethod.GET)
23
+    public ResponseEntity<?> returnAllOptions() {
24
+        Iterable<Option> options = optionRepository.getAllOptions();
25
+        options.forEach(opt -> System.out.println(opt.toString()));
26
+        return new ResponseEntity<>(options, HttpStatus.OK);
27
+    }
28
+}

+ 2
- 4
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Прегледај датотеку

@@ -10,7 +10,6 @@ import org.springframework.http.ResponseEntity;
10 10
 import org.springframework.web.bind.annotation.*;
11 11
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
12 12
 
13
-import javax.validation.constraints.Null;
14 13
 import java.net.URI;
15 14
 
16 15
 @RestController
@@ -28,7 +27,7 @@ public class PollController {
28 27
         return new ResponseEntity<>(allPolls, HttpStatus.OK);
29 28
     }
30 29
 
31
-    @RequestMapping(method = RequestMethod.POST)
30
+    @RequestMapping(value="/polls", method = RequestMethod.POST)
32 31
     public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
33 32
         URI newPollUri = ServletUriComponentsBuilder
34 33
                 .fromCurrentRequest()
@@ -64,8 +63,7 @@ public class PollController {
64 63
         return new ResponseEntity<>(HttpStatus.OK);
65 64
     }
66 65
 
67
-//    @RequestMapping(method = RequestMethod.GET)
68
-    public void verifyPoll(Long pollId) throws ResourceNotFoundException {
66
+    private void verifyPoll(Long pollId) throws ResourceNotFoundException {
69 67
         Poll verify = pollRepository.findOne(pollId);
70 68
         if (verify == null) {
71 69
             throw new ResourceNotFoundException();

+ 3
- 6
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Прегледај датотеку

@@ -5,15 +5,12 @@ import javax.persistence.*;
5 5
 @Entity
6 6
 public class Option {
7 7
 
8
-    @Id
9
-    @GeneratedValue
10
-    @Column(name = "OPTION_ID")
11
-    Long id;
8
+    private @Id @GeneratedValue @Column(name = "OPTION_ID") Long id;
12 9
 
13
-    @Column(name = "OPTION_VALUE")
14
-    String value;
10
+    private @Column(name = "OPTION_VALUE") String value;
15 11
 
16 12
     public Long getId() {
13
+        System.out.println("Returning option ID: " + id.toString());
17 14
         return id;
18 15
     }
19 16
 

+ 53
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/error/ErrorDetail.java Прегледај датотеку

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

+ 5
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Прегледај датотеку

@@ -1,8 +1,12 @@
1 1
 package io.zipcoder.tc_spring_poll_application.repositories;
2 2
 
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Option;
4
+import org.springframework.data.jpa.repository.Query;
4 5
 import org.springframework.data.repository.CrudRepository;
5 6
 
6 7
 public interface OptionRepository extends CrudRepository<Option, Long> {
7
-
8
+    @Query(value =
9
+            "SELECT * " +
10
+            "FROM Option", nativeQuery = true)
11
+    public Iterable<Option> getAllOptions();
8 12
 }