Home > database >  How can I shorted my method code with any way in Spring?
How can I shorted my method code with any way in Spring?

Time:11-16

Hi guys it is my addAll method and I think solving this way isn't optimize version, can you offer me a simpler version by the way. Any help into this would be appreciated!

@Override
public ApiResponse addAll(HttpHeaders headers, List<NewsDto> newsDtoList) {
    for (NewsDto newsDto : Objects.requireNonNull(newsDtoList)) {
        News news = new News();
        if (newsDto.getUserId() != null) news.setUserId(newsDto.getUserId());
        if (newsDto.getTitleRu() != null) news.setTitleRu(newsDto.getTitleRu());
        if (newsDto.getTextRu() != null) news.setTextRu(newsDto.getTextRu());
        if (newsDto.getTitleUz() != null) news.setTitleUz(newsDto.getTitleUz());
        if (newsDto.getTextUz() != null) news.setTextUz(newsDto.getTextUz());
        if (newsDto.getTitleEng() != null) news.setTitleEng(newsDto.getTitleEng());
        if (newsDto.getTextEng() != null) news.setTextEng(newsDto.getTextEng());
        newsRepo.save(news);
    }
    return new ApiResponse(true, "all list saved");
}

I try mapped with mapstruct but my entity class extend some fields at another class thats why mapstruct could't see any fields in class and I try solve this way.

CodePudding user response:

Refactor your code slightly with the following considerations:

Your null checks aren't useful; if the field is null on one, it will be set to null on the other anyway. For example, in the original code you have:

if (newsDto.getUserId() != null) news.setUserId(newsDto.getUserId());

but if newsDto.getUserId() returns null then using this object on the setter doesn't change anything unless a default value is already presesnt.

So you can just write

News news = new News();
news.setUserId(newsDto.getUserId());
news.setTitleRu(newsDto.getTitleRu());
news.setTextRu(newsDto.getTextRu());
news.setTitleUz(newsDto.getTitleUz());
news.setTextUz(newsDto.getTextUz());
news.setTitleEng(newsDto.getTitleEng());
news.setTextEng(newsDto.getTextEng());

Or if you need some special logic for default values, you could make a method for that too

public String defaultValue(String raw) {
    if (raw == null || raw.trim().isEmpty()) {
        return null; // or return an empty string or whatever you want
    }
    return raw;
}

Then you can use this in your mapping method

News news = new News();
news.setUserId(defaultValue(newsDto.getUserId()));
news.setTitleRu(defaultValue(newsDto.getTitleRu()));
news.setTextRu(defaultValue(newsDto.getTextRu()));
news.setTitleUz(defaultValue(newsDto.getTitleUz()));
news.setTextUz(defaultValue(newsDto.getTextUz()));
news.setTitleEng(defaultValue(newsDto.getTitleEng()));
news.setTextEng(defaultValue(newsDto.getTextEng()));

The above code should be in its own method, something like

public News toEntity(NewsDto newsDto) {
    ... // see above
    return news;
}

Then you can test this method for the conversion of one news dto to its respective entity class.

After that, use it in the loop you had:

@Override
public ApiResponse addAll(HttpHeaders headers, List<NewsDto> newsDtoList) {
    for (NewsDto newsDto : Objects.requireNonNull(newsDtoList)) {
        newsRepo.save(toEntity(newsDto));
    }
    return new ApiResponse(true, "all list saved");
}

CodePudding user response:

You could use ModelMapper. It should map your DTO to Entity as long as the variable names match.

private static final ModelMapper modelMapper = new ModelMapper();

public static News convertNewsDTOToNewsEntity(NewsDTO newsDTO) {
    return modelMapper.map(newsDTO, News.class);
}
  • Related