Skip to content

Commit

Permalink
Replace Page<> with Slice<> for pagination operations
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Dec 27, 2023
1 parent eb8441b commit 5dc6d5d
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
import java.util.List;
import javax.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
Expand Down Expand Up @@ -108,7 +107,7 @@ public void deleteArticle(
articleService.delete(id);
}

@ApiOperation(value = "Read articles paged", response = Page.class)
@ApiOperation(value = "Read articles paged", response = Slice.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Articles successfully retrieved"),
})
Expand All @@ -125,7 +124,7 @@ public void deleteArticle(
"Multiple sort criteria are supported.",
defaultValue = "title,ASC")
})
public Page<ArticleDto> readArticlesPaged(
public Slice<ArticleDto> readArticlesPaged(
@PageableDefault(page = 0, size = 50)
@SortDefault.SortDefaults({
@SortDefault(sort = "title", direction = Sort.Direction.ASC)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/by/andd3dfx/templateapp/dto/LocationDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class LocationDto {

private String country;
private String city;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package by.andd3dfx.templateapp.persistence.dao;

import by.andd3dfx.templateapp.persistence.entities.Article;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ArticleRepository extends PagingAndSortingRepository<Article, Long>, ArticleRepositoryCustom {
public interface ArticleRepository extends CrudRepository<Article, Long>, ArticleRepositoryCustom {

List<Article> getArticleByCountryNCity(String country, String city);

Slice<Article> findAll(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@NoArgsConstructor
@Data
public class Location {

private String country;
private String city;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import by.andd3dfx.templateapp.dto.ArticleDto;
import by.andd3dfx.templateapp.dto.ArticleUpdateDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

import java.util.List;

Expand All @@ -17,7 +17,7 @@ public interface IArticleService {

void delete(Long id);

Page<ArticleDto> getAll(Pageable pageable);
Slice<ArticleDto> getAll(Pageable pageable);

List<ArticleDto> getArticleByLocation(String country, String city);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -62,8 +63,8 @@ public void delete(Long id) {

@Transactional(readOnly = true)
@Override
public Page<ArticleDto> getAll(Pageable pageable) {
final Page<Article> pagedResult = articleRepository.findAll(pageable);
public Slice<ArticleDto> getAll(Pageable pageable) {
Slice<Article> pagedResult = articleRepository.findAll(pageable);
return pagedResult.map(articleMapper::toArticleDto);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import by.andd3dfx.templateapp.dto.ArticleDto;
import by.andd3dfx.templateapp.dto.LocationDto;
import by.andd3dfx.templateapp.persistence.dao.ArticleRepository;
import by.andd3dfx.templateapp.persistence.entities.Article;
import by.andd3dfx.templateapp.services.impl.ArticleService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,14 +20,14 @@ class SpringBootTemplateApplicationTests {

@Test
void contextLoads() {
ArticleDto article = new ArticleDto();
var article = new ArticleDto();
article.setTitle("Voina i mir");
article.setAuthor("Lev Tolstoj");
article.setText("Some text");
article.setLocation(new LocationDto("US", "New York"));
ArticleDto createdArticle = articleService.create(article);
var createdArticle = articleService.create(article);

ArticleDto savedArticle = articleService.get(createdArticle.getId());
var savedArticle = articleService.get(createdArticle.getId());
assertThat(savedArticle.getTitle(), is(article.getTitle()));
assertThat(savedArticle.getAuthor(), is(article.getAuthor()));
assertThat(savedArticle.getText(), is(article.getText()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package by.andd3dfx.templateapp.persistence.dao;

import by.andd3dfx.templateapp.IntegrationTestInitializer;
import by.andd3dfx.templateapp.persistence.entities.Article;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.ContextConfiguration;

import java.time.LocalDateTime;
import java.util.Arrays;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

@ContextConfiguration(initializers = IntegrationTestInitializer.class)
@SpringBootTest
class ArticleRepositoryTest {

@Autowired
private ArticleRepository repository;

private Article entity;
private Article entity2;
private Article entity3;

@BeforeEach
public void setup() {
repository.deleteAll();
entity = buildArticle("Ivan", "HD", LocalDateTime.parse("2010-12-03T10:15:30"));
entity2 = buildArticle("Vasily", "HD", LocalDateTime.parse("2011-12-03T10:15:30"));
entity3 = buildArticle("Ivan", "4K", LocalDateTime.parse("2012-12-03T10:15:30"));
repository.saveAll(Arrays.asList(entity, entity2, entity3));
}

@AfterEach
public void tearDown() {
repository.deleteAll();
}

@Test
public void findAll() {
var result = repository.findAll(Pageable.ofSize(10));

assertThat("Wrong records amount", result.getNumberOfElements(), is(3));
}

@Test
public void findAll_withPageNSizeNSorting() {
var result = repository.findAll(PageRequest.of(0, 2, Sort.by("title", "summary")));

assertThat("Wrong records amount", result.getNumberOfElements(), is(2));
var articles = result.getContent();

assertThat(articles.get(0).getTitle(), is(entity3.getTitle()));
assertThat(articles.get(0).getSummary(), is(entity3.getSummary()));

assertThat(articles.get(1).getTitle(), is(entity.getTitle()));
assertThat(articles.get(1).getSummary(), is(entity.getSummary()));
}

private static Article buildArticle(String title, String summary, LocalDateTime timestamp) {
var result = new Article();
result.setTitle(title);
result.setSummary(summary);
result.setText("any text");
result.setTimestamp(timestamp);
result.setAuthor("Some author");
return result;
}
}

0 comments on commit 5dc6d5d

Please sign in to comment.