diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerFacade.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerFacade.java index 370eed4b5a45b4505768faf1d8396d0385d4d26b..57ad90530dd02e9a3a810521eeb325d1314d5e81 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerFacade.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerFacade.java @@ -69,8 +69,7 @@ public class AnswerFacade { */ public AnswerDto update(long id, AnswerCreateDto dto) { var updatedAnswer = mapper.fromCreateDto(dto); - updatedAnswer.setId(id); - answerService.update(updatedAnswer); + answerService.update(id, updatedAnswer); Question question; question = questionService.find(dto.getQuestionId()); @@ -79,7 +78,7 @@ public class AnswerFacade { questionAnswers.removeIf(a -> a.getId() == id); questionAnswers.add(updatedAnswer); question.setAnswers(questionAnswers); - questionService.update(question); + questionService.update(dto.getQuestionId(), question); return mapper.toDto(updatedAnswer); } diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/common/DomainService.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/common/DomainService.java index ec9d96818a5ba660d0e49c85e5437fc298d5a732..9e15af6f915bbfc3269950a3466858a84a1d6551 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/common/DomainService.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/common/DomainService.java @@ -35,13 +35,15 @@ public abstract class DomainService<T extends DomainObject> { /** * Update an entity * + * @param id the entity ID * @param entity the entity to update * @return the updated entity */ - public T update(T entity) { - if (!getRepository().existsById(entity.getId())) { + public T update(long id, T entity) { + if (!getRepository().existsById(id)) { throw new EntityNotFoundException("Entity with id " + entity.getId() + " not found."); } + entity.setId(id); return getRepository().save(entity); } diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseFacade.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseFacade.java index 3b3a98f5a54b594b292d52eb4656e82bcf5ca7a4..c6206ea03352eab77bb932ca5f603dad6f9ef3c2 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseFacade.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseFacade.java @@ -105,8 +105,7 @@ public class ExerciseFacade { */ public ExerciseDto update(long id, ExerciseCreateDto dto) { Exercise exercise = exerciseMapper.fromCreateDto(dto); - exercise.setId(id); - Exercise updatedExercise = exerciseService.update(exercise); + Exercise updatedExercise = exerciseService.update(id, exercise); return exerciseMapper.toDto(updatedExercise); } 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 bf6f97d1b2b7f3273d8157731b4a4cfb37247fc3..10442e8ed616cd0a8a81023f120282e3ecb7b108 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 @@ -106,7 +106,7 @@ public class QuestionFacade { question.setId(id); List<Answer> questionAnswers = answerService.findAllByQuestionId(id); question.setAnswers(new HashSet<>(questionAnswers)); - Question updatedQuestion = questionService.update(question); + Question updatedQuestion = questionService.update(id, question); return questionMapper.toDto(updatedQuestion); }