diff --git a/src/main/java/mate/academy/rickandmorty/config/AppConfig.java b/src/main/java/mate/academy/rickandmorty/config/AppConfig.java deleted file mode 100644 index 78bafc40..00000000 --- a/src/main/java/mate/academy/rickandmorty/config/AppConfig.java +++ /dev/null @@ -1,13 +0,0 @@ -package mate.academy.rickandmorty.config; - -import java.util.Random; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class AppConfig { - @Bean - public Random random() { - return new Random(); - } -} diff --git a/src/main/java/mate/academy/rickandmorty/controller/CharactersController.java b/src/main/java/mate/academy/rickandmorty/controller/CharactersController.java index 1d66bda5..fe3a59cb 100644 --- a/src/main/java/mate/academy/rickandmorty/controller/CharactersController.java +++ b/src/main/java/mate/academy/rickandmorty/controller/CharactersController.java @@ -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; @@ -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 getAllByNamePart(@RequestParam String namePart) { return characterService.getAllByNamePart(namePart); } diff --git a/src/main/java/mate/academy/rickandmorty/service/RickAndMortyClient.java b/src/main/java/mate/academy/rickandmorty/service/RickAndMortyClient.java index 0b117757..eaaac8ce 100644 --- a/src/main/java/mate/academy/rickandmorty/service/RickAndMortyClient.java +++ b/src/main/java/mate/academy/rickandmorty/service/RickAndMortyClient.java @@ -16,6 +16,7 @@ @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; @@ -23,7 +24,7 @@ public List getCharacters() { List 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() diff --git a/src/main/java/mate/academy/rickandmorty/service/character/CharacterServiceImpl.java b/src/main/java/mate/academy/rickandmorty/service/character/CharacterServiceImpl.java index 2761c905..2a403dae 100644 --- a/src/main/java/mate/academy/rickandmorty/service/character/CharacterServiceImpl.java +++ b/src/main/java/mate/academy/rickandmorty/service/character/CharacterServiceImpl.java @@ -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; @@ -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 characters) { @@ -24,7 +25,11 @@ public void saveAll(List 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));