From 91508c64054cccbcfcedabaa19faf5ea6448d87d Mon Sep 17 00:00:00 2001 From: Dima Date: Tue, 9 Jul 2024 10:42:23 +0200 Subject: [PATCH] fixed class CharacterServiceImpl and add new method to mapper --- .../mate/academy/rickandmorty/mapper/CharactersMapper.java | 4 ++++ .../java/mate/academy/rickandmorty/model/Character.java | 6 ++++-- .../rickandmorty/service/impl/CharactersServiceImpl.java | 7 ++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/mate/academy/rickandmorty/mapper/CharactersMapper.java b/src/main/java/mate/academy/rickandmorty/mapper/CharactersMapper.java index 243d1eeb..721534c4 100644 --- a/src/main/java/mate/academy/rickandmorty/mapper/CharactersMapper.java +++ b/src/main/java/mate/academy/rickandmorty/mapper/CharactersMapper.java @@ -6,9 +6,13 @@ import mate.academy.rickandmorty.model.Character; import org.mapstruct.Mapper; +import java.util.List; + @Mapper(config = MapperConfig.class) public interface CharactersMapper { CharactersResponseDto toDto(Character character); + List toDtoList(List characterList); + Character toModel(CharactersResponseResultDto charactersResponseResultDto); } diff --git a/src/main/java/mate/academy/rickandmorty/model/Character.java b/src/main/java/mate/academy/rickandmorty/model/Character.java index 4f8fcb36..3c91cfbf 100644 --- a/src/main/java/mate/academy/rickandmorty/model/Character.java +++ b/src/main/java/mate/academy/rickandmorty/model/Character.java @@ -6,11 +6,13 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; -import lombok.Data; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; +@Getter +@Setter @Entity -@Data @NoArgsConstructor @Table(name = "characters") public class Character { diff --git a/src/main/java/mate/academy/rickandmorty/service/impl/CharactersServiceImpl.java b/src/main/java/mate/academy/rickandmorty/service/impl/CharactersServiceImpl.java index 65717853..fddacbbb 100644 --- a/src/main/java/mate/academy/rickandmorty/service/impl/CharactersServiceImpl.java +++ b/src/main/java/mate/academy/rickandmorty/service/impl/CharactersServiceImpl.java @@ -16,10 +16,10 @@ public class CharactersServiceImpl implements CharactersService { private final CharactersMapper charactersMapper; private final CharactersRepository charactersRepository; private final CharactersClient charactersClient; + private final Random random; @Override public CharactersResponseDto getRandomCharacter() { - Random random = new Random(); Long randomId = random.nextLong( charactersClient.getCharacters().info().count()); return charactersMapper.toDto(charactersRepository.getById(randomId)); @@ -27,9 +27,6 @@ public CharactersResponseDto getRandomCharacter() { @Override public List getAllCharactersByName(String name) { - return charactersRepository.findAllByName(name).stream() - .map(charactersMapper::toDto) - .toList(); + return charactersMapper.toDtoList(charactersRepository.findAllByName(name)); } - }