Skip to content

Commit

Permalink
Merge branch 'Story3'
Browse files Browse the repository at this point in the history
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
  • Loading branch information
gawdwarf committed Mar 23, 2024
2 parents a0ccf0c + 4eff5f6 commit c29d7b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1")
Expand Down Expand Up @@ -39,8 +36,13 @@ public ProductController(ProductService productService) {
mediaType = "application/json",
schema = @Schema(implementation = NotFoundException.class)))
@GetMapping("/products")
public List<ProductResponse> getProducts() {
return productService.getAll();
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);
}

@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.PageRequest;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -18,6 +19,12 @@ 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 ProductResponse getBySku(String sku) {
Optional<Product> product = productRepository.findBySku(sku);
if (product.isEmpty()) {
Expand Down

0 comments on commit c29d7b3

Please sign in to comment.