Kibret Tecle 8 лет назад
Родитель
Сommit
d63523889a

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

@@ -0,0 +1,64 @@
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.ResponseEntity;
9
+import org.springframework.web.bind.annotation.*;
10
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11
+
12
+import javax.inject.Inject;
13
+import java.net.URI;
14
+
15
+@RestController
16
+public class PollController {
17
+
18
+    @Inject
19
+    private PollRepository pollRepository;
20
+
21
+    @Autowired
22
+    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
+        HttpHeaders responseHeaders = new HttpHeaders();
36
+        URI newPollUri = ServletUriComponentsBuilder
37
+                .fromCurrentRequest()
38
+                .path("/{id}")
39
+                .buildAndExpand(poll.getId())
40
+                .toUri();
41
+        responseHeaders.setLocation(newPollUri);
42
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
43
+
44
+    }
45
+
46
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
47
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
48
+        Poll p = pollRepository.findOne(pollId);
49
+        return new ResponseEntity<> (p, HttpStatus.OK);
50
+    }
51
+
52
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
53
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
54
+        // Save the entity
55
+        Poll p = pollRepository.save(poll);
56
+        return new ResponseEntity<>(HttpStatus.OK);
57
+    }
58
+
59
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
60
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
61
+        pollRepository.delete(pollId);
62
+        return new ResponseEntity<>(HttpStatus.OK);
63
+    }
64
+}

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

@@ -0,0 +1,33 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
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.ResponseEntity;
9
+import org.springframework.web.bind.annotation.*;
10
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11
+
12
+@RestController
13
+public class VoteController {
14
+
15
+    private VoteRepository voteRepository;
16
+
17
+    @Autowired
18
+    public VoteController(VoteRepository voteRepository){
19
+        this.voteRepository = voteRepository;
20
+    }
21
+
22
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
23
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
24
+            vote) {
25
+        vote = voteRepository.save(vote);
26
+        // Set the headers for the newly created resource
27
+        HttpHeaders responseHeaders = new HttpHeaders();
28
+        responseHeaders.setLocation(ServletUriComponentsBuilder.
29
+                fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
30
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
31
+    }
32
+
33
+}

+ 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
+    @Column(name = "OPTION_VALUE")
16
+    private String value;
17
+
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
+}

+ 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
+    @Column(name = "QUESTION")
14
+    private String question;
15
+
16
+    @OneToMany(cascade = CascadeType.ALL)
17
+    @JoinColumn(name = "POLL_ID")
18
+    @OrderBy
19
+    private Set<Option> options;
20
+
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
+}

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

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

@@ -0,0 +1,7 @@
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 Просмотреть файл

@@ -0,0 +1,7 @@
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 Просмотреть файл

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

+ 1
- 1
src/test/java/io/zipcoder/tc_spring_poll_application/QuickPollApplicationTest.java Просмотреть файл

@@ -1,4 +1,4 @@
1 1
 package io.zipcoder.tc_spring_poll_application;
2 2
 
3
-public class QuickPollApplicationTest {
3
+public class  QuickPollApplicationTest {
4 4
 }