Skip to content

Commit

Permalink
add swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorromaniuk committed Dec 8, 2024
1 parent d102963 commit ddeb328
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
20 changes: 15 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<version>3.4.0</version>
<relativePath/>
</parent>
<groupId>mate.academy</groupId>
Expand All @@ -15,26 +15,26 @@
<description>jv-rick-and-morty</description>
<properties>
<java.version>17</java.version>
<maven.checkstyle.plugin.version>3.1.1</maven.checkstyle.plugin.version>
<maven.checkstyle.plugin.configLocation>
https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml
</maven.checkstyle.plugin.configLocation>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
<mapstruct.version>1.6.3</mapstruct.version>
<lombok.version>1.18.36</lombok.version>
<openapi.version>2.7.0</openapi.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
Expand All @@ -57,6 +57,16 @@
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok.mapstruct.binding.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${openapi.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
<version>${openapi.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import mate.academy.rickandmorty.exception.MappingException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@RequiredArgsConstructor
Expand All @@ -35,6 +36,7 @@ public List<ExternalCharacterDto> fetchAllCharacters() {
int page = FIRST_PAGE;
int status;
List<ExternalCharacterDto> responseCharacterDtoList = new ArrayList<>();
boolean isListFinished = false;

do {
HttpRequest request = HttpRequest.newBuilder()
Expand All @@ -53,7 +55,7 @@ public List<ExternalCharacterDto> fetchAllCharacters() {
}
status = response.statusCode();

if (status == HttpStatus.OK.value()) {
if (status == HttpStatus.OK.value() && StringUtils.hasText(response.body())) {
try {
ResponseAllCharactersDto charactersDto = objectMapper
.readValue(response.body(), new TypeReference<>() {});
Expand All @@ -62,9 +64,11 @@ public List<ExternalCharacterDto> fetchAllCharacters() {
throw new MappingException("Can't map response body to "
+ "ResponseAllCharactersDto object. Body: " + response.body(), e);
}
} else {
isListFinished = true;
}
page++;
} while (status == HttpStatus.OK.value());
} while (status == HttpStatus.OK.value() && !isListFinished);

return responseCharacterDtoList;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package mate.academy.rickandmorty.controller;

import io.swagger.v3.oas.annotations.Operation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import mate.academy.rickandmorty.dto.internal.InternalCharacterDto;
import mate.academy.rickandmorty.service.CharacterService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -16,14 +19,17 @@
public class CharacterController {
private final CharacterService characterService;

@GetMapping("/random")
@Operation(summary = "Get random \"Rick and Morty\" character")
@GetMapping(value = "/random", produces = MediaType.APPLICATION_JSON_VALUE)
public InternalCharacterDto getRandomCharacter() {
return characterService.getRandomCharacter();
}

@GetMapping
@Operation(summary = "Get \"Rick and Morty\" characters list by name like with pagination")
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<InternalCharacterDto> getCharactersWithNameLike(
@RequestParam("partName") String partName, Pageable pageable) {
return characterService.getAllCharactersWithNameLike(partName, pageable);
@RequestParam("namePart") String namePart,
@ParameterObject() Pageable pageable) {
return characterService.getAllCharactersWithNameLike(namePart, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public interface CharacterService {

InternalCharacterDto getRandomCharacter();

List<InternalCharacterDto> getAllCharactersWithNameLike(String partName, Pageable pageable);
List<InternalCharacterDto> getAllCharactersWithNameLike(String namePart, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public InternalCharacterDto getRandomCharacter() {
}

@Override
public List<InternalCharacterDto> getAllCharactersWithNameLike(String partName,
public List<InternalCharacterDto> getAllCharactersWithNameLike(String namePart,
Pageable pageable) {
return repository.getAllByNameLikeIgnoreCase("%" + partName + "%", pageable)
return repository.getAllByNameLikeIgnoreCase("%" + namePart + "%", pageable)
.stream()
.map(characterMapper::toDto)
.toList();
Expand Down

0 comments on commit ddeb328

Please sign in to comment.