From 3280e133b6b8604cbd08299913a919a7acc9ae03 Mon Sep 17 00:00:00 2001 From: sojungpp Date: Tue, 8 Aug 2023 00:11:31 +0900 Subject: [PATCH 1/3] =?UTF-8?q?#78=20fix:=20=EC=83=81=ED=92=88=20=EB=A9=94?= =?UTF-8?q?=EC=9D=B8=20=EC=A1=B0=ED=9A=8C=20=ED=8E=98=EC=9D=B4=EC=A7=95=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/controller/ProductController.kt | 8 ++- .../product/dto/response/GetProductsRes.kt | 4 +- .../repository/product/ProductCustom.kt | 5 +- .../product/ProductRepositoryImpl.kt | 49 +++++++++++++------ .../psr/psr/product/service/ProductService.kt | 5 +- 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/com/psr/psr/product/controller/ProductController.kt b/src/main/kotlin/com/psr/psr/product/controller/ProductController.kt index 097b032..92a6cb6 100644 --- a/src/main/kotlin/com/psr/psr/product/controller/ProductController.kt +++ b/src/main/kotlin/com/psr/psr/product/controller/ProductController.kt @@ -6,6 +6,8 @@ import com.psr.psr.product.dto.response.GetProductsByUserRes import com.psr.psr.product.dto.response.GetProductsRes import com.psr.psr.product.dto.response.MyProduct import com.psr.psr.product.service.ProductService +import org.springframework.data.domain.Pageable +import org.springframework.data.web.PageableDefault import org.springframework.security.core.annotation.AuthenticationPrincipal import org.springframework.web.bind.annotation.* import java.util.* @@ -20,8 +22,10 @@ class ProductController( * 상품 메인 화면 */ @GetMapping() - fun getProducts(@AuthenticationPrincipal userAccount: UserAccount, @RequestParam(required = false) category: String): BaseResponse { - return BaseResponse(productService.getProducts(userAccount.getUser(), category)); + fun getProducts(@AuthenticationPrincipal userAccount: UserAccount, + @RequestParam(required = false) category: String, + @PageableDefault(size = 8, page = 0) pageable: Pageable): BaseResponse { + return BaseResponse(productService.getProducts(userAccount.getUser(), category, pageable)); } /** diff --git a/src/main/kotlin/com/psr/psr/product/dto/response/GetProductsRes.kt b/src/main/kotlin/com/psr/psr/product/dto/response/GetProductsRes.kt index d2bb68e..f7cdb15 100644 --- a/src/main/kotlin/com/psr/psr/product/dto/response/GetProductsRes.kt +++ b/src/main/kotlin/com/psr/psr/product/dto/response/GetProductsRes.kt @@ -1,6 +1,8 @@ package com.psr.psr.product.dto.response +import org.springframework.data.domain.Page + data class GetProductsRes( val popularList: List, - val productList: List + val productList: Page ) diff --git a/src/main/kotlin/com/psr/psr/product/repository/product/ProductCustom.kt b/src/main/kotlin/com/psr/psr/product/repository/product/ProductCustom.kt index 773407f..9007d07 100644 --- a/src/main/kotlin/com/psr/psr/product/repository/product/ProductCustom.kt +++ b/src/main/kotlin/com/psr/psr/product/repository/product/ProductCustom.kt @@ -4,8 +4,11 @@ import com.psr.psr.product.dto.response.PopularProductDetail import com.psr.psr.product.dto.response.ProductDetail import com.psr.psr.user.entity.Category import com.psr.psr.user.entity.User +import org.springframework.data.domain.Page +import org.springframework.data.domain.Pageable + interface ProductCustom { fun findTop5PopularProducts(user: User, category: List): List - fun findAllCategoryProducts(user: User, category: List): List + fun findAllCategoryProducts(pageable: Pageable, user: User, category: List): Page } \ No newline at end of file diff --git a/src/main/kotlin/com/psr/psr/product/repository/product/ProductRepositoryImpl.kt b/src/main/kotlin/com/psr/psr/product/repository/product/ProductRepositoryImpl.kt index adc3e41..4abbe50 100644 --- a/src/main/kotlin/com/psr/psr/product/repository/product/ProductRepositoryImpl.kt +++ b/src/main/kotlin/com/psr/psr/product/repository/product/ProductRepositoryImpl.kt @@ -5,17 +5,22 @@ import com.psr.psr.product.dto.response.PopularProductDetail import com.psr.psr.product.dto.response.ProductDetail import com.psr.psr.product.dto.response.QPopularProductDetail import com.psr.psr.product.dto.response.QProductDetail -import com.querydsl.jpa.impl.JPAQueryFactory -import org.springframework.stereotype.Component import com.psr.psr.product.entity.product.QProduct.product -import com.psr.psr.product.entity.product.QProductLike.productLike import com.psr.psr.product.entity.product.QProductImg.productImg +import com.psr.psr.product.entity.product.QProductLike.productLike import com.psr.psr.product.entity.review.QReview.review import com.psr.psr.user.entity.Category import com.psr.psr.user.entity.User import com.querydsl.core.types.ExpressionUtils import com.querydsl.core.types.dsl.Expressions import com.querydsl.jpa.JPAExpressions +import com.querydsl.jpa.impl.JPAQueryFactory +import org.springframework.data.domain.Page +import org.springframework.data.domain.PageImpl +import org.springframework.data.domain.Pageable +import org.springframework.stereotype.Component + + @Component class ProductRepositoryImpl( @@ -44,23 +49,39 @@ class ProductRepositoryImpl( .fetch() } - override fun findAllCategoryProducts(target: User, category: List): List { + override fun findAllCategoryProducts(pageable: Pageable, target: User, category: List): Page { - return queryFactory - .select(QProductDetail( - product.id, - JPAExpressions.select(productImg.imgKey).from(productImg).where(productImg.id.eq(JPAExpressions.select(productImg.id.min()).from(productImg).where(productImg.product.eq(product)))), - product.user.id, - product.user.nickname, - product.name, - product.price, - Expressions.asBoolean(JPAExpressions.selectFrom(productLike).where(productLike.user.eq(target).and(productLike.product.eq(product)).and(productLike.status.eq("active"))).exists()) - )) + val result = queryFactory + .select( + QProductDetail( + product.id, + JPAExpressions.select(productImg.imgKey).from(productImg).where( + productImg.id.eq( + JPAExpressions.select(productImg.id.min()).from(productImg) + .where(productImg.product.eq(product)) + ) + ), + product.user.id, + product.user.nickname, + product.name, + product.price, + Expressions.asBoolean( + JPAExpressions.selectFrom(productLike).where( + productLike.user.eq(target).and(productLike.product.eq(product)) + .and(productLike.status.eq("active")) + ).exists() + ) + ) + ) .from(product) .where(product.category.`in`(category)) + .offset(pageable.offset) + .limit(pageable.pageSize.toLong()) .groupBy(product.id) .orderBy(product.createdAt.desc()) .fetch() + return PageImpl(result, pageable, result.size.toLong()) + } } \ No newline at end of file diff --git a/src/main/kotlin/com/psr/psr/product/service/ProductService.kt b/src/main/kotlin/com/psr/psr/product/service/ProductService.kt index 42d4cde..06f0e46 100644 --- a/src/main/kotlin/com/psr/psr/product/service/ProductService.kt +++ b/src/main/kotlin/com/psr/psr/product/service/ProductService.kt @@ -14,6 +14,7 @@ import com.psr.psr.user.entity.Category import com.psr.psr.user.entity.User import com.psr.psr.user.repository.UserInterestRepository import com.psr.psr.user.repository.UserRepository +import org.springframework.data.domain.Pageable import org.springframework.stereotype.Service @Service @@ -24,7 +25,7 @@ class ProductService( private val userRepository: UserRepository, private val productAssembler: ProductAssembler ) { - fun getProducts(user: User, category: String): GetProductsRes { + fun getProducts(user: User, category: String, pageable: Pageable): GetProductsRes { var interestCategoryList: MutableList = ArrayList() if(category.isEmpty()) { // 유저의 관심목록 @@ -34,7 +35,7 @@ class ProductService( interestCategoryList.add(Category.getCategoryByName(category)) } - val productList = productRepository.findAllCategoryProducts(user, interestCategoryList) + val productList = productRepository.findAllCategoryProducts(pageable, user, interestCategoryList) val popularList = productRepository.findTop5PopularProducts(user, interestCategoryList) return GetProductsRes(popularList, productList) From b18a23158ae9936ba0f5191b5937516a9e477a77 Mon Sep 17 00:00:00 2001 From: sojungpp Date: Tue, 8 Aug 2023 00:28:09 +0900 Subject: [PATCH 2/3] =?UTF-8?q?#78=20fix:=20active=20constant=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../psr/product/repository/product/ProductRepositoryImpl.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/psr/psr/product/repository/product/ProductRepositoryImpl.kt b/src/main/kotlin/com/psr/psr/product/repository/product/ProductRepositoryImpl.kt index 4abbe50..8f3d10a 100644 --- a/src/main/kotlin/com/psr/psr/product/repository/product/ProductRepositoryImpl.kt +++ b/src/main/kotlin/com/psr/psr/product/repository/product/ProductRepositoryImpl.kt @@ -1,6 +1,7 @@ package com.psr.psr.product.repository.product +import com.psr.psr.global.Constant.UserStatus.UserStatus.ACTIVE_STATUS import com.psr.psr.product.dto.response.PopularProductDetail import com.psr.psr.product.dto.response.ProductDetail import com.psr.psr.product.dto.response.QPopularProductDetail @@ -11,6 +12,7 @@ import com.psr.psr.product.entity.product.QProductLike.productLike import com.psr.psr.product.entity.review.QReview.review import com.psr.psr.user.entity.Category import com.psr.psr.user.entity.User + import com.querydsl.core.types.ExpressionUtils import com.querydsl.core.types.dsl.Expressions import com.querydsl.jpa.JPAExpressions @@ -68,7 +70,7 @@ class ProductRepositoryImpl( Expressions.asBoolean( JPAExpressions.selectFrom(productLike).where( productLike.user.eq(target).and(productLike.product.eq(product)) - .and(productLike.status.eq("active")) + .and(productLike.status.eq(ACTIVE_STATUS)) ).exists() ) ) From 7ff253b421db24ae617bb719104af16ec127aa2e Mon Sep 17 00:00:00 2001 From: sojungpp Date: Tue, 8 Aug 2023 19:13:26 +0900 Subject: [PATCH 3/3] =?UTF-8?q?#78=20fix:=20default=EA=B0=92=20=EC=A4=91?= =?UTF-8?q?=EB=B3=B5=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/psr/psr/product/controller/ProductController.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/psr/psr/product/controller/ProductController.kt b/src/main/kotlin/com/psr/psr/product/controller/ProductController.kt index 92a6cb6..5266ed4 100644 --- a/src/main/kotlin/com/psr/psr/product/controller/ProductController.kt +++ b/src/main/kotlin/com/psr/psr/product/controller/ProductController.kt @@ -24,7 +24,7 @@ class ProductController( @GetMapping() fun getProducts(@AuthenticationPrincipal userAccount: UserAccount, @RequestParam(required = false) category: String, - @PageableDefault(size = 8, page = 0) pageable: Pageable): BaseResponse { + @PageableDefault(size = 8) pageable: Pageable): BaseResponse { return BaseResponse(productService.getProducts(userAccount.getUser(), category, pageable)); }