Skip to content

Commit

Permalink
swagger documentation appended, bugs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yehorbk committed Apr 23, 2022
1 parent ca36304 commit 8f827f0
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<AuthorDto> 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<BookDto> getAuthorBooks(@PathVariable Long authorId) {
return bookService.findBooksByAuthorId(authorId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<BookDto> 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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public AuthorCreateDto(String name) {
this.name = name;
}

public AuthorCreateDto() {
}

public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public AuthorUpdateDto(String name) {
this.name = name;
}

public AuthorUpdateDto() {
}

public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ public Page<Book> findAllByTitleAndTagsAndAuthor_Name(String title, Set<String>
.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<Book> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public AuthorDto updateAuthor(Long id, AuthorUpdateDto dto) {
}

public void deleteAuthor(Long id) {
findAuthor(id);
authorRepository.deleteById(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public BookDto updateBook(Long id, BookUpdateDto bookUpdateDto) {
}

public void deleteBook(Long id) {
findBook(id);
bookRepository.deleteById(id);
}

Expand Down

0 comments on commit 8f827f0

Please sign in to comment.