-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
195 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
src/main/java/mate/academy/rickandmorty/config/CustomApplicationRunner.java
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
src/main/java/mate/academy/rickandmorty/config/DataLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package mate.academy.rickandmorty.config; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.mapper.CharacterMapper; | ||
import mate.academy.rickandmorty.model.Character; | ||
import mate.academy.rickandmorty.repository.CharacterRepository; | ||
import mate.academy.rickandmorty.service.impl.DataFetchServiceImpl; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class DataLoader implements ApplicationRunner { | ||
private final DataFetchServiceImpl dataFetcher; | ||
private final CharacterMapper characterMapper; | ||
private final CharacterRepository characterRepository; | ||
|
||
@Override | ||
public void run(ApplicationArguments args) { | ||
List<Character> characters = dataFetcher.fetch() | ||
.stream() | ||
.map(characterMapper::toModel) | ||
.toList(); | ||
characterRepository.saveAll(characters); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/mate/academy/rickandmorty/config/MapperConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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 { | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/mate/academy/rickandmorty/dto/FetchedDataDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package mate.academy.rickandmorty.dto; | ||
|
||
import java.util.List; | ||
import lombok.Data; | ||
import mate.academy.rickandmorty.dto.jsonmapping.CharacterMappingDto; | ||
import mate.academy.rickandmorty.dto.jsonmapping.InfoMappingDto; | ||
|
||
@Data | ||
public class FetchedDataDto { | ||
private InfoMappingDto info; | ||
private List<CharacterMappingDto> results; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/rickandmorty/dto/jsonmapping/CharacterMappingDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package mate.academy.rickandmorty.dto.jsonmapping; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class CharacterMappingDto { | ||
private int id; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/rickandmorty/dto/jsonmapping/InfoMappingDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package mate.academy.rickandmorty.dto.jsonmapping; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class InfoMappingDto { | ||
private int count; | ||
private int pages; | ||
private String next; | ||
private String prev; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package mate.academy.rickandmorty.mapper; | ||
|
||
import mate.academy.rickandmorty.config.MapperConfig; | ||
import mate.academy.rickandmorty.dto.jsonmapping.CharacterMappingDto; | ||
import mate.academy.rickandmorty.model.Character; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
|
||
@Mapper(config = MapperConfig.class) | ||
public interface CharacterMapper { | ||
@Mapping(target = "externalId", source = "id") | ||
@Mapping(target = "id", ignore = true) | ||
Character toModel(CharacterMappingDto characterDto); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/mate/academy/rickandmorty/service/DataFetchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.dto.jsonmapping.CharacterMappingDto; | ||
|
||
public interface DataFetchService { | ||
List<CharacterMappingDto> fetch(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/mate/academy/rickandmorty/service/impl/DataFetchServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package mate.academy.rickandmorty.service.impl; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.FetchedDataDto; | ||
import mate.academy.rickandmorty.dto.jsonmapping.CharacterMappingDto; | ||
import mate.academy.rickandmorty.service.DataFetchService; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class DataFetchServiceImpl implements DataFetchService { | ||
private final RestTemplate restTemplate; | ||
private final ObjectMapper objectMapper; | ||
@Value("${characters.url}") | ||
private String characterUrl; | ||
private final List<CharacterMappingDto> characterDtos; | ||
|
||
@Override | ||
public List<CharacterMappingDto> fetch() { | ||
String url = characterUrl; | ||
|
||
while (url != null) { | ||
String jsonResponse = restTemplate.getForObject(url, String.class); | ||
try { | ||
FetchedDataDto fetchedDataDto = objectMapper | ||
.readValue(jsonResponse, FetchedDataDto.class); | ||
characterDtos.addAll(fetchedDataDto.getResults()); | ||
url = fetchedDataDto.getInfo().getNext(); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException("Can't fetch all characters"); | ||
} | ||
} | ||
|
||
return characterDtos; | ||
} | ||
} |
61 changes: 0 additions & 61 deletions
61
src/main/java/mate/academy/rickandmorty/utils/DataFetcher.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ | |
@SpringBootTest | ||
class ApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |