Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] 상품 메인 조회 페이징 추가 #80

Merged
merged 3 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -20,8 +22,10 @@ class ProductController(
* 상품 메인 화면
*/
@GetMapping()
fun getProducts(@AuthenticationPrincipal userAccount: UserAccount, @RequestParam(required = false) category: String): BaseResponse<GetProductsRes> {
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<GetProductsRes> {
sojungpp marked this conversation as resolved.
Show resolved Hide resolved
return BaseResponse(productService.getProducts(userAccount.getUser(), category, pageable));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.psr.psr.product.dto.response

import org.springframework.data.domain.Page

data class GetProductsRes(
val popularList: List<PopularProductDetail>,
val productList: List<ProductDetail>
val productList: Page<ProductDetail>
)
Original file line number Diff line number Diff line change
Expand Up @@ -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<Category>): List<PopularProductDetail>
fun findAllCategoryProducts(user: User, category: List<Category>): List<ProductDetail>
fun findAllCategoryProducts(pageable: Pageable, user: User, category: List<Category>): Page<ProductDetail>
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
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
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(
Expand Down Expand Up @@ -44,23 +51,39 @@ class ProductRepositoryImpl(
.fetch()
}

override fun findAllCategoryProducts(target: User, category: List<Category>): List<ProductDetail> {
override fun findAllCategoryProducts(pageable: Pageable, target: User, category: List<Category>): Page<ProductDetail> {

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_STATUS))
).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())

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Category> = ArrayList()
if(category.isEmpty()) {
// 유저의 관심목록
Expand All @@ -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)
Expand Down
Loading