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

added integration with rick-and-morty API #4

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

RedArtelerist
Copy link

No description provided.

Copy link

@NazarSmith228 NazarSmith228 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! Let's improve your solution

pom.xml Outdated
Comment on lines 54 to 57
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need this?

Comment on lines 7 to 8
public record CharacterInfoDto(
@JsonProperty("id") Long externalId, String name, String status, String gender

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird formatting:

Suggested change
public record CharacterInfoDto(
@JsonProperty("id") Long externalId, String name, String status, String gender
public record CharacterInfoDto(
@JsonProperty("id")
Long externalId,
String name,
String status,
String gender


import java.util.List;

public record CharacterResponseDto(

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:

public record SomeRecord(String field1, Integer field2) {

if your record has more than 2 fields:

public record SomeRecord(String field1, 
                         int field2,
                         Double field3,
                         boolean field4) {

or alrternative:

public record SomeRecord(
         String field1, 
         int field2,
         Double field3,
         boolean field4) {

Comment on lines 19 to 20
@Column(nullable = false)
private Long externalId;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be unique?


@Service
public class CharactersClientService {
private static final String BASE_URL = "https://rickandmortyapi.com/api/character";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be stored in your app properties, and then be injected here via @Value

@Service
public class CharactersClientService {
private static final String BASE_URL = "https://rickandmortyapi.com/api/character";
private final HttpClient httpClient = HttpClient.newHttpClient();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider defining it as a bean

also you may find using RestTemplate easier

public class CharactersClientService {
private static final String BASE_URL = "https://rickandmortyapi.com/api/character";
private final HttpClient httpClient = HttpClient.newHttpClient();
private final ObjectMapper objectMapper = new ObjectMapper();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

web starter already brings autoconfiguration, that has a bean of ObjectMapper - no need to create additional one

import org.springframework.stereotype.Service;

@Service
public class CharactersClientService {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be simplified to CharactersClient

}
return characters;
} catch (URISyntaxException | IOException | InterruptedException e) {
throw new RuntimeException("Retrieving characters failed", e);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider creating your custom exception


@Override
public CharacterDto getRandomCharacter() {
List<Character> characters = characterRepository.findAll();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit incorrect

you need to get a random character, however you load all the characters from the DB

think of how you can load only 1 random character via repository :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have an idea how to do this in one SQL query, so I thought of using a random page with a size=1

Copy link

@NazarSmith228 NazarSmith228 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


@Override
public List<CharacterDto> searchCharacters(
String name, @PageableDefault(size = 5, sort = "id") Pageable pageable) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no need to use @PageableDefault here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants