Skip to content

Commit

Permalink
fixed class CharacterServiceImpl and add new method to mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima23-ops5 committed Jul 9, 2024
1 parent cb285d1 commit 91508c6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CharactersResponseDto> toDtoList(List<Character> characterList);

Character toModel(CharactersResponseResultDto charactersResponseResultDto);
}
6 changes: 4 additions & 2 deletions src/main/java/mate/academy/rickandmorty/model/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,17 @@ 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));
}

@Override
public List<CharactersResponseDto> getAllCharactersByName(String name) {
return charactersRepository.findAllByName(name).stream()
.map(charactersMapper::toDto)
.toList();
return charactersMapper.toDtoList(charactersRepository.findAllByName(name));
}

}

0 comments on commit 91508c6

Please sign in to comment.