Explorar el Código

could post a poll, but not a specific vote

ahsonali hace 8 años
padre
commit
b4a79d93be

+ 77
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Ver fichero

@@ -0,0 +1,77 @@
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
+
13
+import javax.inject.Inject;
14
+import java.net.URI;
15
+
16
+@RestController
17
+public class PollController {
18
+
19
+    @Inject
20
+    private PollRepository pollRepository;
21
+
22
+    @Autowired
23
+    PollController(PollRepository pollRepository)
24
+    {
25
+        this.pollRepository = pollRepository;
26
+    }
27
+
28
+    @RequestMapping(value="/polls", method=RequestMethod.GET)
29
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
30
+        Iterable<Poll> allPolls = pollRepository.findAll();
31
+        return new ResponseEntity<>(pollRepository.findAll(), HttpStatus.OK);
32
+    }
33
+
34
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
35
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
36
+        poll = pollRepository.save(poll);
37
+
38
+        HttpHeaders responseHeaders = new HttpHeaders();
39
+        URI newPollUri = ServletUriComponentsBuilder
40
+                .fromCurrentRequest()
41
+                .path("/{id}")
42
+                .buildAndExpand(poll.getId())
43
+                .toUri();
44
+        responseHeaders.setLocation(newPollUri);
45
+
46
+        return new ResponseEntity<>(null, HttpStatus.CREATED);
47
+
48
+        }
49
+
50
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
51
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
52
+        Poll p = pollRepository.findOne(pollId);
53
+        return new ResponseEntity<> (p, HttpStatus.OK);
54
+    }
55
+
56
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
57
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
58
+        // Save the entity
59
+        Poll p = pollRepository.save(poll);
60
+        return new ResponseEntity<>(HttpStatus.OK);
61
+    }
62
+
63
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
64
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
65
+        pollRepository.delete(pollId);
66
+        return new ResponseEntity<>(HttpStatus.OK);
67
+    }
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+}

+ 35
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Ver fichero

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

+ 34
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Ver fichero

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

+ 46
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Ver fichero

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

+ 32
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Ver fichero

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

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Ver fichero

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

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Ver fichero

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

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Ver fichero

@@ -0,0 +1,8 @@
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
+
8
+}