Skip to content

Commit

Permalink
update according to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyH277 committed May 8, 2024
1 parent 5c783b3 commit 4c02374
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 85 deletions.
54 changes: 54 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
</maven.checkstyle.plugin.configLocation>
<springdoc.version>2.5.0</springdoc.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -85,6 +87,12 @@
<version>${springdoc.version}</version>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -112,6 +120,52 @@
<linkXRef>false</linkXRef>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok.mapstruct.binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</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>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
<sourceDirectories>src</sourceDirectories>
</configuration>
</plugin>
</plugins>
</build>

Expand Down

This file was deleted.

28 changes: 28 additions & 0 deletions src/main/java/mate/academy/rickandmorty/config/DataLoader.java
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 src/main/java/mate/academy/rickandmorty/config/MapperConfig.java
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 src/main/java/mate/academy/rickandmorty/dto/FetchedDataDto.java
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;
}
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;
}
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;
}
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);
}
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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import jakarta.transaction.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import lombok.RequiredArgsConstructor;
import mate.academy.rickandmorty.model.Character;
Expand All @@ -21,8 +20,7 @@ public class CharacterServiceImpl implements CharacterService {
public Character generateRandomCharacter() {
long charactersAmount = characterRepository.count();
long id = random.nextLong(charactersAmount);
return Optional.of(characterRepository.findById(id))
.get()
return characterRepository.findById(id)
.orElseThrow(() -> new IllegalStateException("Character not found with id: " + id));
}

Expand Down
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 src/main/java/mate/academy/rickandmorty/utils/DataFetcher.java

This file was deleted.

6 changes: 3 additions & 3 deletions src/test/java/mate/academy/rickandmorty/ApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@SpringBootTest
class ApplicationTests {

@Test
void contextLoads() {
}
@Test
void contextLoads() {
}

}

0 comments on commit 4c02374

Please sign in to comment.