User является полем объекта Answer
@Component
public abstract class AnswerConverter {
@Autowired
private UserService userService;
public static final AnswerConverter INSTANCE = Mappers.getMapper(AnswerConverter.class);
@Mapping(source = "answer.id", target = "id")
@Mapping(source = "answer.user", target = "userId", qualifiedByName = "userToLongId")
@Mapping(source = "answer.persistDateTime", target = "persistDate")
@Mapping(source = "answer.updateDateTime", target = "dateAccept")
@Mapping(source = "answer.isHelpful", target = "isHelpful")
@Mapping(source = "answer.question", target = "questionId", qualifiedByName = "questionToQuestionId")
@Mapping(source = "answer.htmlBody", target = "body")
@Mapping(source = "answer.user", target = "image", qualifiedByName = "userToImageLink")
@Mapping(source = "answer.user", target = "nickName", qualifiedByName = "userToNickName")
@Mapping(source = "answer.voteAnswers", target = "countValuable", qualifiedByName = "voteToCount")
public abstract AnswerDto answerToAnswerDto(Answer answer);
@Named("userToLongId")
public Long userToLongId(Answer answer) {
return answer.getUser().getId();
}
@Named("questionToQuestionId")
public Long questionToQuestionId(Answer answer) {
return answer.getQuestion().getId();
}
@Named("userToImageLink")
public String userToImageLink(Answer answer) {
return answer.getUser().getImageLink();
}
@Named("userToNickName")
public String userToNickName(Answer answer) {
return answer.getUser().getNickname();
}
@Named("voteToCount")
public Long voteToCount(Answer answer) {
return (long) answer.getVoteAnswers().size();
}
@Mapping(source = "answerDto.userId", target = "user", qualifiedByName = "LongIdToUser")
@Mapping(source = "answerDto.persistDate", target = "persistDateTime")
@Mapping(source = "answerDto.dateAccept", target = "updateDateTime")
@Mapping(source = "answerDto.questionId", target = "question", qualifiedByName = "questionIdToQuestion")
@Mapping(source = "answerDto.body", target = "htmlBody")
@Mapping(source = "answerDto.image", target = "user", qualifiedByName = "ImageToUser")
@Mapping(source = "answerDto.nickName", target = "user", qualifiedByName = "nickNameToUser")
@Mapping(source = "answerDto.countValuable", target = "voteAnswers", qualifiedByName = "countToVote")
public abstract Answer AnswerDtoToAnswer(AnswerDto answerDto);
@Named("questionIdToQuestion")
public Question questionIdToQuestion(AnswerDto answerDto) {
return null;
}
@Named("ImageToUser")
public User ImageToUser(AnswerDto answerDto) {
User user = userService.getById(answerDto.getUserId()).get();
user.setImageLink(answerDto.getImage());
return user;
}
@Named("LongIdToUser")
public User LongIdToUser(AnswerDto answerDto) {
return userService.getById(answerDto.getUserId()).get();
}
@Named("nickNameToUser")
public User nickNameToUser (AnswerDto answerDto) {
User user = userService.getById(answerDto.getUserId()).get();
user.setNickname(answerDto.getNickName());
return user;
}
}