Skip to content

Commit 863a8a8

Browse files
authored
Merge pull request #4 from ZaharKalosha/feature/question-controller-refactoring
Add question DTOs and move all business logic from Controller to service
2 parents 095cf1c + f173b88 commit 863a8a8

File tree

11 files changed

+719
-452
lines changed

11 files changed

+719
-452
lines changed
Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,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-
}
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+
}
Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,40 @@
1-
package com.example.postgresdemo.controller;
2-
3-
import com.example.postgresdemo.exception.ResourceNotFoundException;
4-
import com.example.postgresdemo.model.Question;
5-
import com.example.postgresdemo.repository.QuestionRepository;
6-
import org.springframework.beans.factory.annotation.Autowired;
7-
import org.springframework.data.domain.Page;
8-
import org.springframework.data.domain.Pageable;
9-
import org.springframework.http.ResponseEntity;
10-
import org.springframework.web.bind.annotation.*;
11-
import javax.validation.Valid;
12-
13-
@RestController
14-
public class QuestionController {
15-
16-
@Autowired
17-
private QuestionRepository questionRepository;
18-
19-
@GetMapping("/questions")
20-
public Page<Question> getQuestions(Pageable pageable) {
21-
return questionRepository.findAll(pageable);
22-
}
23-
24-
25-
@PostMapping("/questions")
26-
public Question createQuestion(@Valid @RequestBody Question question) {
27-
return questionRepository.save(question);
28-
}
29-
30-
@PutMapping("/questions/{questionId}")
31-
public Question updateQuestion(@PathVariable Long questionId,
32-
@Valid @RequestBody Question questionRequest) {
33-
return questionRepository.findById(questionId)
34-
.map(question -> {
35-
question.setTitle(questionRequest.getTitle());
36-
question.setDescription(questionRequest.getDescription());
37-
return questionRepository.save(question);
38-
}).orElseThrow(() -> new ResourceNotFoundException("Question not found with id " + questionId));
39-
}
40-
41-
42-
@DeleteMapping("/questions/{questionId}")
43-
public ResponseEntity<?> deleteQuestion(@PathVariable Long questionId) {
44-
return questionRepository.findById(questionId)
45-
.map(question -> {
46-
questionRepository.delete(question);
47-
return ResponseEntity.ok().build();
48-
}).orElseThrow(() -> new ResourceNotFoundException("Question not found with id " + questionId));
49-
}
50-
}
1+
package com.example.postgresdemo.controller;
2+
3+
import com.example.postgresdemo.model.QuestionRequestDTO;
4+
import com.example.postgresdemo.model.QuestionResponseDTO;
5+
import com.example.postgresdemo.service.QuestionService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.data.domain.Page;
8+
import org.springframework.data.domain.Pageable;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.*;
11+
12+
import javax.validation.Valid;
13+
14+
@RestController
15+
16+
public class QuestionController {
17+
@Autowired
18+
private QuestionService questionService;
19+
20+
@GetMapping("/questions")
21+
public Page<QuestionResponseDTO> getQuestions(Pageable pageable) {
22+
return questionService.findAll(pageable);
23+
}
24+
25+
@PostMapping("/questions")
26+
public QuestionResponseDTO createQuestion(@Valid @RequestBody QuestionRequestDTO question) {
27+
return questionService.create(question);
28+
}
29+
30+
@PutMapping("/questions/{questionId}")
31+
public QuestionResponseDTO updateQuestion(@PathVariable Long questionId,
32+
@Valid @RequestBody QuestionRequestDTO questionRequest) {
33+
return questionService.update(questionId, questionRequest);
34+
}
35+
36+
@DeleteMapping("/questions/{questionId}")
37+
public void deleteQuestion(@PathVariable Long questionId) {
38+
questionService.delete(questionId);
39+
}
40+
}
Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
package com.example.postgresdemo.model;
2-
3-
import com.fasterxml.jackson.annotation.JsonIgnore;
4-
import org.hibernate.annotations.OnDelete;
5-
import org.hibernate.annotations.OnDeleteAction;
6-
7-
import javax.persistence.*;
8-
9-
@Entity
10-
@Table(name = "answers")
11-
public class Answer extends AuditModel {
12-
@Id
13-
@GeneratedValue(generator = "answer_generator")
14-
@SequenceGenerator(
15-
name = "answer_generator",
16-
sequenceName = "answer_sequence",
17-
initialValue = 1000
18-
)
19-
private Long id;
20-
21-
@Column(columnDefinition = "text")
22-
private String text;
23-
24-
@ManyToOne(fetch = FetchType.LAZY, optional = false)
25-
@JoinColumn(name = "question_id", nullable = false)
26-
@OnDelete(action = OnDeleteAction.CASCADE)
27-
@JsonIgnore
28-
private Question question;
29-
30-
public Long getId() {
31-
return id;
32-
}
33-
34-
public void setId(Long id) {
35-
this.id = id;
36-
}
37-
38-
public String getText() {
39-
return text;
40-
}
41-
42-
public void setText(String text) {
43-
this.text = text;
44-
}
45-
46-
public Question getQuestion() {
47-
return question;
48-
}
49-
50-
public void setQuestion(Question question) {
51-
this.question = question;
52-
}
53-
}
1+
package com.example.postgresdemo.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import org.hibernate.annotations.OnDelete;
5+
import org.hibernate.annotations.OnDeleteAction;
6+
7+
import javax.persistence.*;
8+
9+
@Entity
10+
@Table(name = "answers")
11+
public class Answer extends AuditModel {
12+
@Id
13+
@GeneratedValue(generator = "answer_generator")
14+
@SequenceGenerator(
15+
name = "answer_generator",
16+
sequenceName = "answer_sequence",
17+
initialValue = 1000
18+
)
19+
private Long id;
20+
21+
@Column(columnDefinition = "text")
22+
private String text;
23+
24+
@ManyToOne(fetch = FetchType.LAZY, optional = false)
25+
@JoinColumn(name = "question_id", nullable = false)
26+
@OnDelete(action = OnDeleteAction.CASCADE)
27+
@JsonIgnore
28+
private Question question;
29+
30+
public Long getId() {
31+
return id;
32+
}
33+
34+
public void setId(Long id) {
35+
this.id = id;
36+
}
37+
38+
public String getText() {
39+
return text;
40+
}
41+
42+
public void setText(String text) {
43+
this.text = text;
44+
}
45+
46+
public Question getQuestion() {
47+
return question;
48+
}
49+
50+
public void setQuestion(Question question) {
51+
this.question = question;
52+
}
53+
}

0 commit comments

Comments
 (0)