-
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
17 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/main/java/mate/academy/rickandmorty/config/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,22 @@ | ||
package mate.academy.rickandmorty.config.mapper; | ||
|
||
import mate.academy.rickandmorty.dto.external.CharacterResponseDataDto; | ||
import mate.academy.rickandmorty.dto.internal.CharacterDto; | ||
import mate.academy.rickandmorty.model.CharacterRickAndMorty; | ||
import org.mapstruct.InjectionStrategy; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.MappingConstants; | ||
import org.mapstruct.NullValueCheckStrategy; | ||
|
||
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING, | ||
injectionStrategy = InjectionStrategy.CONSTRUCTOR, | ||
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, | ||
implementationPackage = "<PACKAGE_NAME>.impl" | ||
) | ||
public interface CharacterMapper { | ||
@Mapping(source = "id", target = "externalId") | ||
CharacterRickAndMorty toCharacterModel(CharacterResponseDataDto responseDto); | ||
|
||
CharacterDto toDto(CharacterRickAndMorty characterRickAndMorty); | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/mate/academy/rickandmorty/controller/RickAndMortyController.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,48 @@ | ||
package mate.academy.rickandmorty.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.internal.CharacterDto; | ||
import mate.academy.rickandmorty.service.RickAndMortyClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/character") | ||
@Tag( | ||
name = "Rick and Morty controller", | ||
description = "endpoints for getting characters") | ||
public class RickAndMortyController { | ||
private final RickAndMortyClient client; | ||
|
||
@GetMapping("/random") | ||
@Operation( | ||
summary = "get random character", | ||
description = "get random character", | ||
responses = {@ApiResponse( | ||
responseCode = "200", | ||
description = "success" | ||
)} | ||
) | ||
public CharacterDto getRandomCharacter() { | ||
return client.getRandomCharacter(); | ||
} | ||
|
||
@GetMapping("/by-name") | ||
@Operation( | ||
summary = "get all characters containing name", | ||
description = "get all characters containing name", | ||
responses = {@ApiResponse( | ||
responseCode = "200", | ||
description = "success" | ||
)} | ||
) | ||
public List<CharacterDto> getCharacterByName(String name) { | ||
return client.findByPartOfName(name); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/rickandmorty/dto/external/ApiInfoDto.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.external; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class ApiInfoDto { | ||
private Long count; | ||
private Long pages; | ||
private String next; | ||
private String prev; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/mate/academy/rickandmorty/dto/external/ApiResponseDto.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,10 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
import java.util.List; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class ApiResponseDto { | ||
private ApiInfoDto info; | ||
private List<CharacterResponseDataDto> results; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/mate/academy/rickandmorty/dto/external/CharacterResponseDataDto.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.dto.external; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.Data; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@Data | ||
public class CharacterResponseDataDto { | ||
private Long id; | ||
private String name; | ||
private String gender; | ||
private String status; | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/mate/academy/rickandmorty/dto/internal/CharacterDto.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.internal; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class CharacterDto { | ||
private Long id; | ||
private Long externalId; | ||
private String name; | ||
private String gender; | ||
private String status; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/rickandmorty/exception/ApiResponseParsingException.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,7 @@ | ||
package mate.academy.rickandmorty.exception; | ||
|
||
public class ApiResponseParsingException extends RuntimeException { | ||
public ApiResponseParsingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/mate/academy/rickandmorty/model/CharacterRickAndMorty.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,32 @@ | ||
package mate.academy.rickandmorty.model; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Entity | ||
@Table(name = "characters") | ||
@Getter | ||
@Setter | ||
public class CharacterRickAndMorty { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(nullable = false) | ||
private Long externalId; | ||
|
||
@Column(nullable = false) | ||
private String name; | ||
|
||
@Column(nullable = false) | ||
private String gender; | ||
|
||
@Column(nullable = false) | ||
private String status; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/rickandmorty/repository/CharacterRepository.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,13 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.model.CharacterRickAndMorty; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface CharacterRepository extends JpaRepository<CharacterRickAndMorty, Long> { | ||
public long count(); | ||
|
||
public List<CharacterRickAndMorty> findByNameContaining(String name); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/mate/academy/rickandmorty/service/PullDataService.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,5 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
public interface PullDataService { | ||
void pullAllFromExternalApiToDb(); | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/mate/academy/rickandmorty/service/PullDataServiceImpl.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,60 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.annotation.PostConstruct; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.config.mapper.CharacterMapper; | ||
import mate.academy.rickandmorty.dto.external.ApiResponseDto; | ||
import mate.academy.rickandmorty.dto.external.CharacterResponseDataDto; | ||
import mate.academy.rickandmorty.exception.ApiResponseParsingException; | ||
import mate.academy.rickandmorty.model.CharacterRickAndMorty; | ||
import mate.academy.rickandmorty.repository.CharacterRepository; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class PullDataServiceImpl implements PullDataService { | ||
@Value("${external.api.url}") | ||
private String baseUrl; | ||
private final CharacterRepository characterRepository; | ||
private final CharacterMapper mapper; | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@Override | ||
@PostConstruct | ||
public void pullAllFromExternalApiToDb() { | ||
String nextPageUrl = baseUrl; | ||
while (nextPageUrl != null) { | ||
saveAllCharactersFromPage(nextPageUrl); | ||
ApiResponseDto responseDto = getApiResponseDto(nextPageUrl); | ||
nextPageUrl = responseDto.getInfo().getNext(); | ||
} | ||
|
||
} | ||
|
||
private ApiResponseDto getApiResponseDto(String url) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); | ||
try { | ||
return objectMapper.readValue(response.getBody(), ApiResponseDto.class); | ||
} catch (JsonProcessingException e) { | ||
throw new ApiResponseParsingException("Failed to parse API response", e); | ||
} | ||
} | ||
|
||
private void saveAllCharactersFromPage(String url) { | ||
ApiResponseDto apiResponseDto = getApiResponseDto(url); | ||
List<CharacterResponseDataDto> characterResponseDataDtos = apiResponseDto.getResults(); | ||
List<CharacterRickAndMorty> characterRicksAndMorties = characterResponseDataDtos | ||
.stream() | ||
.map(mapper::toCharacterModel) | ||
.toList(); | ||
|
||
characterRepository.saveAll(characterRicksAndMorties); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/mate/academy/rickandmorty/service/RickAndMortyClient.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,10 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.dto.internal.CharacterDto; | ||
|
||
public interface RickAndMortyClient { | ||
CharacterDto getRandomCharacter(); | ||
|
||
List<CharacterDto> findByPartOfName(String partOfName); | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/mate/academy/rickandmorty/service/RickAndMortyClientImpl.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,38 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import jakarta.persistence.EntityNotFoundException; | ||
import java.util.List; | ||
import java.util.Random; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.config.mapper.CharacterMapper; | ||
import mate.academy.rickandmorty.dto.internal.CharacterDto; | ||
import mate.academy.rickandmorty.model.CharacterRickAndMorty; | ||
import mate.academy.rickandmorty.repository.CharacterRepository; | ||
import org.springframework.stereotype.Component; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class RickAndMortyClientImpl implements RickAndMortyClient { | ||
private final CharacterRepository characterRepository; | ||
private final CharacterMapper characterMapper; | ||
|
||
public CharacterDto getRandomCharacter() { | ||
long totalCharacters = characterRepository.count(); | ||
Random random = new Random(); | ||
Long randomId = random.nextLong(totalCharacters); | ||
CharacterRickAndMorty character = characterRepository.findById(randomId).orElseThrow( | ||
() -> new EntityNotFoundException("Character not found")); | ||
return characterMapper.toDto(character); | ||
|
||
} | ||
|
||
@Override | ||
public List<CharacterDto> findByPartOfName(String partOfName) { | ||
List<CharacterRickAndMorty> listOfCharacters = | ||
characterRepository.findByNameContaining(partOfName); | ||
return listOfCharacters | ||
.stream() | ||
.map(characterMapper::toDto) | ||
.toList(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,15 @@ | ||
spring.application.name=Rick-and-Morty | ||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | ||
spring.datasource.url=jdbc:mysql://localhost:3306/rick_and_morty?createDatabaseIfNotExist=true&serverTimezone=UTC | ||
spring.datasource.username=root | ||
spring.datasource.password=pass | ||
|
||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect | ||
spring.jpa.hibernate.ddl-auto=validate | ||
spring.jpa.show-sql=true | ||
spring.jpa.open-in-view=false | ||
|
||
external.api.url=https://rickandmortyapi.com/api/character | ||
|
||
springdoc.swagger-ui.syntaxHighlight.theme=nord | ||
springdoc.swagger-ui.path=/swagger-ui.html |
36 changes: 36 additions & 0 deletions
36
src/main/resources/db/changelog/changes/01-create-characters-table.yaml
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,36 @@ | ||
databaseChangeLog: | ||
- changeSet: | ||
id: create-characters-table | ||
author: vlad | ||
changes: | ||
- createTable: | ||
tableName: characters | ||
columns: | ||
- column: | ||
name: id | ||
type: BIGINT | ||
autoIncrement: true | ||
constraints: | ||
primaryKey: true | ||
nullable: false | ||
- column: | ||
name: external_id | ||
type: BIGINT | ||
constraints: | ||
nullable: false | ||
- column: | ||
name: name | ||
type: VARCHAR(255) | ||
constraints: | ||
nullable: false | ||
- column: | ||
name: status | ||
type: VARCHAR(255) | ||
constraints: | ||
nullable: false | ||
- column: | ||
name: gender | ||
type: VARCHAR(255) | ||
constraints: | ||
nullable: false | ||
|
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,3 @@ | ||
databaseChangeLog: | ||
- include: | ||
file: db/changelog/changes/01-create-characters-table.yaml |
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ class ApplicationTests { | |
|
||
@Test | ||
void contextLoads() { | ||
|
||
} | ||
|
||
} |