Skip to content

Commit

Permalink
pagination on book search endpoint added
Browse files Browse the repository at this point in the history
  • Loading branch information
yehorbk committed Apr 23, 2022
1 parent 139130a commit ca36304
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 14 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<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>
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
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
Expand Up @@ -5,10 +5,10 @@
import com.multipleton.spring.dto.book.BookSearchDto;
import com.multipleton.spring.dto.book.BookUpdateDto;
import com.multipleton.spring.service.BookService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/rest")
public class BookRestController extends FrontRestController {
Expand Down Expand Up @@ -41,8 +41,10 @@ public void deleteBook(@PathVariable Long bookId) {
}

@GetMapping("/books/search")
public List<BookDto> searchBooks(BookSearchDto dto) {
return bookService.searchBooks(dto);
public Page<BookDto> searchBooks(BookSearchDto dto,
@RequestParam(value = "page", defaultValue = "0") Integer page,
@RequestParam(value = "size", defaultValue = "20") Integer size) {
return bookService.searchBooks(dto, PageRequest.of(page, size));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.multipleton.spring.dto.book;

import io.swagger.annotations.ApiModelProperty;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -36,6 +38,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
@@ -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,7 @@ 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);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.multipleton.spring.repository;

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

import java.util.*;
Expand All @@ -17,12 +20,16 @@ public List<Book> findAll() {
}

@Override
public List<Book> findAllByTitleAndTagsAndAuthor_Name(String title, Set<String> tags, String name) {
return books.stream()
public Page<Book> findAllByTitleAndTagsAndAuthor_Name(String title, Set<String> tags, String name, Pageable pageable) {
List<Book> entries = books.stream()
.filter(book -> title == null || title.isEmpty() || book.getTitle().contains(title))
.filter(book -> tags.isEmpty() || book.getTags().containsAll(tags))
.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());
}

@Override
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/multipleton/spring/service/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.multipleton.spring.model.Book;
import com.multipleton.spring.repository.BookRepository;
import com.multipleton.spring.service.exception.EntityNotFoundException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
Expand Down Expand Up @@ -56,11 +58,12 @@ public List<BookDto> findAllBooks() {
.collect(Collectors.toList());
}

public List<BookDto> searchBooks(BookSearchDto dto) {
return bookRepository.findAllByTitleAndTagsAndAuthor_Name(dto.getTitle(), dto.getTagsSet(), dto.getAuthor())
.stream()
.map(BookDto::from)
.collect(Collectors.toList());
public Page<BookDto> searchBooks(BookSearchDto dto, Pageable pageable) {
return bookRepository.findAllByTitleAndTagsAndAuthor_Name(dto.getTitle(),
dto.getTagsSet(),
dto.getAuthor(),
pageable)
.map(BookDto::from);
}

public List<BookDto> findBooksByAuthorId(Long authorId) {
Expand Down

0 comments on commit ca36304

Please sign in to comment.