Skip to content

Commit

Permalink
added mapstruct
Browse files Browse the repository at this point in the history
  • Loading branch information
Nonzeq committed Mar 26, 2024
1 parent b32dcc6 commit 98c911b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 34 deletions.
36 changes: 36 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<maven.checkstyle.plugin.configLocation>
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
</maven.checkstyle.plugin.configLocation>
<mapstruct.version>1.5.5.Final</mapstruct.version>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -54,6 +56,12 @@
<version>2.1.0</version>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -77,6 +85,33 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok.mapstruct.binding.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
Expand All @@ -90,6 +125,7 @@
</execution>
</executions>
<configuration>
<sourceDirectories>src/main/java</sourceDirectories>
<configLocation>${maven.checkstyle.plugin.configLocation}</configLocation>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/mate/academy/rickandmorty/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/mate/academy/rickandmorty/config/MapperConfig.java
Original file line number Diff line number Diff line change
@@ -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 = "<PACKAGE_NAME>.impl"
)
public class MapperConfig {
}
Original file line number Diff line number Diff line change
@@ -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<CharacterDto> toListDtos(List<CharacterPerson> characters);

List<CharacterPerson> toListModels(List<CharacterRequestDto> requestDtoList);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RickAndMortyInternalServiceImpl implements RickAndMortyInternalServ
public List<CharacterDto> getAllCharactersNameLike(String name) {
name = "%" + name + "%";
List<CharacterPerson> characters = characterRepository.findByNameLikeIgnoreCase(name);
return characters.stream().map(characterMapper::toDto).toList();
return characterMapper.toListDtos(characters);
}

public CharacterDto getRandomCharacter() {
Expand All @@ -36,8 +36,8 @@ public CharacterDto getRandomCharacter() {

@PostConstruct
public void saveCharacters() {
List<CharacterPerson> result = rickAndMortyClientServiceImpl.getCharacters().stream().map(
characterMapper::toModel).toList();
List<CharacterPerson> result = characterMapper.toListModels(
rickAndMortyClientServiceImpl.getCharacters());
UPPER_CHARACTERS_BOUND = result.size() + 1;
characterRepository.saveAll(result);
}
Expand Down

0 comments on commit 98c911b

Please sign in to comment.