-
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
task done, to review #19
Conversation
added: client connection get random character info get a character with string occurrence in the name
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.
Go through all the Business requirements again
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class RickAndMortyClient { |
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.
All data from the public API should be fetched once, and only once, when the Application context is created
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"; |
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.
save the API in properties instead of constants
you can use
@Value("${rick-and-morty-api}")
private String url;
@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(); | ||
} |
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.
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
@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; |
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.
remove this hardcode
@Override | ||
public ExternalCharResponseDto getRandomCharacter() { | ||
Random random = new Random(); | ||
int maxCharAmount = 826; |
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.
remove this hardcode
|
||
@Override | ||
@PostConstruct | ||
public void save() { |
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 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++) { |
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.
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)) |
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.
same
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.
Well done!
} | ||
|
||
@Operation(summary = "show character by search name parameter", | ||
description = "show character by string occurrence in his/her/its name") |
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.
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(); |
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.
make Random a class field
added:
client connection
get random character info
get a character with string occurrence in the name