|
1 | | -package com.example.postgresdemo.controller; |
2 | | - |
3 | | -import com.example.postgresdemo.exception.ResourceNotFoundException; |
4 | | -import com.example.postgresdemo.model.Answer; |
5 | | -import com.example.postgresdemo.repository.AnswerRepository; |
6 | | -import com.example.postgresdemo.repository.QuestionRepository; |
7 | | -import org.springframework.beans.factory.annotation.Autowired; |
8 | | -import org.springframework.http.ResponseEntity; |
9 | | -import org.springframework.web.bind.annotation.*; |
10 | | -import javax.validation.Valid; |
11 | | -import java.util.List; |
12 | | - |
13 | | -@RestController |
14 | | -public class AnswerController { |
15 | | - |
16 | | - @Autowired |
17 | | - private AnswerRepository answerRepository; |
18 | | - |
19 | | - @Autowired |
20 | | - private QuestionRepository questionRepository; |
21 | | - |
22 | | - @GetMapping("/questions/{questionId}/answers") |
23 | | - public List<Answer> getAnswersByQuestionId(@PathVariable Long questionId) { |
24 | | - return answerRepository.findByQuestionId(questionId); |
25 | | - } |
26 | | - |
27 | | - @PostMapping("/questions/{questionId}/answers") |
28 | | - public Answer addAnswer(@PathVariable Long questionId, |
29 | | - @Valid @RequestBody Answer answer) { |
30 | | - return questionRepository.findById(questionId) |
31 | | - .map(question -> { |
32 | | - answer.setQuestion(question); |
33 | | - return answerRepository.save(answer); |
34 | | - }).orElseThrow(() -> new ResourceNotFoundException("Question not found with id " + questionId)); |
35 | | - } |
36 | | - |
37 | | - @PutMapping("/questions/{questionId}/answers/{answerId}") |
38 | | - public Answer updateAnswer(@PathVariable Long questionId, |
39 | | - @PathVariable Long answerId, |
40 | | - @Valid @RequestBody Answer answerRequest) { |
41 | | - if(!questionRepository.existsById(questionId)) { |
42 | | - throw new ResourceNotFoundException("Question not found with id " + questionId); |
43 | | - } |
44 | | - |
45 | | - return answerRepository.findById(answerId) |
46 | | - .map(answer -> { |
47 | | - answer.setText(answerRequest.getText()); |
48 | | - return answerRepository.save(answer); |
49 | | - }).orElseThrow(() -> new ResourceNotFoundException("Answer not found with id " + answerId)); |
50 | | - } |
51 | | - |
52 | | - @DeleteMapping("/questions/{questionId}/answers/{answerId}") |
53 | | - public ResponseEntity<?> deleteAnswer(@PathVariable Long questionId, |
54 | | - @PathVariable Long answerId) { |
55 | | - if(!questionRepository.existsById(questionId)) { |
56 | | - throw new ResourceNotFoundException("Question not found with id " + questionId); |
57 | | - } |
58 | | - |
59 | | - return answerRepository.findById(answerId) |
60 | | - .map(answer -> { |
61 | | - answerRepository.delete(answer); |
62 | | - return ResponseEntity.ok().build(); |
63 | | - }).orElseThrow(() -> new ResourceNotFoundException("Answer not found with id " + answerId)); |
64 | | - |
65 | | - } |
66 | | -} |
| 1 | +package com.example.postgresdemo.controller; |
| 2 | + |
| 3 | +import com.example.postgresdemo.exception.ResourceNotFoundException; |
| 4 | +import com.example.postgresdemo.model.Answer; |
| 5 | +import com.example.postgresdemo.repository.AnswerRepository; |
| 6 | +import com.example.postgresdemo.repository.QuestionRepository; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.http.ResponseEntity; |
| 9 | +import org.springframework.web.bind.annotation.*; |
| 10 | +import javax.validation.Valid; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +@RestController |
| 14 | +public class AnswerController { |
| 15 | + |
| 16 | + @Autowired |
| 17 | + private AnswerRepository answerRepository; |
| 18 | + |
| 19 | + @Autowired |
| 20 | + private QuestionRepository questionRepository; |
| 21 | + |
| 22 | + @GetMapping("/questions/{questionId}/answers") |
| 23 | + public List<Answer> getAnswersByQuestionId(@PathVariable Long questionId) { |
| 24 | + return answerRepository.findByQuestionId(questionId); |
| 25 | + } |
| 26 | + |
| 27 | + @PostMapping("/questions/{questionId}/answers") |
| 28 | + public Answer addAnswer(@PathVariable Long questionId, |
| 29 | + @Valid @RequestBody Answer answer) { |
| 30 | + return questionRepository.findById(questionId) |
| 31 | + .map(question -> { |
| 32 | + answer.setQuestion(question); |
| 33 | + return answerRepository.save(answer); |
| 34 | + }).orElseThrow(() -> new ResourceNotFoundException("Question not found with id " + questionId)); |
| 35 | + } |
| 36 | + |
| 37 | + @PutMapping("/questions/{questionId}/answers/{answerId}") |
| 38 | + public Answer updateAnswer(@PathVariable Long questionId, |
| 39 | + @PathVariable Long answerId, |
| 40 | + @Valid @RequestBody Answer answerRequest) { |
| 41 | + if(!questionRepository.existsById(questionId)) { |
| 42 | + throw new ResourceNotFoundException("Question not found with id " + questionId); |
| 43 | + } |
| 44 | + |
| 45 | + return answerRepository.findById(answerId) |
| 46 | + .map(answer -> { |
| 47 | + answer.setText(answerRequest.getText()); |
| 48 | + return answerRepository.save(answer); |
| 49 | + }).orElseThrow(() -> new ResourceNotFoundException("Answer not found with id " + answerId)); |
| 50 | + } |
| 51 | + |
| 52 | + @DeleteMapping("/questions/{questionId}/answers/{answerId}") |
| 53 | + public ResponseEntity<?> deleteAnswer(@PathVariable Long questionId, |
| 54 | + @PathVariable Long answerId) { |
| 55 | + if(!questionRepository.existsById(questionId)) { |
| 56 | + throw new ResourceNotFoundException("Question not found with id " + questionId); |
| 57 | + } |
| 58 | + |
| 59 | + return answerRepository.findById(answerId) |
| 60 | + .map(answer -> { |
| 61 | + answerRepository.delete(answer); |
| 62 | + return ResponseEntity.ok().build(); |
| 63 | + }).orElseThrow(() -> new ResourceNotFoundException("Answer not found with id " + answerId)); |
| 64 | + |
| 65 | + } |
| 66 | +} |
0 commit comments