-
Notifications
You must be signed in to change notification settings - Fork 240
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
added integration with rick-and-morty API #4
Open
RedArtelerist
wants to merge
4
commits into
mate-academy:main
Choose a base branch
from
RedArtelerist:dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/rickandmorty/config/WebAppConfig.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.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class WebAppConfig { | ||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/mate/academy/rickandmorty/controller/CharacterController.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,43 @@ | ||
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.CharacterDto; | ||
import mate.academy.rickandmorty.service.CharacterService; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.web.PageableDefault; | ||
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 = "Rick and Morty character management", | ||
description = "endpoints for working with third-party Rick and Morty API" | ||
) | ||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/api/rick-and-morty/characters") | ||
public class CharacterController { | ||
private final CharacterService characterService; | ||
|
||
@Operation( | ||
summary = "Retrieve random character", | ||
description = "Retrieve random character from database." | ||
) | ||
@GetMapping("/random") | ||
public CharacterDto getRandom() { | ||
return characterService.getRandomCharacter(); | ||
} | ||
|
||
@Operation( | ||
summary = "Search characters by name", | ||
description = "Search all characters whose name contains the search string." | ||
) | ||
@GetMapping("/search") | ||
public List<CharacterDto> searchByName( | ||
@RequestParam String name, @PageableDefault(sort = "id", size = 5) Pageable pageable) { | ||
return characterService.searchCharacters(name, pageable); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/mate/academy/rickandmorty/dto/external/CharacterInfoDto.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.external; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record CharacterInfoDto( | ||
@JsonProperty("id") Long externalId, | ||
String name, | ||
String status, | ||
String gender) { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/rickandmorty/dto/external/CharacterResponseDto.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.dto.external; | ||
|
||
import java.util.List; | ||
|
||
public record CharacterResponseDto( | ||
MetaInfoDto info, List<CharacterInfoDto> results) { | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/mate/academy/rickandmorty/dto/external/MetaInfoDto.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,8 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
public record MetaInfoDto( | ||
Integer count, | ||
Integer pages, | ||
String next, | ||
String prev) { | ||
} |
9 changes: 9 additions & 0 deletions
9
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,9 @@ | ||
package mate.academy.rickandmorty.dto.internal; | ||
|
||
public record CharacterDto( | ||
Long id, | ||
Long externalId, | ||
String name, | ||
String status, | ||
String gender) { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/rickandmorty/exception/CharacterNotFoundException.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 CharacterNotFoundException extends RuntimeException { | ||
public CharacterNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/rickandmorty/exception/RetrieveDataException.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 RetrieveDataException extends RuntimeException { | ||
public RetrieveDataException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/mate/academy/rickandmorty/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,19 @@ | ||
package mate.academy.rickandmorty.mapper; | ||
|
||
import mate.academy.rickandmorty.dto.external.CharacterInfoDto; | ||
import mate.academy.rickandmorty.dto.internal.CharacterDto; | ||
import mate.academy.rickandmorty.model.Character; | ||
import org.mapstruct.InjectionStrategy; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.NullValueCheckStrategy; | ||
|
||
@Mapper( | ||
componentModel = "spring", | ||
injectionStrategy = InjectionStrategy.CONSTRUCTOR, | ||
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS | ||
) | ||
public interface CharacterMapper { | ||
CharacterDto toDto(Character character); | ||
|
||
Character toCharacter(CharacterInfoDto bookRequestDto); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/mate/academy/rickandmorty/model/Character.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,30 @@ | ||
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.Data; | ||
|
||
@Data | ||
@Entity | ||
@Table(name = "characters") | ||
public class Character { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(nullable = false, unique = true) | ||
private Long externalId; | ||
|
||
@Column(nullable = false) | ||
private String name; | ||
|
||
@Column(nullable = false) | ||
private String status; | ||
|
||
@Column(nullable = false) | ||
private String gender; | ||
} |
12 changes: 12 additions & 0 deletions
12
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,12 @@ | ||
package mate.academy.rickandmorty.repository; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.model.Character; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface CharacterRepository extends JpaRepository<Character, Long> { | ||
List<Character> findAllByNameContainingIgnoreCase(String name, Pageable pageable); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/rickandmorty/service/CharacterService.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.service; | ||
|
||
import java.util.List; | ||
import mate.academy.rickandmorty.dto.internal.CharacterDto; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface CharacterService { | ||
CharacterDto getRandomCharacter(); | ||
|
||
List<CharacterDto> searchCharacters(String name, Pageable pageable); | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/mate/academy/rickandmorty/service/CharactersClient.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,37 @@ | ||
package mate.academy.rickandmorty.service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.external.CharacterInfoDto; | ||
import mate.academy.rickandmorty.dto.external.CharacterResponseDto; | ||
import mate.academy.rickandmorty.exception.RetrieveDataException; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CharactersClient { | ||
@Value("${rick-and-morty.api.url}") | ||
private String apiUrl; | ||
private final RestTemplate restTemplate; | ||
|
||
public List<CharacterInfoDto> loadCharacters() { | ||
try { | ||
var characterResponseDto = restTemplate | ||
.getForObject(apiUrl, CharacterResponseDto.class); | ||
var characters = new ArrayList<>(characterResponseDto.results()); | ||
String nextPageUrl = characterResponseDto.info().next(); | ||
while (nextPageUrl != null) { | ||
characterResponseDto = restTemplate | ||
.getForObject(nextPageUrl, CharacterResponseDto.class); | ||
characters.addAll(characterResponseDto.results()); | ||
nextPageUrl = characterResponseDto.info().next(); | ||
} | ||
return characters; | ||
} catch (Exception e) { | ||
throw new RetrieveDataException("Retrieving characters failed", e); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.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,57 @@ | ||
package mate.academy.rickandmorty.service.impl; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.internal.CharacterDto; | ||
import mate.academy.rickandmorty.exception.CharacterNotFoundException; | ||
import mate.academy.rickandmorty.mapper.CharacterMapper; | ||
import mate.academy.rickandmorty.model.Character; | ||
import mate.academy.rickandmorty.repository.CharacterRepository; | ||
import mate.academy.rickandmorty.service.CharacterService; | ||
import mate.academy.rickandmorty.service.CharactersClient; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.web.PageableDefault; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class CharacterServiceImpl implements CharacterService { | ||
private static final int PAGE_SIZE = 1; | ||
private static final int FIRST_INDEX = 0; | ||
private final CharacterRepository characterRepository; | ||
private final CharactersClient charactersClient; | ||
private final CharacterMapper characterMapper; | ||
|
||
@PostConstruct | ||
public void initCharacters() { | ||
List<Character> list = charactersClient.loadCharacters().stream() | ||
.map(characterMapper::toCharacter) | ||
.toList(); | ||
characterRepository.saveAll(list); | ||
} | ||
|
||
@Override | ||
public CharacterDto getRandomCharacter() { | ||
long numberOfCharacters = characterRepository.count(); | ||
int pageNumber = (int)(Math.random() * numberOfCharacters); | ||
Page<Character> page = characterRepository.findAll(PageRequest.of(pageNumber, PAGE_SIZE)); | ||
return Optional.ofNullable(page.getContent().get(FIRST_INDEX)) | ||
.map(characterMapper::toDto) | ||
.orElseThrow(() -> new CharacterNotFoundException( | ||
"Can't generate random character") | ||
); | ||
} | ||
|
||
@Override | ||
public List<CharacterDto> searchCharacters( | ||
String name, @PageableDefault(size = 5, sort = "id") Pageable pageable) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is no need to use |
||
return characterRepository.findAllByNameContainingIgnoreCase(name, pageable) | ||
.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,8 @@ | ||
|
||
server.port=8081 | ||
spring.datasource.url=jdbc:mysql://localhost:3306/rick_and_morty?serverTimezone=UTC | ||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | ||
spring.datasource.username=root | ||
spring.datasource.password=611001 | ||
spring.jpa.hibernate.ddl-auto=create-drop | ||
spring.jpa.show-sql=true | ||
rick-and-morty.api.url=https://rickandmortyapi.com/api/character |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a general recommendation on record formatting
if your record has 1-2 fields, you can:
if your record has more than 2 fields:
or alrternative: