瀏覽代碼

Poll Controller

Nick Satinover 6 年之前
父節點
當前提交
5ca7e22612

+ 2
- 1
pom.xml 查看文件

@@ -15,7 +15,8 @@
15 15
     </parent>
16 16
     <properties>
17 17
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18
-        <start-class>io.zipcoder.tc_spring_poll_application.QuickPollApplication</start-class>
18
+        <start-class>io.zipcoder.springdemo.QuickPollApplication</start-class>
19
+        <!--<start-class>io.zipcoder.tc_spring_poll_application.QuickPollApplication</start-class> -->
19 20
         <java.version>1.7</java.version>
20 21
     </properties>
21 22
     <dependencies>

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

@@ -0,0 +1,29 @@
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.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 PollController {
14
+
15
+    @Autowired
16
+    PollRepository pollRepository;
17
+
18
+    PollController(@Autowired PollRepository pollRepository){
19
+        this.pollRepository = pollRepository;
20
+    }
21
+
22
+    @RequestMapping(value = "/polls", method = RequestMethod.GET)
23
+    public ResponseEntity<Iterable<Poll>> getAllPolls(){
24
+        Iterable<Poll> allPolls = pollRepository.findAll();
25
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
26
+    }
27
+
28
+
29
+}

+ 32
- 0
src/test/java/io/zipcoder/tc_spring_poll_application/domain/Option.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 Option {
7
+
8
+    @Id
9
+    @GeneratedValue(strategy = GenerationType.AUTO)
10
+    @Column(name = "OPTION_ID")
11
+    private Long id;
12
+    @Column(name = "OPTION_VALUE")
13
+    private String value;
14
+
15
+    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 String getValue() {
26
+        return value;
27
+    }
28
+
29
+    public void setValue(String value) {
30
+        this.value = value;
31
+    }
32
+}

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

@@ -0,0 +1,48 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.*;
4
+import java.util.Set;
5
+
6
+@Entity
7
+public class Poll {
8
+
9
+    @Id
10
+    @GeneratedValue(strategy = GenerationType.AUTO)
11
+    @Column(name = "POLL_ID")
12
+    private Long id;
13
+
14
+    @Column(name = "QUESTION")
15
+    private String question;
16
+
17
+    @OneToMany(cascade = CascadeType.ALL)
18
+    @JoinColumn(name = "POLL_ID")
19
+    @OrderBy
20
+    private Set<Option> options;
21
+
22
+    public Poll() {
23
+    }
24
+
25
+    public Long getId() {
26
+        return id;
27
+    }
28
+
29
+    public void setId(Long id) {
30
+        this.id = id;
31
+    }
32
+
33
+    public String getQuestion() {
34
+        return question;
35
+    }
36
+
37
+    public void setQuestion(String question) {
38
+        this.question = question;
39
+    }
40
+
41
+    public Set<Option> getOptions() {
42
+        return options;
43
+    }
44
+
45
+    public void setOptions(Set<Option> options) {
46
+        this.options = options;
47
+    }
48
+}

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

@@ -0,0 +1,35 @@
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(strategy = GenerationType.AUTO)
10
+    @Column(name = "VOTE_ID")
11
+    private Long id;
12
+
13
+    @ManyToOne
14
+    @JoinColumn(name = "OPTION_ID")
15
+    private Option option;
16
+
17
+    public Vote() {
18
+    }
19
+
20
+    public Long getId() {
21
+        return id;
22
+    }
23
+
24
+    public void setId(Long id) {
25
+        this.id = id;
26
+    }
27
+
28
+    public Option getOption() {
29
+        return option;
30
+    }
31
+
32
+    public void setOption(Option option) {
33
+        this.option = option;
34
+    }
35
+}

+ 9
- 0
src/test/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java 查看文件

@@ -0,0 +1,9 @@
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
+import org.springframework.stereotype.Repository;
6
+
7
+@Repository
8
+public interface OptionRepository extends CrudRepository<Option, Long> {
9
+}

+ 9
- 0
src/test/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java 查看文件

@@ -0,0 +1,9 @@
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
+import org.springframework.stereotype.Repository;
6
+
7
+@Repository
8
+public interface PollRepository extends CrudRepository<Poll, Long> {
9
+}

+ 8
- 0
src/test/java/io/zipcoder/tc_spring_poll_application/repositories/Vote.java 查看文件

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