-
Notifications
You must be signed in to change notification settings - Fork 250
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
add repository, service, mapper, controller & third-party API client … #58
base: main
Are you sure you want to change the base?
Conversation
@Component | ||
@RequiredArgsConstructor | ||
public class DataLoader implements ApplicationRunner { | ||
private final CartoonCharacterClient characterClient; | ||
private final CartoonCharacterService characterService; | ||
|
||
public void run(ApplicationArguments args) { | ||
List<CreateCartoonCharacterRequestDto> characters = characterClient.getAllCharacters(); | ||
characterService.saveAll(characters); | ||
} |
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.
Why do you use Component for this and create inner class?
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.
I need to get service layer beans. Can't get them directly in a static main method. The option with initialization in ApplicationRunner was considered in one of the lessons. What other options are there?
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.
I like the new implementation because of its compactness, but I am not sure that it is correct. I added only a fully encapsulated ThirdPartyApiDataLoader class bean to the Application. After its instance is created, its PostConstruct method is called, which performs data loading.
public CartoonCharacterDto getByName() { | ||
return cartoonCharacterService.getRandom(); |
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.
Is it correct naming of method?
try { | ||
Long pagesCount = PAGES_COUNT; | ||
List<CreateCartoonCharacterRequestDto> result = null; | ||
for (int i = 0; i < pagesCount; i++) { | ||
HttpRequest httpRequest = HttpRequest.newBuilder() | ||
.GET() | ||
.uri(URI.create(BASE_URL.formatted(i))) | ||
.build(); | ||
HttpResponse<String> response = httpClient.send(httpRequest, | ||
HttpResponse.BodyHandlers.ofString()); | ||
CartoonCharactersListResponseDto cartoonCharactersListResponseDto = | ||
objectMapper.readValue(response.body(), | ||
CartoonCharactersListResponseDto.class); | ||
pagesCount = cartoonCharactersListResponseDto.info().pages(); | ||
if (result == null) { | ||
result = cartoonCharactersListResponseDto.results(); | ||
} else { | ||
result.addAll(cartoonCharactersListResponseDto.results()); | ||
} | ||
} | ||
if (result == null || result.isEmpty()) { | ||
throw new GettingCharactersListException("Error getting characters list"); | ||
} | ||
return result; | ||
} catch (IOException | InterruptedException e) { | ||
throw new GettingCharactersListException("Error getting characters list", e); |
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.
Can you divide logic for one helpful method?
if (characters.isEmpty()) { | ||
throw new EntityNotFoundException("Can't find characters with name: " + name); | ||
} | ||
return characters.stream() | ||
.map(mapper::toDto) | ||
.toList(); |
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.
This logic better place to service layer
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.
Good job
…implementation