-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from multipleton/lw-4
Laboratory work 4
- Loading branch information
Showing
16 changed files
with
305 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/com/multipleton/spring/config/SwaggerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.multipleton.spring.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import springfox.documentation.builders.ApiInfoBuilder; | ||
import springfox.documentation.builders.PathSelectors; | ||
import springfox.documentation.builders.RequestHandlerSelectors; | ||
import springfox.documentation.spi.DocumentationType; | ||
import springfox.documentation.spring.web.plugins.Docket; | ||
import springfox.documentation.swagger2.annotations.EnableSwagger2; | ||
|
||
@Configuration | ||
@EnableSwagger2 | ||
public class SwaggerConfig { | ||
|
||
private static final String TITLE = "Spring Labs API"; | ||
private static final String DESCRIPTION = "Library Catalog"; | ||
private static final String VERSION = "v1.0.0"; | ||
private static final String LICENSE = "MIT"; | ||
|
||
@Bean | ||
public Docket api() { | ||
return new Docket(DocumentationType.SWAGGER_2).select() | ||
.paths(PathSelectors.any()) | ||
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) | ||
.build() | ||
.useDefaultResponseMessages(false) | ||
.apiInfo(new ApiInfoBuilder() | ||
.title(TITLE) | ||
.description(DESCRIPTION) | ||
.version(VERSION) | ||
.license(LICENSE) | ||
.build()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/com/multipleton/spring/controller/rest/AuthorRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.multipleton.spring.controller.rest; | ||
|
||
import com.multipleton.spring.dto.author.AuthorCreateDto; | ||
import com.multipleton.spring.dto.author.AuthorDto; | ||
import com.multipleton.spring.dto.author.AuthorUpdateDto; | ||
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 { | ||
|
||
private final AuthorService authorService; | ||
private final BookService bookService; | ||
|
||
public AuthorRestController(AuthorService authorService, BookService bookService) { | ||
this.authorService = authorService; | ||
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); | ||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/multipleton/spring/controller/rest/BookRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.multipleton.spring.controller.rest; | ||
|
||
import com.multipleton.spring.dto.book.BookCreateDto; | ||
import com.multipleton.spring.dto.book.BookDto; | ||
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 { | ||
|
||
private BookService bookService; | ||
|
||
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(defaultValue = "0") Integer page, | ||
@RequestParam(defaultValue = "20") Integer size) { | ||
return bookService.searchBooks(dto, PageRequest.of(page, size)); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/multipleton/spring/controller/rest/FrontRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.multipleton.spring.controller.rest; | ||
|
||
import com.multipleton.spring.service.exception.EntityNotFoundException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
public class FrontRestController { | ||
|
||
@ExceptionHandler | ||
public void handleEntityNotFoundException(final EntityNotFoundException ex, | ||
final HttpServletResponse response) throws IOException { | ||
response.sendError(HttpStatus.NOT_FOUND.value(), ex.getMessage()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.