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

Change AnswerMapper to map to entities inside dto

parent ac767596
No related branches found
No related tags found
3 merge requests!31M2,!30M2 exercise,!29M2 exercise
...@@ -89,15 +89,6 @@ public class AnswerFacade { ...@@ -89,15 +89,6 @@ public class AnswerFacade {
* @param id of answer to delete * @param id of answer to delete
*/ */
public void delete(long id) { public void delete(long id) {
var answer = answerService.find(id);
Question question;
question = questionService.find(answer.getQuestion().getId());
var questionAnswers = question.getAnswers();
questionAnswers.removeIf(a -> a.getId() == answer.getId());
question.setAnswers(questionAnswers);
answerService.delete(id); answerService.delete(id);
} }
} }
...@@ -4,7 +4,12 @@ import org.fuseri.model.dto.exercise.AnswerCreateDto; ...@@ -4,7 +4,12 @@ import org.fuseri.model.dto.exercise.AnswerCreateDto;
import org.fuseri.model.dto.exercise.AnswerDto; import org.fuseri.model.dto.exercise.AnswerDto;
import org.fuseri.model.dto.exercise.AnswerInQuestionCreateDto; import org.fuseri.model.dto.exercise.AnswerInQuestionCreateDto;
import org.fuseri.moduleexercise.common.DomainMapper; import org.fuseri.moduleexercise.common.DomainMapper;
import org.fuseri.moduleexercise.question.Question;
import org.fuseri.moduleexercise.question.QuestionService;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
...@@ -12,8 +17,16 @@ import java.util.Set; ...@@ -12,8 +17,16 @@ import java.util.Set;
/** /**
* Mapper between Answers and their corresponding DTOs * Mapper between Answers and their corresponding DTOs
*/ */
@Mapper @Mapper(componentModel = "spring", uses = {QuestionService.class})
public interface AnswerMapper extends DomainMapper<Answer, AnswerDto> { public abstract class AnswerMapper implements DomainMapper<Answer, AnswerDto> {
@Autowired
private QuestionService questionService;
@Named("mapIdToQuestion")
public Question mapIdToQuestion(Long id) {
return questionService.find(id);
}
/** /**
* Convert DTO of type AnswerCreateDto to Answer * Convert DTO of type AnswerCreateDto to Answer
...@@ -21,7 +34,8 @@ public interface AnswerMapper extends DomainMapper<Answer, AnswerDto> { ...@@ -21,7 +34,8 @@ public interface AnswerMapper extends DomainMapper<Answer, AnswerDto> {
* @param dto DTO to be converted * @param dto DTO to be converted
* @return corresponding Answer entity created from DTO * @return corresponding Answer entity created from DTO
*/ */
Answer fromCreateDto(AnswerCreateDto dto); @Mapping(target = "question", source = "dto.questionId", qualifiedByName = "mapIdToQuestion")
public abstract Answer fromCreateDto(AnswerCreateDto dto);
/** /**
* Convert List of AnswerInQuestionCreateDto to List of corresponding Answers * Convert List of AnswerInQuestionCreateDto to List of corresponding Answers
...@@ -29,12 +43,13 @@ public interface AnswerMapper extends DomainMapper<Answer, AnswerDto> { ...@@ -29,12 +43,13 @@ public interface AnswerMapper extends DomainMapper<Answer, AnswerDto> {
* @param dtos to be converted * @param dtos to be converted
* @return corresponding list of Answers * @return corresponding list of Answers
*/ */
Set<Answer> fromCreateDtoList(List<AnswerInQuestionCreateDto> dtos); public abstract Set<Answer> fromCreateDtoList(List<AnswerInQuestionCreateDto> dtos);
/** /**
* Convert DTO of type AnswerInQuestionCreateDto to Answer * Convert DTO of type AnswerInQuestionCreateDto to Answer
*
* @param dto DTO to be converted * @param dto DTO to be converted
* @return corresponding Answer entity created from DTO * @return corresponding Answer entity created from DTO
*/ */
Answer fromCreateDto(AnswerInQuestionCreateDto dto); public abstract Answer fromCreateDto(AnswerInQuestionCreateDto dto);
} }
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