Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saved necessary data from an external DB and implemented two methods … #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<maven.checkstyle.plugin.configLocation>
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
</maven.checkstyle.plugin.configLocation>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -41,6 +43,34 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.13</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>

<build>
Expand All @@ -62,13 +92,41 @@
</execution>
</executions>
<configuration>
<sourceDirectories>
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
<sourceDirectory>${project.build.testSourceDirectory}</sourceDirectory>
</sourceDirectories>
<configLocation>${maven.checkstyle.plugin.configLocation}</configLocation>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<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.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok.mapstruct.binding.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

</project>
13 changes: 13 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,13 @@
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 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package mate.academy.rickandmorty.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import mate.academy.rickandmorty.dto.internal.RickAndMortyCharacter;
import mate.academy.rickandmorty.service.character.RickAndMortyCharacterService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Actions with characters",
description = "Endpoints which indicates on a specific action with Character")
@RequiredArgsConstructor
@RestController
@RequestMapping("/rickandmorty")
public class RickAndMortyController {
private final RickAndMortyCharacterService service;

@GetMapping("/random")
@Operation(summary = "Get random Character",
description = "Get random Character which is stored in internal DB")
public RickAndMortyCharacter getRandomCharacter() {
return service.getRandomCharacter();
}

@Operation(summary = "Get Characters by search param(name)",
description = "Receive all Characters containing name")
@GetMapping("/find")
public List<RickAndMortyCharacter> getByNameContaining(@RequestParam String name) {
return service.findByNameContaining(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mate.academy.rickandmorty.dto.external;

import java.util.List;
import lombok.Data;

@Data
public class CharacterResponseDataDto {
private List<RickAndMortyCharacterDto> results;
}
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 RickAndMortyCharacterDto {
private Long id;
private String name;
private String status;
private String gender;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mate.academy.rickandmorty.dto.internal;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;

@Data
@Entity
@Table(name = "rick_and_morty_characters")
public class RickAndMortyCharacter {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long externalId;
private String name;
private String status;
private String gender;

}
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.external.RickAndMortyCharacterDto;
import mate.academy.rickandmorty.dto.internal.RickAndMortyCharacter;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(config = MapperConfig.class)
public interface CharacterMapper {
@Mapping(target = "id", ignore = true)
@Mapping(target = "externalId", source = "id")
RickAndMortyCharacter toModel(RickAndMortyCharacterDto characterDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mate.academy.rickandmorty.repository.character;

import java.util.List;
import mate.academy.rickandmorty.dto.internal.RickAndMortyCharacter;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface RickAndMortyCharacterRepository
extends JpaRepository<RickAndMortyCharacter, Long>,
JpaSpecificationExecutor<RickAndMortyCharacter> {
List<RickAndMortyCharacter> findByNameContaining(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package mate.academy.rickandmorty.service;

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.List;
import lombok.RequiredArgsConstructor;
import mate.academy.rickandmorty.dto.external.CharacterResponseDataDto;
import mate.academy.rickandmorty.dto.external.RickAndMortyCharacterDto;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class RickAndMortyClient {
private static final String BASE_URL = "https://rickandmortyapi.com/api";
private static final String ALL_CHARACTERS = "/character";
private final ObjectMapper objectMapper;

public List<RickAndMortyCharacterDto> getAllCharacter() {
HttpClient httpClient = HttpClient.newHttpClient();
String url = BASE_URL + ALL_CHARACTERS;
HttpRequest httpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create(url))
.build();
try {
HttpResponse<String> response =
httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
CharacterResponseDataDto responseDataDto =
objectMapper.readValue(response.body(), CharacterResponseDataDto.class);
return responseDataDto.getResults();
} catch (IOException | InterruptedException e) {
throw new RuntimeException("Couldn't send httpRequest");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package mate.academy.rickandmorty.service.character;

import java.util.List;
import mate.academy.rickandmorty.dto.internal.RickAndMortyCharacter;

public interface RickAndMortyCharacterService {
RickAndMortyCharacter getRandomCharacter();

List<RickAndMortyCharacter> findByNameContaining(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package mate.academy.rickandmorty.service.character;

import jakarta.annotation.PostConstruct;
import java.util.List;
import java.util.Random;
import lombok.RequiredArgsConstructor;
import mate.academy.rickandmorty.dto.internal.RickAndMortyCharacter;
import mate.academy.rickandmorty.mapper.CharacterMapper;
import mate.academy.rickandmorty.repository.character.RickAndMortyCharacterRepository;
import mate.academy.rickandmorty.service.RickAndMortyClient;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class RickAndMortyCharacterServiceImpl implements RickAndMortyCharacterService {
private final RickAndMortyClient client;
private final RickAndMortyCharacterRepository repository;
private final CharacterMapper characterMapper;

@PostConstruct
public void saveAllCharacters() {
List<RickAndMortyCharacter> list =
client.getAllCharacter()
.stream()
.map(characterMapper::toModel)
.toList();
repository.saveAll(list);
}

@Override
public RickAndMortyCharacter getRandomCharacter() {
List<RickAndMortyCharacter> all = repository.findAll();
return all.get(new Random().nextInt(all.size()));
}

@Override
public List<RickAndMortyCharacter> findByNameContaining(String name) {
return repository.findByNameContaining(name);
}
}
8 changes: 8 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
spring.datasource.url=jdbc:mysql://localhost:3306/rick-and-morty
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.mvc.log-request-details=true

spring.thymeleaf.check-template-location=false
spring.jpa.open-in-view=false
8 changes: 3 additions & 5 deletions src/test/java/mate/academy/rickandmorty/ApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

@SpringBootTest
class ApplicationTests {

@Test
void contextLoads() {
}

@Test
void contextLoads() {
}
}