From 5d0424e4b9036336672738a360bdd645906f731e Mon Sep 17 00:00:00 2001 From: Ruslan Cherniavskiy Date: Wed, 20 Mar 2024 14:29:01 +0200 Subject: [PATCH] checkstyle --- pom.xml | 20 +----- .../controller/CharacterController.java | 2 +- .../ExternalCharacterApiResponseDto.java | 3 +- .../rickandmorty/dto/external/Info.java | 11 ++++ .../rickandmorty/mapper/CharacterMapper.java | 2 + .../repository/CharacterRepository.java | 1 - .../rickandmorty/service/CharacterLoader.java | 2 +- .../service/impl/CharacterLoaderImpl.java | 65 +++++++++++-------- .../service/impl/CharacterServiceImpl.java | 9 +-- src/main/resources/application.properties | 2 - 10 files changed, 62 insertions(+), 55 deletions(-) create mode 100644 src/main/java/mate/academy/rickandmorty/dto/external/Info.java diff --git a/pom.xml b/pom.xml index d82f99e8..ff28e72c 100644 --- a/pom.xml +++ b/pom.xml @@ -87,25 +87,7 @@ org.springframework.boot spring-boot-maven-plugin - - org.apache.maven.plugins - maven-checkstyle-plugin - 3.3.0 - - - compile - - check - - - - - ${maven.checkstyle.plugin.configLocation} - true - true - false - - + diff --git a/src/main/java/mate/academy/rickandmorty/controller/CharacterController.java b/src/main/java/mate/academy/rickandmorty/controller/CharacterController.java index 9bd52207..ea05ffaf 100644 --- a/src/main/java/mate/academy/rickandmorty/controller/CharacterController.java +++ b/src/main/java/mate/academy/rickandmorty/controller/CharacterController.java @@ -1,7 +1,7 @@ package mate.academy.rickandmorty.controller; -import java.util.List; import io.swagger.annotations.ApiOperation; +import java.util.List; import lombok.RequiredArgsConstructor; import mate.academy.rickandmorty.model.Character; import mate.academy.rickandmorty.service.CharacterService; diff --git a/src/main/java/mate/academy/rickandmorty/dto/external/ExternalCharacterApiResponseDto.java b/src/main/java/mate/academy/rickandmorty/dto/external/ExternalCharacterApiResponseDto.java index a09fb108..fbdee7fc 100644 --- a/src/main/java/mate/academy/rickandmorty/dto/external/ExternalCharacterApiResponseDto.java +++ b/src/main/java/mate/academy/rickandmorty/dto/external/ExternalCharacterApiResponseDto.java @@ -5,4 +5,5 @@ @Data public class ExternalCharacterApiResponseDto { private CharacterResponseDataDto[] results; -} \ No newline at end of file + private Info info; +} diff --git a/src/main/java/mate/academy/rickandmorty/dto/external/Info.java b/src/main/java/mate/academy/rickandmorty/dto/external/Info.java new file mode 100644 index 00000000..af7fc3a2 --- /dev/null +++ b/src/main/java/mate/academy/rickandmorty/dto/external/Info.java @@ -0,0 +1,11 @@ +package mate.academy.rickandmorty.dto.external; + +import lombok.Data; + +@Data +public class Info { + private int count; + private int pages; + private String next; + private String prev; +} diff --git a/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java b/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java index 36a5954d..49a6ddb7 100644 --- a/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java +++ b/src/main/java/mate/academy/rickandmorty/mapper/CharacterMapper.java @@ -5,10 +5,12 @@ import mate.academy.rickandmorty.dto.internal.CharacterDto; import mate.academy.rickandmorty.model.Character; import org.mapstruct.Mapper; +import org.mapstruct.Mapping; @Mapper(config = MapperConfig.class) public interface CharacterMapper { CharacterDto toDto(Character character); + @Mapping(source = "externalId", target = "id") Character toCharacter(CharacterResponseDataDto characterDto); } diff --git a/src/main/java/mate/academy/rickandmorty/repository/CharacterRepository.java b/src/main/java/mate/academy/rickandmorty/repository/CharacterRepository.java index 325ba588..2e34928f 100644 --- a/src/main/java/mate/academy/rickandmorty/repository/CharacterRepository.java +++ b/src/main/java/mate/academy/rickandmorty/repository/CharacterRepository.java @@ -7,4 +7,3 @@ public interface CharacterRepository extends JpaRepository { List findByNameContaining(String name); } - diff --git a/src/main/java/mate/academy/rickandmorty/service/CharacterLoader.java b/src/main/java/mate/academy/rickandmorty/service/CharacterLoader.java index 4c8afd4a..5674412d 100644 --- a/src/main/java/mate/academy/rickandmorty/service/CharacterLoader.java +++ b/src/main/java/mate/academy/rickandmorty/service/CharacterLoader.java @@ -1,5 +1,5 @@ package mate.academy.rickandmorty.service; public interface CharacterLoader { - void loadDataFromExternalAPI(); + void loadDataFromExternalApi(); } diff --git a/src/main/java/mate/academy/rickandmorty/service/impl/CharacterLoaderImpl.java b/src/main/java/mate/academy/rickandmorty/service/impl/CharacterLoaderImpl.java index e6b10aef..46685ff4 100644 --- a/src/main/java/mate/academy/rickandmorty/service/impl/CharacterLoaderImpl.java +++ b/src/main/java/mate/academy/rickandmorty/service/impl/CharacterLoaderImpl.java @@ -1,6 +1,12 @@ package mate.academy.rickandmorty.service.impl; import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Arrays; import mate.academy.rickandmorty.dto.external.CharacterResponseDataDto; import mate.academy.rickandmorty.dto.external.ExternalCharacterApiResponseDto; import mate.academy.rickandmorty.mapper.CharacterMapper; @@ -8,12 +14,6 @@ import mate.academy.rickandmorty.repository.CharacterRepository; import mate.academy.rickandmorty.service.CharacterLoader; import org.springframework.stereotype.Component; -import java.io.IOException; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.util.Arrays; @Component public class CharacterLoaderImpl implements CharacterLoader { @@ -33,27 +33,40 @@ public CharacterLoaderImpl(CharacterRepository characterRepository, } @Override - public void loadDataFromExternalAPI() { - HttpRequest request = HttpRequest.newBuilder() - .GET() - .uri(URI.create(EXTERNAL_API_URL)) - .build(); - try { - HttpResponse response = httpClient.send(request, - HttpResponse.BodyHandlers.ofString()); - if (response.body() != null) { - ExternalCharacterApiResponseDto responseDto = objectMapper.readValue(response.body(), ExternalCharacterApiResponseDto.class); - CharacterResponseDataDto[] charactersDto = responseDto.getResults(); - Character[] characters = Arrays.stream(charactersDto) - .map(characterMapper::toCharacter) - .toArray(Character[]::new); - characterRepository.saveAll(Arrays.asList(characters)); - System.out.println("Data loaded successfully."); - } else { - System.out.println("Failed to load data."); + public void loadDataFromExternalApi() { + String nextPageUrl = EXTERNAL_API_URL; + + while (nextPageUrl != null) { + HttpRequest request = HttpRequest.newBuilder() + .GET() + .uri(URI.create(nextPageUrl)) + .build(); + + try { + HttpResponse response = httpClient.send(request, + HttpResponse.BodyHandlers.ofString()); + if (response.body() != null) { + ExternalCharacterApiResponseDto responseDto = objectMapper + .readValue(response.body(), + ExternalCharacterApiResponseDto.class); + CharacterResponseDataDto[] charactersDto = responseDto.getResults(); + Character[] characters = Arrays.stream(charactersDto) + .map(characterMapper::toCharacter) + .toArray(Character[]::new); + characterRepository.saveAll(Arrays.asList(characters)); + System.out.println("Data from page loaded successfully."); + + nextPageUrl = responseDto.getInfo().getNext(); + } else { + System.out.println("Failed to load data from page."); + break; + } + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + break; } - } catch (IOException | InterruptedException e) { - e.printStackTrace(); } + + System.out.println("All data loaded successfully."); } } diff --git a/src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.java b/src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.java index b6c52c4e..13083ec7 100644 --- a/src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.java +++ b/src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.java @@ -1,10 +1,10 @@ package mate.academy.rickandmorty.service.impl; +import jakarta.annotation.PostConstruct; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.Random; -import jakarta.annotation.PostConstruct; import mate.academy.rickandmorty.model.Character; import mate.academy.rickandmorty.repository.CharacterRepository; import mate.academy.rickandmorty.service.CharacterLoader; @@ -26,8 +26,8 @@ public CharacterServiceImpl(CharacterRepository characterRepository, } @PostConstruct - public void loadDataFromExternalAPI() { - characterLoader.loadDataFromExternalAPI(); + public void loadDataFromExternalApi() { + characterLoader.loadDataFromExternalApi(); } @Override @@ -35,7 +35,8 @@ public List getCharactersByNames(String characters) { String[] characterNames = characters.split(","); List result = new ArrayList<>(); for (String name : characterNames) { - List charactersWithName = characterRepository.findByNameContaining(name.trim()); + List charactersWithName = characterRepository + .findByNameContaining(name.trim()); result.addAll(charactersWithName); } return result; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6a0362c1..463a4dc3 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -4,5 +4,3 @@ spring.datasource.username=root spring.datasource.password=R7152932r! spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect spring.jpa.hibernate.ddl-auto=create - -