#24 jaejoson SpringQuickPoll

Открыто
jaejoson хочет смерджить 2 коммит(ов) из jaejoson/SpringQuickPoll:master в master

+ 62
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Просмотреть файл

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.HttpHeaders;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.RequestEntity;
9
+import org.springframework.http.ResponseEntity;
10
+import org.springframework.web.bind.annotation.*;
11
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
12
+
13
+import java.net.URI;
14
+
15
+@RestController
16
+public class PollController {
17
+    PollRepository pollRepository;
18
+
19
+    @Autowired
20
+    public PollController(PollRepository pollRepository) {
21
+        this.pollRepository = pollRepository;
22
+    }
23
+
24
+    @GetMapping("/getAllPolls")
25
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
26
+        Iterable<Poll> allPolls = pollRepository.findAll();
27
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
28
+    }
29
+
30
+    @PostMapping("/polls")
31
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
32
+        poll = pollRepository.save(poll);
33
+
34
+        URI newPollUri = ServletUriComponentsBuilder
35
+                .fromCurrentRequest()
36
+                .path("/{id}")
37
+                .buildAndExpand(poll.getId())
38
+                .toUri();
39
+
40
+        return new ResponseEntity<>(newPollUri, HttpStatus.CREATED);
41
+    }
42
+
43
+    @GetMapping("/polls/{pollId}")
44
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
45
+        Poll p = pollRepository.findOne(pollId);
46
+        return new ResponseEntity<>(p, HttpStatus.OK);
47
+    }
48
+
49
+    @PutMapping("/polls/{pollId}")
50
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
51
+        Poll p = pollRepository.save(poll);
52
+        return new ResponseEntity<>(HttpStatus.OK);
53
+    }
54
+
55
+    @DeleteMapping("/polls/{pollId}")
56
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
57
+        pollRepository.delete(pollId);
58
+        return new ResponseEntity<>(HttpStatus.OK);
59
+    }
60
+
61
+
62
+}

+ 34
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Просмотреть файл

1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.*;
4
+
5
+@Entity
6
+public class Option {
7
+    @Id
8
+    @GeneratedValue(strategy = GenerationType.AUTO)
9
+    @Column(name = "OPTION_ID")
10
+    private Long id;
11
+
12
+    @Column(name = "OPTION_VALUE")
13
+    private String value;
14
+
15
+    public Option(String value) {
16
+        this.value = value;
17
+    }
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
+}

+ 44
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Просмотреть файл

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
+    @Id
9
+    @GeneratedValue(strategy = GenerationType.AUTO)
10
+    @Column(name = "POLL_ID")
11
+    private Long id;
12
+
13
+    @Column(name = "POLL_QUESTION")
14
+    private String question;
15
+
16
+    @OneToMany(cascade = CascadeType.ALL)
17
+            @JoinColumn(name = "POLL_ID")
18
+            @OrderBy
19
+    Set <Option> options;
20
+
21
+    public Long getId() {
22
+        return id;
23
+    }
24
+
25
+    public void setId(Long id) {
26
+        this.id = id;
27
+    }
28
+
29
+    public String getQuestion() {
30
+        return question;
31
+    }
32
+
33
+    public void setQuestion(String question) {
34
+        this.question = question;
35
+    }
36
+
37
+    public Set<Option> getOptions() {
38
+        return options;
39
+    }
40
+
41
+    public void setOptions(Set<Option> options) {
42
+        this.options = options;
43
+    }
44
+}

+ 32
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Просмотреть файл

1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+
4
+import javax.persistence.*;
5
+
6
+@Entity
7
+public class Vote {
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 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
+}

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Просмотреть файл

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
+}

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Просмотреть файл

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
+}

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Просмотреть файл

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
+}