瀏覽代碼

some changes

William Brown 6 年之前
父節點
當前提交
8ba94c5cab

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

@@ -0,0 +1,44 @@
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.repository.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.RequestBody;
9
+import org.springframework.web.bind.annotation.RequestMapping;
10
+import org.springframework.web.bind.annotation.RequestMethod;
11
+import org.springframework.web.bind.annotation.RestController;
12
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
13
+
14
+import java.net.URI;
15
+
16
+@RestController
17
+public class PollController {
18
+
19
+    private PollRepository pollRepository;
20
+
21
+    @Autowired
22
+    public PollController(PollRepository pollRepository) {
23
+        this.pollRepository = pollRepository;
24
+    }
25
+
26
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
27
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
28
+        Iterable<Poll> allPolls = pollRepository.findAll();
29
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
30
+    }
31
+
32
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
33
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
34
+        poll = pollRepository.save(poll);
35
+
36
+        URI newPollUri = ServletUriComponentsBuilder
37
+                .fromCurrentRequest()
38
+                .path("/{id}")
39
+                .buildAndExpand(poll.getId())
40
+                .toUri();
41
+
42
+        return new ResponseEntity<>(newPollUri, HttpStatus.CREATED);
43
+    }
44
+}

+ 34
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java 查看文件

@@ -0,0 +1,34 @@
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
+    @Id
12
+    @GeneratedValue
13
+    @Column(name = "OPTION_ID")
14
+    private long id;
15
+
16
+    @Column(name = "OPTION_VALUE")
17
+    private String value;
18
+
19
+    public long getId() {
20
+        return id;
21
+    }
22
+
23
+    public void setId(long id) {
24
+        this.id = id;
25
+    }
26
+
27
+    public String getValue() {
28
+        return value;
29
+    }
30
+
31
+    public void setValue(String value) {
32
+        this.value = value;
33
+    }
34
+}

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

@@ -0,0 +1,45 @@
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
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 long getId() {
23
+        return id;
24
+    }
25
+
26
+    public void setId(long id) {
27
+        this.id = id;
28
+    }
29
+
30
+    public String getQuestion() {
31
+        return question;
32
+    }
33
+
34
+    public void setQuestion(String question) {
35
+        this.question = question;
36
+    }
37
+
38
+    public Set<Option> getOptions() {
39
+        return options;
40
+    }
41
+
42
+    public void setOptions(Set<Option> options) {
43
+        this.options = options;
44
+    }
45
+}

+ 32
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.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 Vote {
7
+
8
+    @Id
9
+    @GeneratedValue
10
+    @Column(name = "VOTE_ID")
11
+    private Long id;
12
+
13
+    @ManyToOne
14
+    @JoinColumn(name = "OPTION_ID")
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
+}

+ 10
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repository/OptionRepository.java 查看文件

@@ -0,0 +1,10 @@
1
+package io.zipcoder.tc_spring_poll_application.repository;
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
+
8
+
9
+
10
+}

+ 10
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repository/PollRepository.java 查看文件

@@ -0,0 +1,10 @@
1
+package io.zipcoder.tc_spring_poll_application.repository;
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
+
8
+
9
+
10
+}

+ 10
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repository/VoteRepository.java 查看文件

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