diff --git a/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/category/CategoryController.java b/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/category/CategoryController.java index ec3819f..da0d45c 100644 --- a/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/category/CategoryController.java +++ b/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/category/CategoryController.java @@ -1,16 +1,21 @@ package com.lcaohoanq.shoppe.domain.category; +import com.lcaohoanq.shoppe.api.PageResponse; import com.lcaohoanq.shoppe.component.LocalizationUtils; import com.lcaohoanq.shoppe.api.ApiResponse; +import com.lcaohoanq.shoppe.domain.product.Product; +import com.lcaohoanq.shoppe.domain.product.ProductResponse; import com.lcaohoanq.shoppe.domain.subcategory.CreateNewSubcategoryResponse; import com.lcaohoanq.shoppe.domain.subcategory.SubcategoryDTO; import com.lcaohoanq.shoppe.domain.subcategory.SubcategoryResponse; import com.lcaohoanq.shoppe.exception.MethodArgumentNotValidException; import com.lcaohoanq.shoppe.util.DTOConverter; import jakarta.validation.Valid; +import java.util.HashSet; import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.PageRequest; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; @@ -22,6 +27,7 @@ import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @@ -151,6 +157,17 @@ public ResponseEntity> delete(@PathVariable int id .build() ); } - + + @GetMapping("/{id}/products") + @PreAuthorize("permitAll()") + public ResponseEntity> getProductsByCategoryId( + @PathVariable long id, + @RequestParam(defaultValue = "0") int page, + @RequestParam(defaultValue = "50") int limit + ) { + return ResponseEntity.ok( + categoryService.findByCategoryId(id, PageRequest.of(page, limit)) + ); + } } diff --git a/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/category/CategoryService.java b/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/category/CategoryService.java index 5327ebf..1e2bf02 100755 --- a/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/category/CategoryService.java +++ b/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/category/CategoryService.java @@ -1,5 +1,9 @@ package com.lcaohoanq.shoppe.domain.category; +import com.lcaohoanq.shoppe.api.PageResponse; +import com.lcaohoanq.shoppe.domain.product.Product; +import com.lcaohoanq.shoppe.domain.product.ProductRepository; +import com.lcaohoanq.shoppe.domain.product.ProductResponse; import com.lcaohoanq.shoppe.domain.subcategory.CreateNewSubcategoryResponse; import com.lcaohoanq.shoppe.domain.subcategory.Subcategory; import com.lcaohoanq.shoppe.domain.subcategory.SubcategoryDTO; @@ -11,9 +15,12 @@ import com.lcaohoanq.shoppe.base.exception.DataNotFoundException; import com.lcaohoanq.shoppe.util.DTOConverter; import com.lcaohoanq.shoppe.util.PaginationConverter; +import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @Service @@ -23,6 +30,7 @@ public class CategoryService implements ICategoryService, DTOConverter, private final CategoryRepository categoryRepository; private final SubcategoryRepository subcategoryRepository; + private final ProductRepository productRepository; @Override public List createCategory(CategoryDTO category) @@ -111,4 +119,18 @@ public CreateNewSubcategoryResponse createSubCategory(SubcategoryDTO subcategory } + + @Override + public PageResponse findByCategoryId(Long categoryId, Pageable pageable) { + if(getById(categoryId) == null){ + throw new DataNotFoundException("Category not found"); + } + Page productPage = productRepository.findByCategoryId(categoryId, pageable); + return mapPageResponse( + productPage, + pageable, + this::toProductResponse, + "Get products by category successfully" + ); + } } diff --git a/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/category/ICategoryService.java b/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/category/ICategoryService.java index c241aff..4e899c9 100755 --- a/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/category/ICategoryService.java +++ b/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/category/ICategoryService.java @@ -1,11 +1,16 @@ package com.lcaohoanq.shoppe.domain.category; +import com.lcaohoanq.shoppe.api.PageResponse; import com.lcaohoanq.shoppe.base.exception.DataAlreadyExistException; import com.lcaohoanq.shoppe.base.exception.DataNotFoundException; +import com.lcaohoanq.shoppe.domain.product.Product; +import com.lcaohoanq.shoppe.domain.product.ProductResponse; import com.lcaohoanq.shoppe.domain.subcategory.CreateNewSubcategoryResponse; import com.lcaohoanq.shoppe.domain.subcategory.SubcategoryDTO; import com.lcaohoanq.shoppe.domain.subcategory.SubcategoryResponse; +import java.util.HashSet; import java.util.List; +import org.springframework.data.domain.Pageable; public interface ICategoryService { @@ -16,5 +21,5 @@ public interface ICategoryService { void delete(long id); SubcategoryResponse getSubCategory(long subcategoryId) throws DataNotFoundException; CreateNewSubcategoryResponse createSubCategory(SubcategoryDTO subcategoryDTO) throws DataNotFoundException, DataAlreadyExistException; - + PageResponse findByCategoryId(Long categoryId, Pageable pageable); } diff --git a/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/product/ProductRepository.java b/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/product/ProductRepository.java index 82ee2c6..d4484fb 100644 --- a/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/product/ProductRepository.java +++ b/SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/product/ProductRepository.java @@ -2,6 +2,8 @@ import java.util.HashSet; import java.util.Optional; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; @@ -34,4 +36,6 @@ void decreaseProductQuantity( @Query("SELECT COALESCE(SUM(p.quantity), 0) FROM Product p WHERE p.warehouse.id = :warehouseId") Long countTotalQuantityByWarehouseId(@Param("warehouseId") Long warehouseId); + Page findByCategoryId(Long categoryId, Pageable pageable); + }