Sfoglia il codice sorgente

build process working, adding rest methods to controller

Patrick Glavin 8 anni fa
parent
commit
76abffa831

+ 32
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Vedi File

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.repositories.PollRepository;
5
+import org.springframework.http.HttpStatus;
6
+import org.springframework.http.ResponseEntity;
7
+import org.springframework.web.bind.annotation.RequestBody;
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
+import javax.inject.Inject;
13
+
14
+@RestController
15
+public class PollController {
16
+
17
+    @Inject
18
+    private PollRepository pollRepository;
19
+
20
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
21
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
22
+        Iterable<Poll> allPolls = pollRepository.findAll();
23
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
24
+    }
25
+
26
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
27
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
28
+        poll = pollRepository.save(poll);
29
+        return new ResponseEntity<>(null, HttpStatus.CREATED);
30
+    }
31
+
32
+}

+ 9
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Vedi File

7
 
7
 
8
 @Entity
8
 @Entity
9
 public class Option {
9
 public class Option {
10
+
10
     @Id
11
     @Id
11
     @GeneratedValue
12
     @GeneratedValue
12
     @Column(name = "OPTION_ID")
13
     @Column(name = "OPTION_ID")
13
-    Long id;
14
+    private Long id;
14
 
15
 
15
     @Column(name = "OPTION_VALUE")
16
     @Column(name = "OPTION_VALUE")
16
-    String value;
17
+    private String value;
17
 
18
 
18
     public Long getId() {
19
     public Long getId() {
19
         return id;
20
         return id;
30
     public void setValue(String value) {
31
     public void setValue(String value) {
31
         this.value = value;
32
         this.value = value;
32
     }
33
     }
33
-}
34
+
35
+    @Override
36
+    public String toString() {
37
+        return getId() + "," + getValue();
38
+    }
39
+}

+ 15
- 4
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Vedi File

1
 package io.zipcoder.tc_spring_poll_application.domain;
1
 package io.zipcoder.tc_spring_poll_application.domain;
2
 
2
 
3
+import org.hibernate.validator.constraints.NotEmpty;
4
+
3
 import javax.persistence.*;
5
 import javax.persistence.*;
6
+import javax.validation.constraints.Size;
4
 import java.util.Set;
7
 import java.util.Set;
5
 
8
 
6
 @Entity
9
 @Entity
7
 public class Poll {
10
 public class Poll {
11
+
8
     @Id
12
     @Id
9
     @GeneratedValue
13
     @GeneratedValue
10
     @Column(name = "POLL_ID")
14
     @Column(name = "POLL_ID")
11
-    Long id;
15
+    private Long id;
12
 
16
 
13
     @Column(name = "QUESTION")
17
     @Column(name = "QUESTION")
14
-    String question;
18
+    @NotEmpty
19
+    private String question;
15
 
20
 
16
     @OneToMany(cascade = CascadeType.ALL)
21
     @OneToMany(cascade = CascadeType.ALL)
17
     @JoinColumn(name = "POLL_ID")
22
     @JoinColumn(name = "POLL_ID")
18
     @OrderBy
23
     @OrderBy
19
-    Set<Option> options;
24
+    @Size(min=2, max = 6)
25
+    private Set<Option> options;
20
 
26
 
21
     public Long getId() {
27
     public Long getId() {
22
         return id;
28
         return id;
41
     public void setOptions(Set<Option> options) {
47
     public void setOptions(Set<Option> options) {
42
         this.options = options;
48
         this.options = options;
43
     }
49
     }
44
-}
50
+
51
+    @Override
52
+    public String toString() {
53
+        return getId() + ", " + getQuestion() + ", " + getOptions();
54
+    }
55
+}

+ 10
- 4
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Vedi File

4
 
4
 
5
 @Entity
5
 @Entity
6
 public class Vote {
6
 public class Vote {
7
+
7
     @Id
8
     @Id
8
     @GeneratedValue
9
     @GeneratedValue
9
     @Column(name = "VOTE_ID")
10
     @Column(name = "VOTE_ID")
10
-    Long id;
11
+    private Long id;
11
 
12
 
12
-    @ManyToMany
13
+    @ManyToOne
13
     @JoinColumn(name = "OPTION_ID")
14
     @JoinColumn(name = "OPTION_ID")
14
-    Option option;
15
+    private Option option;
15
 
16
 
16
     public Long getId() {
17
     public Long getId() {
17
         return id;
18
         return id;
28
     public void setOption(Option option) {
29
     public void setOption(Option option) {
29
         this.option = option;
30
         this.option = option;
30
     }
31
     }
31
-}
32
+
33
+    @Override
34
+    public String toString() {
35
+        return getId() + ", " + getOption();
36
+    }
37
+}