Explorar el Código

Part 3.1 done

Trinh Tong hace 7 años
padre
commit
d2cf4399ef

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

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

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

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
+    private VoteRepository voteRepository;
15
+
16
+    @Autowired
17
+    public VoteController(VoteRepository voteRepository) {
18
+        this.voteRepository = voteRepository;
19
+    }
20
+
21
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
22
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
23
+            vote) {
24
+        vote = voteRepository.save(vote);
25
+        // Set the headers for the newly created resource
26
+        HttpHeaders responseHeaders = new HttpHeaders();
27
+        responseHeaders.setLocation(ServletUriComponentsBuilder.
28
+                fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
29
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
30
+    }
31
+
32
+}

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

1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.*;
4
+
5
+@Entity
6
+public class Option {
7
+
8
+    @Id
9
+    @GeneratedValue
10
+    @Column(name = "OPTION_ID")
11
+    Long id;
12
+
13
+    @Column(name = "OPTION_VALUE")
14
+    String value;
15
+
16
+    public Long getId() {
17
+        return id;
18
+    }
19
+
20
+    public void setId(Long id) {
21
+        this.id = id;
22
+    }
23
+
24
+    public String getValue() {
25
+        return value;
26
+    }
27
+
28
+    public void setValue(String value) {
29
+        this.value = value;
30
+    }
31
+}

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

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

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

1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.*;
4
+
5
+@Entity
6
+public class Vote {
7
+    @Id
8
+    @GeneratedValue
9
+    @Column(name = "VOTE_ID")
10
+    Long id;
11
+
12
+    @ManyToOne
13
+    @JoinColumn(name = "OPTION_ID")
14
+    Option option;
15
+
16
+    public Long getId() {
17
+        return id;
18
+    }
19
+
20
+    public void setId(Long id) {
21
+        this.id = id;
22
+    }
23
+
24
+    public Option getOption() {
25
+        return option;
26
+    }
27
+
28
+    public void setOption(Option option) {
29
+        this.option = option;
30
+    }
31
+}

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

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 Ver fichero

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

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

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