C
C
Charik6352021-08-18 22:28:47
Java
Charik635, 2021-08-18 22:28:47

Guys, does anyone know how to encapsulate an ImageLink in a field of the User object through mapstruct without additional methods?

User is a field of the Answer object

@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;
    }

}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question