瀏覽代碼

first commit

Vincent Gasbarro 8 年之前
父節點
當前提交
5fb7afb050

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

@@ -0,0 +1,60 @@
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.HttpHeaders;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.*;
9
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
10
+
11
+import javax.inject.Inject;
12
+import java.net.URI;
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/{pollId}", method=RequestMethod.GET)
27
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
28
+        Poll p = pollRepository.findOne(pollId);
29
+        return new ResponseEntity<>(p, 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
+        HttpHeaders headers = new HttpHeaders();
37
+        URI newPollUri = ServletUriComponentsBuilder
38
+                .fromCurrentRequest()
39
+                .path("/{id}")
40
+                .buildAndExpand(poll.getId())
41
+                .toUri();
42
+
43
+        headers.setLocation(newPollUri);
44
+
45
+        return new ResponseEntity<>(null, headers, HttpStatus.CREATED);
46
+    }
47
+
48
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
49
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
50
+        Poll p = pollRepository.save(poll);
51
+        return new ResponseEntity<>(HttpStatus.OK);
52
+    }
53
+
54
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
55
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
56
+        pollRepository.delete(pollId);
57
+        return new ResponseEntity<>(HttpStatus.OK);
58
+    }
59
+
60
+}

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

@@ -0,0 +1,31 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+
4
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
5
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
6
+import org.springframework.http.HttpHeaders;
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 javax.inject.Inject;
13
+
14
+@RestController
15
+public class VoteController {
16
+    @Inject
17
+    private VoteRepository voteRepository;
18
+
19
+    @RequestMapping(value="/polls/{pollId}/votes", method= RequestMethod.POST)
20
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote vote) {
21
+        vote = voteRepository.save(vote);
22
+        HttpHeaders responseHeaders = new HttpHeaders();
23
+
24
+        responseHeaders.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
25
+        .buildAndExpand(vote.getId()).toUri());
26
+
27
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
28
+    }
29
+
30
+
31
+}

+ 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 String getValue() {
24
+        return value;
25
+    }
26
+
27
+    public void setId(long id) {
28
+        this.id = id;
29
+    }
30
+
31
+    public void setValue(String value) {
32
+        this.value = value;
33
+    }
34
+}

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

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

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

@@ -0,0 +1,33 @@
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
+
18
+    public long getId() {
19
+        return id;
20
+    }
21
+
22
+    public void setId(long id) {
23
+        this.id = id;
24
+    }
25
+
26
+    public Option getOption() {
27
+        return option;
28
+    }
29
+
30
+    public void setOption(Option option) {
31
+        this.option = option;
32
+    }
33
+}

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

@@ -0,0 +1,8 @@
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
+
6
+public interface OptionRepository extends CrudRepository<Option, Long> {
7
+
8
+}

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

@@ -0,0 +1,8 @@
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
+
6
+public interface PollRepository extends CrudRepository<Poll, Long> {
7
+
8
+}

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

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