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

Home work #132

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

Conversation

SergeyGordeychuk
Copy link

No description provided.

Comment on lines 24 to 39
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest httpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create(url))
.build();
try {
HttpResponse<String> httpResponse = httpClient.send(
httpRequest,
HttpResponse.BodyHandlers.ofString());
ApiResponseDto apiResponseDto = objectMapper
.readValue(httpResponse.body(), ApiResponseDto.class);
List<CharacterDtoFromApi> list = Arrays.stream(apiResponseDto.getResults())
.map(characterMapper::toCharacterDtoForDb)
.toList();
return apiResponseDto;
} catch (IOException | InterruptedException e) {

Choose a reason for hiding this comment

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

are you sure that you scrape all pages, not only the first one?

import jakarta.persistence.Table;
import lombok.Data;

@Data

Choose a reason for hiding this comment

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

dont use data on entities as it cause bugs, use getter/setter instead

…cterMapper and CharacterDtoFromApi.class.Created Getters, Setters and toString methods in Character.class. Modified CharactersApiClient.class and CharacterServiceImpl.class.
Comment on lines 23 to 31
.GET()
.uri(new URI(url))
.build();
HttpResponse<String> httpResponse = httpClient.send(
httpRequest,
HttpResponse.BodyHandlers.ofString());
return objectMapper
.readValue(httpResponse.body(), ApiResponseDto.class);
} catch (IOException | InterruptedException | URISyntaxException e) {

Choose a reason for hiding this comment

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

it still parses only first page

private final ObjectMapper objectMapper;
private final HttpClient httpClient = HttpClient.newHttpClient();

public ApiResponseDto getAllCharacterFromApi(String url) {

Choose a reason for hiding this comment

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

better save this url somewhere here

Suggested change
public ApiResponseDto getAllCharacterFromApi(String url) {
public ApiResponseDto getAllCharacterFromApi() {

…d /CharacterService.java, /CharacterServiceImpl.java and /CharactersApiClient.java.
Copy link

@MAMentorFX MAMentorFX left a comment

Choose a reason for hiding this comment

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

Let's go!


public static void main(String[] args) {
SpringApplication.run(Application.class, args);

Choose a reason for hiding this comment

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

Suggested change

Comment on lines +20 to 23
@Bean
public CommandLineRunner commandLineRunner() {
return args -> characterService.saveCharactersToDb();
}

Choose a reason for hiding this comment

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

Remember to move Bean initialisations into @configuration class


@Component
public class CharacterMapper {
public CharacterResponseDto toResponseDto(Character character) {

Choose a reason for hiding this comment

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

be consistant with naming. It's either toCharacterResponseDto() and to Character() or toDto() and toModel()

Suggested change
public CharacterResponseDto toResponseDto(Character character) {
public CharacterResponseDto toDto(Character character) {


@Entity
@Table(name = "characters")
public class Character {

Choose a reason for hiding this comment

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

Suggested change
public class Character {
@Getter
@Setter
@ToString
public class Character {

import org.springframework.data.domain.Pageable;

public interface CharacterService {

Choose a reason for hiding this comment

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

Suggested change

@RequiredArgsConstructor
@Service
public class CharacterServiceImpl implements CharacterService {

Choose a reason for hiding this comment

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

Suggested change

import org.springframework.stereotype.Component;

@Component
public class CharacterMapper {

Choose a reason for hiding this comment

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

Consider using MapStruct to remove all boiler code :)

Comment on lines +35 to +36
@Override
public CharacterResponseDto getRandomCharacter() {

Choose a reason for hiding this comment

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

Suggested change
@Override
public CharacterResponseDto getRandomCharacter() {
@Override
@Transactional
public CharacterResponseDto getRandomCharacter() {

You have made two requests to db

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.

3 participants