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

task done, to review #19

Closed
wants to merge 7 commits into from
Closed

Conversation

Watxesnavo
Copy link

added:
client connection
get random character info
get a character with string occurrence in the name

added:
client connection
get random character info
get a character with string occurrence in the name
Copy link

@Ivan95kos Ivan95kos left a comment

Choose a reason for hiding this comment

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

Go through all the Business requirements again


@Component
@RequiredArgsConstructor
public class RickAndMortyClient {

Choose a reason for hiding this comment

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

All data from the public API should be fetched once, and only once, when the Application context is created

Comment on lines 23 to 24
private static final String GET_CHAR_BASE_URL = "https://rickandmortyapi.com/api/character/%s";
private static final String GET_ALL_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.

save the API in properties instead of constants
you can use

@Value("${rick-and-morty-api}")
private String url;

Comment on lines 33 to 39
@Operation(summary = "saving all characters from client to db",
description = "save all char info from client db to yours,"
+ " only to use once for the first time with empty table")
@GetMapping("/save")
public void save() {
service.save();
}
Copy link

@Ivan95kos Ivan95kos Oct 24, 2023

Choose a reason for hiding this comment

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

Anyone who finds this endpoint may spam external api on your behalf. Please do not leave such obvious vulnerabilities in code
use @PostConstruct in client service instead of endpoint /save

Suggested change
@Operation(summary = "saving all characters from client to db",
description = "save all char info from client db to yours,"
+ " only to use once for the first time with empty table")
@GetMapping("/save")
public void save() {
service.save();
}

@PostConstruct
public void save() {
List<Character> characters = new ArrayList<>();
int pageAmount = 42;

Choose a reason for hiding this comment

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

remove this hardcode

@Override
public ExternalCharResponseDto getRandomCharacter() {
Random random = new Random();
int maxCharAmount = 826;

Choose a reason for hiding this comment

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

remove this hardcode


@Override
@PostConstruct
public void save() {

Choose a reason for hiding this comment

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

Suggested change
public void save() {
public void saveCharactersFromPublicAPI() {

public void save() {
List<Character> characters = repository.findAll();
if (characters.isEmpty()) {
for (int i = 1; i < CLIENT_API_CHAR_TABLE_PAGE_NUMBER + 1; i++) {

Choose a reason for hiding this comment

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

What if the number of pages changes? No need to be tied to a fixed number. The code should work with any number of pages.

public ExternalCharResponseDto getRandomCharacter() {
Random random = new Random();
return mapper.toDto(
repository.findById(random.nextLong(CLIENT_API_TOTAL_CHAR_AMOUNT))

Choose a reason for hiding this comment

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

same

Copy link

@okuzan okuzan left a comment

Choose a reason for hiding this comment

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

Well done!

}

@Operation(summary = "show character by search name parameter",
description = "show character by string occurrence in his/her/its name")
Copy link

Choose a reason for hiding this comment

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

Suggested change
description = "show character by string occurrence in his/her/its name")
description = "show character by string occurrence in their name")


@Override
public ExternalCharResponseDto getRandomCharacter() {
Random random = new Random();
Copy link

Choose a reason for hiding this comment

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

make Random a class field

@Watxesnavo Watxesnavo closed this by deleting the head repository Oct 31, 2024
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.

5 participants