44import com .example .postgresdemo .model .Question ;
55import com .example .postgresdemo .model .QuestionRequestDTO ;
66import com .example .postgresdemo .model .QuestionResponseDTO ;
7+ import com .example .postgresdemo .model .User ;
78import com .example .postgresdemo .repository .QuestionRepository ;
9+ import com .example .postgresdemo .repository .UserRepository ;
810import org .springframework .data .domain .Page ;
911import org .springframework .data .domain .Pageable ;
1012import org .springframework .stereotype .Service ;
1113
1214@ Service
1315public class QuestionService {
1416 private final QuestionRepository questionRepository ;
17+ private final UserRepository userRepository ;
1518
16- public QuestionService (QuestionRepository questionRepository ) {
19+
20+ public QuestionService (QuestionRepository questionRepository , UserRepository userRepository ) {
1721 this .questionRepository = questionRepository ;
22+ this .userRepository = userRepository ;
1823 }
1924
2025 public Page <QuestionResponseDTO > findAll (Pageable pageable ) {
@@ -23,19 +28,23 @@ public Page<QuestionResponseDTO> findAll(Pageable pageable) {
2328 }
2429
2530 public QuestionResponseDTO create (QuestionRequestDTO questionRequest ) {
26- Question question = toQuestion (questionRequest );
31+ User user = userRepository .findById (questionRequest .getAuthorId ())
32+ .orElseThrow (() -> new ResourceNotFoundException ("User not found with id " + questionRequest .getAuthorId ()));
33+ Question question = toQuestion (questionRequest , user );
2734 return toQuestionResponseDTO (questionRepository .save (question ));
2835 }
2936
3037 public QuestionResponseDTO update (Long questionId , QuestionRequestDTO questionRequest ) {
31- Question question = toQuestion (questionRequest );
38+ User user = userRepository .findById (questionRequest .getAuthorId ())
39+ .orElseThrow (() -> new ResourceNotFoundException ("User not found with id " + questionRequest .getAuthorId ()));
40+ Question question = toQuestion (questionRequest , user );
3241 return questionRepository .findById (questionId )
3342 .map (foundQuestion -> {
3443 foundQuestion .setTitle (question .getTitle ());
3544 foundQuestion .setDescription (question .getDescription ());
45+ foundQuestion .setUser (user );
3646 return toQuestionResponseDTO (questionRepository .save (foundQuestion ));
3747 }).orElseThrow (() -> new ResourceNotFoundException ("Question not found with id " + questionId ));
38-
3948 }
4049
4150 public void delete (Long questionId ) {
@@ -53,10 +62,11 @@ private QuestionResponseDTO toQuestionResponseDTO(Question question) {
5362 return questionResponse ;
5463 }
5564
56- private Question toQuestion (QuestionRequestDTO questionRequestDTO ) {
65+ private Question toQuestion (QuestionRequestDTO questionRequestDTO , User user ) {
5766 Question question = new Question ();
5867 question .setTitle (questionRequestDTO .getTitle ());
5968 question .setDescription (questionRequestDTO .getDescription ());
69+ question .setUser (user );
6070 return question ;
6171 }
6272}
0 commit comments