diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionFacade.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionFacade.java index 1662f5c23c1ef1fb209eca28dbbd660be06357d3..12365a36a81fb46c3fb6c696735b2cdcdc3f2b5a 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionFacade.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionFacade.java @@ -5,15 +5,11 @@ import org.fuseri.model.dto.exercise.AnswerDto; import org.fuseri.model.dto.exercise.QuestionCreateDto; import org.fuseri.model.dto.exercise.QuestionDto; import org.fuseri.model.dto.exercise.QuestionUpdateDto; -import org.fuseri.moduleexercise.answer.Answer; import org.fuseri.moduleexercise.answer.AnswerMapper; import org.fuseri.moduleexercise.answer.AnswerService; -import org.fuseri.moduleexercise.exercise.Exercise; -import org.fuseri.moduleexercise.exercise.ExerciseService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.HashSet; import java.util.List; /** @@ -24,18 +20,16 @@ import java.util.List; @Service public class QuestionFacade { private final QuestionService questionService; - private final ExerciseService exerciseService; private final AnswerService answerService; private final QuestionMapper questionMapper; private final AnswerMapper answerMapper; @Autowired public QuestionFacade( - QuestionService questionService, ExerciseService exerciseService, + QuestionService questionService, AnswerService answerService, QuestionMapper questionMapper, AnswerMapper answerMapper) { this.questionService = questionService; - this.exerciseService = exerciseService; this.answerService = answerService; this.questionMapper = questionMapper; this.answerMapper = answerMapper; @@ -48,8 +42,7 @@ public class QuestionFacade { * @return a QuestionDto object representing the found question */ public QuestionDto find(long id) { - var a = questionService.find(id); - return questionMapper.toDto(a); + return questionMapper.toDto(questionService.find(id)); } /** @@ -69,30 +62,7 @@ public class QuestionFacade { * @return a QuestionDto object representing the added question */ public QuestionDto create(QuestionCreateDto dto) { - Question question = questionMapper.fromCreateDto(dto); - - Exercise exercise; - exercise = exerciseService.find(dto.getExerciseId()); - - exercise.getQuestions().add(question); - question.setExercise(exercise); - - var answerDtos = dto.getAnswers(); - var answers = new HashSet<Answer>(); - for (var answerDto : answerDtos) { - Answer answer = answerMapper.fromCreateDto(answerDto); - answer = answerService.create(answer); - answers.add(answer); - } - - question.setAnswers(answers); - var createdQuestion = questionService.create(question); - - for (var answer : answers) { - answer.setQuestion(createdQuestion); - } - - return questionMapper.toDto(createdQuestion); + return questionMapper.toDto(questionService.create(questionMapper.fromCreateDto(dto))); } /** @@ -102,8 +72,8 @@ public class QuestionFacade { * @return dto of updated question */ public QuestionDto patchUpdate(long id, QuestionUpdateDto dto) { - var a = questionService.patchUpdateWithoutAnswers(id, questionMapper.fromUpdateDto(dto)); - return questionMapper.toDto(a); + var updatedQuestion = questionService.patchUpdateWithoutAnswers(id, questionMapper.fromUpdateDto(dto)); + return questionMapper.toDto(updatedQuestion); } /** @@ -112,10 +82,6 @@ public class QuestionFacade { * @param id of qustion to delete */ public void delete(long id) { - var question = questionService.find(id); - for (var answer : question.getAnswers()) { - answerService.delete(answer.getId()); - } questionService.delete(id); } }