From 8f827f07627ed4f816ccedff42d17baeb604d773 Mon Sep 17 00:00:00 2001 From: Yehor Bublyk Date: Sat, 23 Apr 2022 22:20:50 +0300 Subject: [PATCH] swagger documentation appended, bugs fixed --- .../spring/config/SwaggerConfig.java | 1 + .../controller/rest/AuthorRestController.java | 34 +++++++++++++++++++ .../controller/rest/BookRestController.java | 34 +++++++++++++++++-- .../spring/dto/author/AuthorCreateDto.java | 3 ++ .../spring/dto/author/AuthorUpdateDto.java | 3 ++ .../spring/dto/book/BookSearchDto.java | 2 ++ .../spring/dto/book/BookUpdateDto.java | 4 ++- .../spring/repository/StubBookRepository.java | 12 +++++-- .../spring/service/AuthorService.java | 1 + .../spring/service/BookService.java | 1 + 10 files changed, 89 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/multipleton/spring/config/SwaggerConfig.java b/src/main/java/com/multipleton/spring/config/SwaggerConfig.java index 7fc3e36..011bce6 100644 --- a/src/main/java/com/multipleton/spring/config/SwaggerConfig.java +++ b/src/main/java/com/multipleton/spring/config/SwaggerConfig.java @@ -25,6 +25,7 @@ public Docket api() { .paths(PathSelectors.any()) .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) .build() + .useDefaultResponseMessages(false) .apiInfo(new ApiInfoBuilder() .title(TITLE) .description(DESCRIPTION) diff --git a/src/main/java/com/multipleton/spring/controller/rest/AuthorRestController.java b/src/main/java/com/multipleton/spring/controller/rest/AuthorRestController.java index fd766b8..bc7868a 100644 --- a/src/main/java/com/multipleton/spring/controller/rest/AuthorRestController.java +++ b/src/main/java/com/multipleton/spring/controller/rest/AuthorRestController.java @@ -6,10 +6,15 @@ import com.multipleton.spring.dto.book.BookDto; import com.multipleton.spring.service.AuthorService; import com.multipleton.spring.service.BookService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; import org.springframework.web.bind.annotation.*; import java.util.List; +@Api(tags = "Authors") @RestController @RequestMapping("/rest") public class AuthorRestController extends FrontRestController { @@ -22,32 +27,61 @@ public AuthorRestController(AuthorService authorService, BookService bookService this.bookService = bookService; } + @ApiOperation("Get all authors") + @ApiResponses({ + @ApiResponse(code = 200, message = "OK") + }) @GetMapping("/authors") public List getAllAuthors() { return authorService.findAllAuthors(); } + @ApiOperation("Get author") + @ApiResponses({ + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 404, message = "NOT_FOUND") + }) @GetMapping("/authors/{authorId}") public AuthorDto getAuthor(@PathVariable Long authorId) { return authorService.getAuthor(authorId); } + @ApiOperation("Create author") + @ApiResponses({ + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 400, message = "BAD_REQUEST") + }) @PostMapping("/authors") public AuthorDto createAuthor(@RequestBody AuthorCreateDto dto) { return authorService.createAuthor(dto); } + @ApiOperation("Update author") + @ApiResponses({ + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 400, message = "BAD_REQUEST"), + @ApiResponse(code = 404, message = "NOT_FOUND") + }) @PutMapping("/authors/{authorId}") public AuthorDto updateAuthor(@PathVariable Long authorId, @RequestBody AuthorUpdateDto dto) { return authorService.updateAuthor(authorId, dto); } + @ApiOperation("Delete author") + @ApiResponses({ + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 404, message = "NOT_FOUND") + }) @DeleteMapping("/authors/{authorId}") public void deleteAuthor(@PathVariable Long authorId) { authorService.deleteAuthor(authorId); } + @ApiOperation("Get author books") + @ApiResponses({ + @ApiResponse(code = 200, message = "OK") + }) @GetMapping("/authors/{authorId}/books") public List getAuthorBooks(@PathVariable Long authorId) { return bookService.findBooksByAuthorId(authorId); diff --git a/src/main/java/com/multipleton/spring/controller/rest/BookRestController.java b/src/main/java/com/multipleton/spring/controller/rest/BookRestController.java index 93ed8c9..8ce0f9e 100644 --- a/src/main/java/com/multipleton/spring/controller/rest/BookRestController.java +++ b/src/main/java/com/multipleton/spring/controller/rest/BookRestController.java @@ -5,10 +5,15 @@ import com.multipleton.spring.dto.book.BookSearchDto; import com.multipleton.spring.dto.book.BookUpdateDto; import com.multipleton.spring.service.BookService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.web.bind.annotation.*; +@Api(tags = "Books") @RestController @RequestMapping("/rest") public class BookRestController extends FrontRestController { @@ -19,31 +24,56 @@ public BookRestController(BookService bookService) { this.bookService = bookService; } + @ApiOperation("Get book") + @ApiResponses({ + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 404, message = "NOT_FOUND") + }) @GetMapping("/books/{bookId}") public BookDto getBook(@PathVariable Long bookId) { return bookService.getBook(bookId); } + @ApiOperation("Create book") + @ApiResponses({ + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 400, message = "BAD_REQUEST") + }) @PostMapping("/books") public BookDto createBook(@RequestBody BookCreateDto dto) { return bookService.createBook(dto); } + @ApiOperation("Update book") + @ApiResponses({ + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 400, message = "BAD_REQUEST"), + @ApiResponse(code = 404, message = "NOT_FOUND") + }) @PutMapping("/books/{bookId}") public BookDto updateBook(@PathVariable Long bookId, @RequestBody BookUpdateDto dto) { return bookService.updateBook(bookId, dto); } + @ApiOperation("Delete book") + @ApiResponses({ + @ApiResponse(code = 200, message = "OK"), + @ApiResponse(code = 404, message = "NOT_FOUND") + }) @DeleteMapping("/books/{bookId}") public void deleteBook(@PathVariable Long bookId) { bookService.deleteBook(bookId); } + @ApiOperation("Search books") + @ApiResponses({ + @ApiResponse(code = 200, message = "OK") + }) @GetMapping("/books/search") public Page searchBooks(BookSearchDto dto, - @RequestParam(value = "page", defaultValue = "0") Integer page, - @RequestParam(value = "size", defaultValue = "20") Integer size) { + @RequestParam(defaultValue = "0") Integer page, + @RequestParam(defaultValue = "20") Integer size) { return bookService.searchBooks(dto, PageRequest.of(page, size)); } diff --git a/src/main/java/com/multipleton/spring/dto/author/AuthorCreateDto.java b/src/main/java/com/multipleton/spring/dto/author/AuthorCreateDto.java index ca4844c..94b03a0 100644 --- a/src/main/java/com/multipleton/spring/dto/author/AuthorCreateDto.java +++ b/src/main/java/com/multipleton/spring/dto/author/AuthorCreateDto.java @@ -9,6 +9,9 @@ public AuthorCreateDto(String name) { this.name = name; } + public AuthorCreateDto() { + } + public String getName() { return name; } diff --git a/src/main/java/com/multipleton/spring/dto/author/AuthorUpdateDto.java b/src/main/java/com/multipleton/spring/dto/author/AuthorUpdateDto.java index 658a25a..ab18326 100644 --- a/src/main/java/com/multipleton/spring/dto/author/AuthorUpdateDto.java +++ b/src/main/java/com/multipleton/spring/dto/author/AuthorUpdateDto.java @@ -9,6 +9,9 @@ public AuthorUpdateDto(String name) { this.name = name; } + public AuthorUpdateDto() { + } + public String getName() { return name; } diff --git a/src/main/java/com/multipleton/spring/dto/book/BookSearchDto.java b/src/main/java/com/multipleton/spring/dto/book/BookSearchDto.java index 9f6eec2..4865092 100644 --- a/src/main/java/com/multipleton/spring/dto/book/BookSearchDto.java +++ b/src/main/java/com/multipleton/spring/dto/book/BookSearchDto.java @@ -1,6 +1,7 @@ package com.multipleton.spring.dto.book; import io.swagger.annotations.ApiModelProperty; +import io.swagger.annotations.ApiParam; import java.util.Arrays; import java.util.HashSet; @@ -10,6 +11,7 @@ public class BookSearchDto { private String title; private String author; + @ApiParam(value = "values should be separated by comma") private String tags; public BookSearchDto(String title, String author, String tags) { diff --git a/src/main/java/com/multipleton/spring/dto/book/BookUpdateDto.java b/src/main/java/com/multipleton/spring/dto/book/BookUpdateDto.java index ac81a3b..238f34e 100644 --- a/src/main/java/com/multipleton/spring/dto/book/BookUpdateDto.java +++ b/src/main/java/com/multipleton/spring/dto/book/BookUpdateDto.java @@ -50,6 +50,8 @@ public void updateBook(Book book) { .collect(Collectors.toSet()); book.setTitle(title); book.setTags(tags); - book.setStatus(status); + if (status != null) { + book.setStatus(status); + } } } diff --git a/src/main/java/com/multipleton/spring/repository/StubBookRepository.java b/src/main/java/com/multipleton/spring/repository/StubBookRepository.java index 81c2a10..25b492a 100644 --- a/src/main/java/com/multipleton/spring/repository/StubBookRepository.java +++ b/src/main/java/com/multipleton/spring/repository/StubBookRepository.java @@ -27,9 +27,15 @@ public Page findAllByTitleAndTagsAndAuthor_Name(String title, Set .filter(book -> name == null || name.isEmpty() || book.getAuthor().getName().contains(name)) .sorted(Comparator.comparing(Book::getId)) .collect(Collectors.toList()); - int start = Math.min(pageable.getPageNumber() * pageable.getPageSize(), entries.size()); - int end = Math.min(start + pageable.getPageSize(), entries.size()); - return new PageImpl<>(entries.subList(start, end), pageable, entries.size()); + List result; + if (pageable.isPaged()) { + int start = Math.min(pageable.getPageNumber() * pageable.getPageSize(), entries.size()); + int end = Math.min(start + pageable.getPageSize(), entries.size()); + result = entries.subList(start, end); + } else { + result = entries; + } + return new PageImpl<>(result, pageable, entries.size()); } @Override diff --git a/src/main/java/com/multipleton/spring/service/AuthorService.java b/src/main/java/com/multipleton/spring/service/AuthorService.java index e73c103..2eeb80f 100644 --- a/src/main/java/com/multipleton/spring/service/AuthorService.java +++ b/src/main/java/com/multipleton/spring/service/AuthorService.java @@ -41,6 +41,7 @@ public AuthorDto updateAuthor(Long id, AuthorUpdateDto dto) { } public void deleteAuthor(Long id) { + findAuthor(id); authorRepository.deleteById(id); } diff --git a/src/main/java/com/multipleton/spring/service/BookService.java b/src/main/java/com/multipleton/spring/service/BookService.java index acaa21f..62ba834 100644 --- a/src/main/java/com/multipleton/spring/service/BookService.java +++ b/src/main/java/com/multipleton/spring/service/BookService.java @@ -48,6 +48,7 @@ public BookDto updateBook(Long id, BookUpdateDto bookUpdateDto) { } public void deleteBook(Long id) { + findBook(id); bookRepository.deleteById(id); }