diff --git a/pom.xml b/pom.xml
index 0c754f19..528764e4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -19,6 +19,8 @@
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
+ 0.2.0
+ 1.5.5.Final
@@ -37,14 +39,71 @@
spring-boot-starter-data-jpa
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
com.h2database
h2
+
+
+ mysql
+ mysql-connector-java
+ 8.0.33
+
+
+
+ org.projectlombok
+ lombok
+
+
+
+ org.mapstruct
+ mapstruct
+ ${mapstruct.version}
+
+
+
+ org.liquibase
+ liquibase-core
+
+
+ org.springdoc
+ springdoc-openapi-starter-webmvc-ui
+ 2.1.0
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+ ${java.version}
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+
+
+ org.projectlombok
+ lombok-mapstruct-binding
+ ${lombok.mapstruct.binding.version}
+
+
+ org.mapstruct
+ mapstruct-processor
+ ${mapstruct.version}
+
+
+
+
org.springframework.boot
spring-boot-maven-plugin
@@ -68,6 +127,29 @@
false
+
+ org.apache.maven.plugins
+ maven-checkstyle-plugin
+ 3.3.0
+
+
+ compile
+
+ check
+
+
+
+
+ ${maven.checkstyle.plugin.configLocation}
+ UTF-8
+ UTF-8
+ true
+ true
+ false
+ src
+
+
+
diff --git a/src/main/java/mate/academy/rickandmorty/api/util/ApiClientConfiguration.java b/src/main/java/mate/academy/rickandmorty/api/util/ApiClientConfiguration.java
new file mode 100644
index 00000000..0efc95b3
--- /dev/null
+++ b/src/main/java/mate/academy/rickandmorty/api/util/ApiClientConfiguration.java
@@ -0,0 +1,13 @@
+package mate.academy.rickandmorty.api.util;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+
+@Configuration
+public class ApiClientConfiguration {
+ @Bean
+ public RestTemplate restTemplate() {
+ return new RestTemplate();
+ }
+}
diff --git a/src/main/java/mate/academy/rickandmorty/api/util/PersonageDatabasePopulator.java b/src/main/java/mate/academy/rickandmorty/api/util/PersonageDatabasePopulator.java
new file mode 100644
index 00000000..2a1f2022
--- /dev/null
+++ b/src/main/java/mate/academy/rickandmorty/api/util/PersonageDatabasePopulator.java
@@ -0,0 +1,22 @@
+package mate.academy.rickandmorty.api.util;
+
+import jakarta.annotation.PostConstruct;
+import lombok.RequiredArgsConstructor;
+import mate.academy.rickandmorty.repository.PersonageRepository;
+import org.springframework.stereotype.Component;
+
+@Component
+@RequiredArgsConstructor
+public class PersonageDatabasePopulator {
+ private final RickAndMortyClient rickAndMortyApiClient;
+ private final PersonageRepository personageRepository;
+
+ @PostConstruct
+ public void init() {
+ if (personageRepository.count() != 0) {
+ personageRepository.deleteAll();
+ }
+ personageRepository.saveAll(rickAndMortyApiClient.getAllPersonages());
+ }
+
+}
diff --git a/src/main/java/mate/academy/rickandmorty/api/util/RickAndMortyClient.java b/src/main/java/mate/academy/rickandmorty/api/util/RickAndMortyClient.java
new file mode 100644
index 00000000..ef5d0b9e
--- /dev/null
+++ b/src/main/java/mate/academy/rickandmorty/api/util/RickAndMortyClient.java
@@ -0,0 +1,39 @@
+package mate.academy.rickandmorty.api.util;
+
+import java.util.ArrayList;
+import java.util.List;
+import lombok.RequiredArgsConstructor;
+import mate.academy.rickandmorty.dto.external.ExternalResponseDto;
+import mate.academy.rickandmorty.dto.external.Result;
+import mate.academy.rickandmorty.mapper.PersonageMapper;
+import mate.academy.rickandmorty.model.Personage;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.web.client.RestTemplate;
+
+@Component
+@RequiredArgsConstructor
+public class RickAndMortyClient {
+ private final PersonageMapper personageMapper;
+ private final RestTemplate restTemplate;
+ @Value("${rick-and-morty.api.url}")
+ private String baseCharacterUrl;
+
+ public List getAllPersonages() {
+ List resultList = new ArrayList<>();
+ ExternalResponseDto externalResponseDto =
+ restTemplate.getForEntity(baseCharacterUrl, ExternalResponseDto.class).getBody();
+ String nextUrl = externalResponseDto.info().next();
+ while (nextUrl != null) {
+ resultList.addAll(externalResponseDto.resultList());
+ externalResponseDto = restTemplate
+ .getForEntity(nextUrl, ExternalResponseDto.class)
+ .getBody();
+ nextUrl = externalResponseDto.info().next();
+ }
+ return resultList
+ .stream()
+ .map(personageMapper::toPersonage)
+ .toList();
+ }
+}
diff --git a/src/main/java/mate/academy/rickandmorty/controller/RickAndMortyController.java b/src/main/java/mate/academy/rickandmorty/controller/RickAndMortyController.java
new file mode 100644
index 00000000..8a8ab40f
--- /dev/null
+++ b/src/main/java/mate/academy/rickandmorty/controller/RickAndMortyController.java
@@ -0,0 +1,41 @@
+package mate.academy.rickandmorty.controller;
+
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import java.util.List;
+import lombok.RequiredArgsConstructor;
+import mate.academy.rickandmorty.dto.internal.PersonageResponseDto;
+import mate.academy.rickandmorty.service.PersonageService;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.web.PageableDefault;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@Tag(name = "Controller to get personages",
+ description = "In this controller you can get random personages and personages by name")
+@RequiredArgsConstructor
+@RequestMapping("/rick-and-morty")
+public class RickAndMortyController {
+ private final PersonageService personageRepository;
+
+ @GetMapping("/random-personage")
+ @Operation(summary = "Get random personage",
+ description = "Get random personage from Rick and Morty universe")
+ public PersonageResponseDto getRandomPersonage() {
+ return personageRepository.getRandomPersonage();
+ }
+
+ @GetMapping("/search")
+ @Operation(summary = "Get personages by name",
+ description = "Get from Rick and Morty universe "
+ + "personages witch has name in their name")
+ public List getPersonageByName(
+ @PageableDefault(page = 0, size = 10, sort = "name") Pageable pageable,
+ @RequestParam String name
+ ) {
+ return personageRepository.getPersonageByNameLike(pageable, name);
+ }
+}
diff --git a/src/main/java/mate/academy/rickandmorty/dto/external/ExternalResponseDto.java b/src/main/java/mate/academy/rickandmorty/dto/external/ExternalResponseDto.java
new file mode 100644
index 00000000..4ef58a0f
--- /dev/null
+++ b/src/main/java/mate/academy/rickandmorty/dto/external/ExternalResponseDto.java
@@ -0,0 +1,10 @@
+package mate.academy.rickandmorty.dto.external;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import java.util.List;
+
+public record ExternalResponseDto(
+ Info info,
+ @JsonProperty("results") List resultList
+) {
+}
diff --git a/src/main/java/mate/academy/rickandmorty/dto/external/Info.java b/src/main/java/mate/academy/rickandmorty/dto/external/Info.java
new file mode 100644
index 00000000..07658d24
--- /dev/null
+++ b/src/main/java/mate/academy/rickandmorty/dto/external/Info.java
@@ -0,0 +1,9 @@
+package mate.academy.rickandmorty.dto.external;
+
+public record Info(
+ Long count,
+ Integer pages,
+ String next,
+ String prev
+) {
+}
diff --git a/src/main/java/mate/academy/rickandmorty/dto/external/Result.java b/src/main/java/mate/academy/rickandmorty/dto/external/Result.java
new file mode 100644
index 00000000..e5a3ff1c
--- /dev/null
+++ b/src/main/java/mate/academy/rickandmorty/dto/external/Result.java
@@ -0,0 +1,11 @@
+package mate.academy.rickandmorty.dto.external;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public record Result(
+ @JsonProperty("id") Long externalId,
+ String name,
+ String status,
+ String gender
+) {
+}
diff --git a/src/main/java/mate/academy/rickandmorty/dto/internal/PersonageResponseDto.java b/src/main/java/mate/academy/rickandmorty/dto/internal/PersonageResponseDto.java
new file mode 100644
index 00000000..783db6a7
--- /dev/null
+++ b/src/main/java/mate/academy/rickandmorty/dto/internal/PersonageResponseDto.java
@@ -0,0 +1,8 @@
+package mate.academy.rickandmorty.dto.internal;
+
+public record PersonageResponseDto(Long id,
+ Long externalId,
+ String name,
+ String status,
+ String gender) {
+}
diff --git a/src/main/java/mate/academy/rickandmorty/excpetion/CustomGlobalExceptionHandler.java b/src/main/java/mate/academy/rickandmorty/excpetion/CustomGlobalExceptionHandler.java
new file mode 100644
index 00000000..b5ec28a6
--- /dev/null
+++ b/src/main/java/mate/academy/rickandmorty/excpetion/CustomGlobalExceptionHandler.java
@@ -0,0 +1,16 @@
+package mate.academy.rickandmorty.excpetion;
+
+import jakarta.persistence.EntityNotFoundException;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
+
+@ControllerAdvice
+public class CustomGlobalExceptionHandler extends ResponseEntityExceptionHandler {
+ @ExceptionHandler(EntityNotFoundException.class)
+ public ResponseEntity