Skip to content

Commit

Permalink
corrected mistakes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
nshtykh committed Feb 16, 2024
1 parent a13122a commit 2815b5a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
13 changes: 0 additions & 13 deletions src/main/java/mate/academy/rickandmorty/config/AppConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@RequestMapping("/characters")
@RequiredArgsConstructor
@Tag(name = "Characters management",
description = "Endpoints for managing characters from the Rick and Morty cartoon")
description = "Endpoints for retrieving characters from the Rick and Morty cartoon")
public class CharactersController {
private final CharacterService characterService;

Expand All @@ -26,7 +26,7 @@ public CharacterInternalDto getRandom() {
}

@GetMapping("/by-name")
@Operation(summary = "Get all characters by name part")
@Operation(summary = "Get all characters by name part ignore case")
public List<CharacterInternalDto> getAllByNamePart(@RequestParam String namePart) {
return characterService.getAllByNamePart(namePart);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
@Component
@RequiredArgsConstructor
public class RickAndMortyClient {
private static final int NUMBER_OF_PAGES = 42;
private static final String basicUrl = "https://rickandmortyapi.com/api/character?page=%d";
private final ObjectMapper objectMapper;

public List<CharacterExternalDto> getCharacters() {
List<CharacterExternalDto> charactersFromApi = new ArrayList<>();
HttpClient httpClient = HttpClient.newHttpClient();

for (int i = 1; i <= 42; i++) {
for (int i = 1; i <= NUMBER_OF_PAGES; i++) {
String url = String.format(basicUrl, i);
HttpRequest httpRequest = HttpRequest.newBuilder()
.GET()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mate.academy.rickandmorty.service.character;

import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import lombok.RequiredArgsConstructor;
import mate.academy.rickandmorty.dto.internal.CharacterInternalDto;
import mate.academy.rickandmorty.mapper.CharacterMapper;
Expand All @@ -13,9 +13,10 @@
@RequiredArgsConstructor
public class CharacterServiceImpl implements CharacterService {
private static final String EXC_MSG_CANT_FIND = "Can't find character with id ";
private final Random random;
private final CharacterRepository characterRepository;
private final CharacterMapper mapper;
private final ThreadLocalRandom random = ThreadLocalRandom.current();
private long numberOfCharacters;

@Override
public void saveAll(List<Character> characters) {
Expand All @@ -24,7 +25,11 @@ public void saveAll(List<Character> characters) {

@Override
public CharacterInternalDto getRandomCharacter() {
long randomId = random.nextLong(characterRepository.count());
if (numberOfCharacters == 0) {
numberOfCharacters = characterRepository.count();
}

long randomId = random.nextLong(numberOfCharacters);
Character character =
characterRepository.findById(randomId)
.orElseThrow(() -> new RuntimeException(EXC_MSG_CANT_FIND + randomId));
Expand Down

0 comments on commit 2815b5a

Please sign in to comment.