From 98c911b49571d9ad42ee35ddf36e80f11d9048b0 Mon Sep 17 00:00:00 2001 From: Nonzeq Date: Tue, 26 Mar 2024 16:13:13 +0200 Subject: [PATCH] added mapstruct --- pom.xml | 36 +++++++++++++++++++ .../academy/rickandmorty/Application.java | 1 - .../rickandmorty/config/MapperConfig.java | 13 +++++++ .../rickandmorty/mapper/CharacterMapper.java | 8 +++++ .../mapper/CharacterMapperImpl.java | 30 ---------------- .../impl/RickAndMortyInternalServiceImpl.java | 6 ++-- 6 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 src/main/java/mate/academy/rickandmorty/config/MapperConfig.java delete mode 100644 src/main/java/mate/academy/rickandmorty/mapper/CharacterMapperImpl.java diff --git a/pom.xml b/pom.xml index d52aa52c..d16f0ca3 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,8 @@ https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml + 1.5.5.Final + 0.2.0 @@ -54,6 +56,12 @@ 2.1.0 + + org.mapstruct + mapstruct + ${mapstruct.version} + + org.springframework.boot spring-boot-starter-test @@ -77,6 +85,33 @@ org.springframework.boot spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + + + org.projectlombok + lombok + ${lombok.version} + + + org.projectlombok + lombok-mapstruct-binding + ${lombok.mapstruct.binding.version} + + + org.mapstruct + mapstruct-processor + ${mapstruct.version} + + + + + org.apache.maven.plugins maven-checkstyle-plugin @@ -90,6 +125,7 @@ + src/main/java ${maven.checkstyle.plugin.configLocation} true true diff --git a/src/main/java/mate/academy/rickandmorty/Application.java b/src/main/java/mate/academy/rickandmorty/Application.java index cdea84fc..3b4e97e6 100644 --- a/src/main/java/mate/academy/rickandmorty/Application.java +++ b/src/main/java/mate/academy/rickandmorty/Application.java @@ -5,7 +5,6 @@ @SpringBootApplication public class Application { - public static void main(String[] args) { SpringApplication.run(Application.class, args); } diff --git a/src/main/java/mate/academy/rickandmorty/config/MapperConfig.java b/src/main/java/mate/academy/rickandmorty/config/MapperConfig.java new file mode 100644 index 00000000..450e58db --- /dev/null +++ b/src/main/java/mate/academy/rickandmorty/config/MapperConfig.java @@ -0,0 +1,13 @@ +package mate.academy.rickandmorty.config; + +import org.mapstruct.InjectionStrategy; +import org.mapstruct.NullValueCheckStrategy; + +@org.mapstruct.MapperConfig( + componentModel = "spring", + injectionStrategy = InjectionStrategy.CONSTRUCTOR, + nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, + implementationPackage = ".impl" +) +public class MapperConfig { +} diff --git a/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java b/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java index 4f7ea901..b16f3d3e 100644 --- a/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java +++ b/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java @@ -1,11 +1,19 @@ package mate.academy.rickandmorty.mapper; +import java.util.List; +import mate.academy.rickandmorty.config.MapperConfig; import mate.academy.rickandmorty.dto.CharacterDto; import mate.academy.rickandmorty.dto.CharacterRequestDto; import mate.academy.rickandmorty.model.CharacterPerson; +import org.mapstruct.Mapper; +@Mapper(config = MapperConfig.class) public interface CharacterMapper { CharacterDto toDto(CharacterPerson character); CharacterPerson toModel(CharacterRequestDto requestDto); + + List toListDtos(List characters); + + List toListModels(List requestDtoList); } diff --git a/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapperImpl.java b/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapperImpl.java deleted file mode 100644 index b05db9f1..00000000 --- a/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapperImpl.java +++ /dev/null @@ -1,30 +0,0 @@ -package mate.academy.rickandmorty.mapper; - -import mate.academy.rickandmorty.dto.CharacterDto; -import mate.academy.rickandmorty.dto.CharacterRequestDto; -import mate.academy.rickandmorty.model.CharacterPerson; -import org.springframework.stereotype.Component; - -@Component -public class CharacterMapperImpl implements CharacterMapper { - @Override - public CharacterDto toDto(CharacterPerson character) { - CharacterDto characterDto = new CharacterDto(); - characterDto.setId(character.getId()); - characterDto.setExternalId(character.getExternalId()); - characterDto.setName(character.getName()); - characterDto.setStatus(character.getStatus()); - characterDto.setGender(character.getGender()); - return characterDto; - } - - @Override - public CharacterPerson toModel(CharacterRequestDto requestDto) { - CharacterPerson character = new CharacterPerson(); - character.setExternalId(requestDto.getExternalId()); - character.setGender(requestDto.getGender()); - character.setName(requestDto.getName()); - character.setStatus(requestDto.getStatus()); - return character; - } -} diff --git a/src/main/java/mate/academy/rickandmorty/service/impl/RickAndMortyInternalServiceImpl.java b/src/main/java/mate/academy/rickandmorty/service/impl/RickAndMortyInternalServiceImpl.java index a3e84ae2..6befedcc 100644 --- a/src/main/java/mate/academy/rickandmorty/service/impl/RickAndMortyInternalServiceImpl.java +++ b/src/main/java/mate/academy/rickandmorty/service/impl/RickAndMortyInternalServiceImpl.java @@ -24,7 +24,7 @@ public class RickAndMortyInternalServiceImpl implements RickAndMortyInternalServ public List getAllCharactersNameLike(String name) { name = "%" + name + "%"; List characters = characterRepository.findByNameLikeIgnoreCase(name); - return characters.stream().map(characterMapper::toDto).toList(); + return characterMapper.toListDtos(characters); } public CharacterDto getRandomCharacter() { @@ -36,8 +36,8 @@ public CharacterDto getRandomCharacter() { @PostConstruct public void saveCharacters() { - List result = rickAndMortyClientServiceImpl.getCharacters().stream().map( - characterMapper::toModel).toList(); + List result = characterMapper.toListModels( + rickAndMortyClientServiceImpl.getCharacters()); UPPER_CHARACTERS_BOUND = result.size() + 1; characterRepository.saveAll(result); }