-
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
Home work #132
base: main
Are you sure you want to change the base?
Home work #132
Conversation
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) { |
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.
are you sure that you scrape all pages, not only the first one?
import jakarta.persistence.Table; | ||
import lombok.Data; | ||
|
||
@Data |
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.
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.
.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) { |
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.
it still parses only first page
private final ObjectMapper objectMapper; | ||
private final HttpClient httpClient = HttpClient.newHttpClient(); | ||
|
||
public ApiResponseDto getAllCharacterFromApi(String url) { |
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.
better save this url somewhere here
public ApiResponseDto getAllCharacterFromApi(String url) { | |
public ApiResponseDto getAllCharacterFromApi() { |
…d /CharacterService.java, /CharacterServiceImpl.java and /CharactersApiClient.java.
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.
Let's go!
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
|
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.
@Bean | ||
public CommandLineRunner commandLineRunner() { | ||
return args -> characterService.saveCharactersToDb(); | ||
} |
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.
Remember to move Bean initialisations into @configuration class
|
||
@Component | ||
public class CharacterMapper { | ||
public CharacterResponseDto toResponseDto(Character character) { |
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.
be consistant with naming. It's either toCharacterResponseDto() and to Character() or toDto() and toModel()
public CharacterResponseDto toResponseDto(Character character) { | |
public CharacterResponseDto toDto(Character character) { |
|
||
@Entity | ||
@Table(name = "characters") | ||
public class Character { |
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.
public class Character { | |
@Getter | |
@Setter | |
@ToString | |
public class Character { |
import org.springframework.data.domain.Pageable; | ||
|
||
public interface CharacterService { | ||
|
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.
@RequiredArgsConstructor | ||
@Service | ||
public class CharacterServiceImpl implements CharacterService { | ||
|
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.
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CharacterMapper { |
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.
Consider using MapStruct to remove all boiler code :)
@Override | ||
public CharacterResponseDto getRandomCharacter() { |
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.
@Override | |
public CharacterResponseDto getRandomCharacter() { | |
@Override | |
@Transactional | |
public CharacterResponseDto getRandomCharacter() { |
You have made two requests to db
No description provided.