Skip to content

Commit

Permalink
Merge pull request #5 from multipleton/lw-4
Browse files Browse the repository at this point in the history
Laboratory work 4
  • Loading branch information
yehorbk authored Apr 23, 2022
2 parents 2c609a4 + 8f827f0 commit f9fdd4f
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 10 deletions.
17 changes: 16 additions & 1 deletion 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>2.6.3</version>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.multipleton</groupId>
Expand All @@ -15,6 +15,7 @@
<description>Spring Labs KPI</description>
<properties>
<java.version>1.8</java.version>
<swagger.version>2.9.2</swagger.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -29,7 +30,21 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/multipleton/spring/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
Expand All @@ -18,7 +19,9 @@
import java.util.List;
import java.util.Set;

@SpringBootApplication(exclude = {UserDetailsServiceAutoConfiguration.class})
@SpringBootApplication(exclude = {
UserDetailsServiceAutoConfiguration.class,
DataSourceAutoConfiguration.class})
@EnableWebSecurity
public class Application {

Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/multipleton/spring/config/SwaggerConfig.java
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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.multipleton.spring.service.AuthorService;
import com.multipleton.spring.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -33,7 +34,7 @@ public void setAuthorService(AuthorService authorService) {
@GetMapping
public String books(BookSearchDto bookSearchDto, Model model) {
model.addAttribute("bookSearchDto", bookSearchDto);
model.addAttribute("books", bookService.searchBooks(bookSearchDto));
model.addAttribute("books", bookService.searchBooks(bookSearchDto, Pageable.unpaged()));
return "books";
}

Expand Down
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);
}

}
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));
}

}
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());
}

}
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,5 +1,8 @@
package com.multipleton.spring.dto.book;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -8,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 Expand Up @@ -36,6 +40,7 @@ public String getTags() {
return tags;
}

@ApiModelProperty(hidden = true)
public Set<String> getTagsSet() {
if (tags == null || tags.isEmpty()) {
return new HashSet<>();
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
@@ -1,6 +1,8 @@
package com.multipleton.spring.repository;

import com.multipleton.spring.model.Book;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Optional;
Expand All @@ -10,7 +12,9 @@ public interface BookRepository {

List<Book> findAll();

List<Book> findAllByTitleAndTagsAndAuthor_Name(String title, Set<String> tags, String name);
Page<Book> findAllByTitleAndTagsAndAuthor_Name(String title, Set<String> tags, String name, Pageable pageable);

List<Book> findAllByAuthor_Id(Long authorId);

Optional<Book> findById(Long id);

Expand Down
Loading

0 comments on commit f9fdd4f

Please sign in to comment.