Skip to content
Snippets Groups Projects
Commit 833a713c authored by Dominika Zemanovičová's avatar Dominika Zemanovičová
Browse files

Simplify AnswerFacade update

parent 87100035
No related branches found
No related tags found
3 merge requests!31M2,!30M2 exercise,!29M2 exercise
Pipeline #
...@@ -3,8 +3,6 @@ package org.fuseri.moduleexercise.answer; ...@@ -3,8 +3,6 @@ package org.fuseri.moduleexercise.answer;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import org.fuseri.model.dto.exercise.AnswerCreateDto; import org.fuseri.model.dto.exercise.AnswerCreateDto;
import org.fuseri.model.dto.exercise.AnswerDto; import org.fuseri.model.dto.exercise.AnswerDto;
import org.fuseri.moduleexercise.question.Question;
import org.fuseri.moduleexercise.question.QuestionService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -16,44 +14,29 @@ import org.springframework.stereotype.Service; ...@@ -16,44 +14,29 @@ import org.springframework.stereotype.Service;
@Transactional @Transactional
public class AnswerFacade { public class AnswerFacade {
private final AnswerService answerService; private final AnswerService answerService;
private final QuestionService questionService;
private final AnswerMapper mapper; private final AnswerMapper mapper;
/** /**
* Constructor for AnswerFacade * Constructor for AnswerFacade
* *
* @param answerService the service responsible for handling answer-related logic * @param answerService the service responsible for handling answer-related logic
* @param questionService the service responsible for handling question-related logic * @param mapper the mapper responsible for converting between DTOs and entities
* @param mapper the mapper responsible for converting between DTOs and entities
*/ */
@Autowired @Autowired
public AnswerFacade(AnswerService answerService, QuestionService questionService, AnswerMapper mapper) { public AnswerFacade(AnswerService answerService, AnswerMapper mapper) {
this.answerService = answerService; this.answerService = answerService;
this.questionService = questionService;
this.mapper = mapper; this.mapper = mapper;
} }
/** /**
* Update an answer * Update answer
* *
* @param id of answer to update * @param id of answer to update
* @param dto dto with updated answer information * @param dto dto with updated answer information
*/ */
public AnswerDto update(long id, AnswerCreateDto dto) { public AnswerDto update(long id, AnswerCreateDto dto) {
var updatedAnswer = mapper.fromCreateDto(dto); return mapper.toDto(answerService.update(id, mapper.fromCreateDto(dto)));
answerService.update(id, updatedAnswer);
Question question;
question = questionService.find(dto.getQuestionId());
var questionAnswers = question.getAnswers();
questionAnswers.removeIf(a -> a.getId() == id);
questionAnswers.add(updatedAnswer);
question.setAnswers(questionAnswers);
questionService.update(dto.getQuestionId(), question);
return mapper.toDto(updatedAnswer);
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment