Skip to content

Commit

Permalink
Merge pull request #1 from nuttawut25983/fix-pagination
Browse files Browse the repository at this point in the history
fix pagination
  • Loading branch information
meteedev authored Mar 24, 2024
2 parents 26eaabc + 0be14e9 commit 464bde1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
Expand Down Expand Up @@ -36,13 +36,11 @@ public ProductController(ProductService productService) {
mediaType = "application/json",
schema = @Schema(implementation = NotFoundException.class)))
@GetMapping("/products")
public List<ProductResponse> getProducts(
@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "limit", required = false) Integer limit) {
if (page == null || limit == null) {
return productService.getAll();
}
return productService.getProductsByPage(page, limit);
public Page<ProductResponse> getProducts(
@RequestParam(value = "page", defaultValue = "0") Integer page,
@RequestParam(value = "size", defaultValue = "20") Integer size) {

return productService.getProductsByPage(page, size);
}

@ApiResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.kampus.kbazaar.exceptions.NotFoundException;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

Expand All @@ -19,10 +20,8 @@ public List<ProductResponse> getAll() {
return productRepository.findAll().stream().map(Product::toResponse).toList();
}

public List<ProductResponse> getProductsByPage(Integer page, Integer limit) {
return productRepository.findAll(PageRequest.of(page, limit)).stream()
.map(Product::toResponse)
.toList();
public Page<ProductResponse> getProductsByPage(Integer page, Integer size) {
return productRepository.findAll(PageRequest.of(page, size)).map(Product::toResponse);
}

public ProductResponse getBySku(String sku) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
Expand Down Expand Up @@ -48,12 +49,12 @@ public void shouldReturnAllProduct() throws Exception {
// Given

// When & Then
when(productService.getAll()).thenReturn(new ArrayList<>());
when(productService.getProductsByPage(0, 20)).thenReturn(new PageImpl<ProductResponse>(new ArrayList<>()));

mockMvc.perform(get("/api/v1/products").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());

verify(productService, times(1)).getAll();
verify(productService, times(1)).getProductsByPage(0, 20);
}

@Test
Expand Down

0 comments on commit 464bde1

Please sign in to comment.