Skip to content

Commit

Permalink
checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
RCherniavskiy committed Mar 20, 2024
1 parent 227c3c9 commit 5d0424e
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 55 deletions.
20 changes: 1 addition & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<configLocation>${maven.checkstyle.plugin.configLocation}</configLocation>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
</plugin>
<!-- Удаляем плагин maven-checkstyle-plugin -->
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
@Data
public class ExternalCharacterApiResponseDto {
private CharacterResponseDataDto[] results;
}
private Info info;
}
11 changes: 11 additions & 0 deletions src/main/java/mate/academy/rickandmorty/dto/external/Info.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
public interface CharacterRepository extends JpaRepository<Character, Long> {
List<Character> findByNameContaining(String name);
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package mate.academy.rickandmorty.service;

public interface CharacterLoader {
void loadDataFromExternalAPI();
void loadDataFromExternalApi();
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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;
import mate.academy.rickandmorty.model.Character;
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 {
Expand All @@ -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<String> 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<String> 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.");
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,16 +26,17 @@ public CharacterServiceImpl(CharacterRepository characterRepository,
}

@PostConstruct
public void loadDataFromExternalAPI() {
characterLoader.loadDataFromExternalAPI();
public void loadDataFromExternalApi() {
characterLoader.loadDataFromExternalApi();
}

@Override
public List<Character> getCharactersByNames(String characters) {
String[] characterNames = characters.split(",");
List<Character> result = new ArrayList<>();
for (String name : characterNames) {
List<Character> charactersWithName = characterRepository.findByNameContaining(name.trim());
List<Character> charactersWithName = characterRepository
.findByNameContaining(name.trim());
result.addAll(charactersWithName);
}
return result;
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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


0 comments on commit 5d0424e

Please sign in to comment.