From abe9b4629e177eabecc7efcf99d4615301e48206 Mon Sep 17 00:00:00 2001 From: sojungpp Date: Sat, 9 Sep 2023 18:03:15 +0900 Subject: [PATCH 1/4] =?UTF-8?q?#168=20refactor:=20ProductAssembler=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20->=20DTO,=20Entity=20=EC=9D=B4=EC=A0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/dto/assembler/ProductAssembler.kt | 94 ------------------- .../product/dto/response/GetHomePageRes.kt | 14 ++- .../dto/response/GetLikeProductsRes.kt | 11 ++- .../product/dto/response/GetMyProductsRes.kt | 14 ++- .../dto/response/GetProductDetailRes.kt | 22 ++++- .../dto/response/GetProductsByUserRes.kt | 15 ++- .../product/dto/response/GetSearchProducts.kt | 10 +- .../psr/product/dto/response/MainProduct.kt | 14 ++- .../product/dto/response/MainTopProduct.kt | 16 +++- .../psr/psr/product/dto/response/MyProduct.kt | 16 +++- .../com/psr/psr/product/entity/Product.kt | 12 +++ .../com/psr/psr/product/entity/ProductImg.kt | 11 ++- .../com/psr/psr/product/entity/ProductLike.kt | 11 ++- .../psr/psr/product/entity/ProductReport.kt | 12 ++- .../psr/psr/product/service/ProductService.kt | 25 ++--- 15 files changed, 180 insertions(+), 117 deletions(-) diff --git a/src/main/kotlin/com/psr/psr/product/dto/assembler/ProductAssembler.kt b/src/main/kotlin/com/psr/psr/product/dto/assembler/ProductAssembler.kt index 9f9fa4d..d7b05b8 100644 --- a/src/main/kotlin/com/psr/psr/product/dto/assembler/ProductAssembler.kt +++ b/src/main/kotlin/com/psr/psr/product/dto/assembler/ProductAssembler.kt @@ -15,94 +15,6 @@ import org.springframework.stereotype.Component @Component class ProductAssembler { - fun toGetMyProductsDto(productList: Page?): GetMyProductsRes { - return GetMyProductsRes( - productList = productList?.map { this.toMyProductDto(it) } - ) - } - fun toMyProductDto(product: Product, imgUrl: String): MyProduct { - return MyProduct( - productId = product.id!!, - imgUrl = imgUrl, - category = product.category.value, - name = product.name, - price = product.price - ) - } - - fun toMyProductDto(product: Product): MyProduct { - return MyProduct( - productId = product.id!!, - imgUrl = product.imgs.firstOrNull()?.imgUrl, - category = product.category.value, - name = product.name, - price = product.price - ) - } - - fun toGetProductsByUserResDto(user: User, productList: Page?): GetProductsByUserRes { - return GetProductsByUserRes( - imgUrl = user.imgUrl, - type = user.type.value, - nickname = user.nickname, - productList = productList?.map { p -> toMyProductDto(p) } - ) - } - - fun toGetProductDetailResDto(isOwner: Boolean, product: Product, imgList: List, numOfLikes: Int, isLike: Boolean): GetProductDetailRes { - return GetProductDetailRes( - isOwner = isOwner, - category = product.category.value, - imgList = imgList.map { i -> i.imgUrl }.toList(), - userId = product.user.id, - nickname = product.user.nickname, - numOfLikes = numOfLikes, - name = product.name, - price = product.price, - description = product.description, - isLike = isLike - ) - - } - - fun toGetLikeProductsResDto(productList: Page?): GetLikeProductsRes { - return GetLikeProductsRes( - productList = productList?.map { pl -> this.toMyProductDto(pl) } - ) - } - - fun toReportEntity(product: Product, user: User, category: ReportCategory): ProductReport { - return ProductReport( - product = product, - user = user, - category = category - ) - } - - fun toGetHomePageResDto(mainTopProductList: List?, productList: List?): GetHomePageRes { - return GetHomePageRes( - mainTopProductList = mainTopProductList?.sortedByDescending { it.createdAt }!!.take(3).map { p -> this.toMainTopProductDto(p) }.toList(), - recentProductList = productList?.sortedByDescending { it.createdAt }!!.take(5).map { p -> this.toMainProductDto(p) }.toList(), - popularProductList = productList?.sortedByDescending { it.likeNum?.size }!!.take(5).map { p -> this.toMainProductDto(p) }.toList() - ) - } - - fun toMainTopProductDto(product: Product): MainTopProduct { - return MainTopProduct( - id = product.id!!, - category = product.category.value, - name = product.name, - description = product.description - ) - } - - fun toMainProductDto(product: Product): MainProduct { - return MainProduct( - id = product.id!!, - imgUrl = product.imgs.firstOrNull()?.imgUrl, - name = product.name - ) - } fun toProductEntity(user: User, request: CreateproductReq): Product { return Product( @@ -114,12 +26,6 @@ class ProductAssembler { ) } - fun toProductImgEntity(product: Product, imgUrl: String): ProductImg { - return ProductImg( - product = product, - imgUrl = imgUrl - ) - } fun toProductLikeEntity(product: Product, user: User): ProductLike { return ProductLike( diff --git a/src/main/kotlin/com/psr/psr/product/dto/response/GetHomePageRes.kt b/src/main/kotlin/com/psr/psr/product/dto/response/GetHomePageRes.kt index aa1315b..e64aa17 100644 --- a/src/main/kotlin/com/psr/psr/product/dto/response/GetHomePageRes.kt +++ b/src/main/kotlin/com/psr/psr/product/dto/response/GetHomePageRes.kt @@ -1,7 +1,19 @@ package com.psr.psr.product.dto.response +import com.psr.psr.product.entity.Product + data class GetHomePageRes( val mainTopProductList: List?, val recentProductList: List?, val popularProductList: List? -) +) { + companion object { + fun toDto(mainTopProductList: List?, productList: List?): GetHomePageRes { + return GetHomePageRes( + mainTopProductList = mainTopProductList?.sortedByDescending { it.createdAt }!!.take(3).map { p -> MainTopProduct.toDto(p) }.toList(), + recentProductList = productList?.sortedByDescending { it.createdAt }!!.take(5).map { p -> MainProduct.toDto(p) }.toList(), + popularProductList = productList?.sortedByDescending { it.likeNum?.size }!!.take(5).map { p -> MainProduct.toDto(p) }.toList() + ) + } + } +} diff --git a/src/main/kotlin/com/psr/psr/product/dto/response/GetLikeProductsRes.kt b/src/main/kotlin/com/psr/psr/product/dto/response/GetLikeProductsRes.kt index ab96570..0481387 100644 --- a/src/main/kotlin/com/psr/psr/product/dto/response/GetLikeProductsRes.kt +++ b/src/main/kotlin/com/psr/psr/product/dto/response/GetLikeProductsRes.kt @@ -1,7 +1,16 @@ package com.psr.psr.product.dto.response +import com.psr.psr.product.entity.Product import org.springframework.data.domain.Page data class GetLikeProductsRes( val productList: Page? -) +) { + companion object { + fun toDto(productList: Page?): GetLikeProductsRes { + return GetLikeProductsRes( + productList = productList?.map { pl -> MyProduct.toDto(pl) } + ) + } + } +} diff --git a/src/main/kotlin/com/psr/psr/product/dto/response/GetMyProductsRes.kt b/src/main/kotlin/com/psr/psr/product/dto/response/GetMyProductsRes.kt index 1c348d1..ba06878 100644 --- a/src/main/kotlin/com/psr/psr/product/dto/response/GetMyProductsRes.kt +++ b/src/main/kotlin/com/psr/psr/product/dto/response/GetMyProductsRes.kt @@ -1,7 +1,19 @@ package com.psr.psr.product.dto.response +import com.psr.psr.global.Constant +import com.psr.psr.product.entity.Product +import com.psr.psr.user.entity.User import org.springframework.data.domain.Page +import org.springframework.data.domain.Pageable data class GetMyProductsRes( val productList: Page? -) +) { + companion object { + fun toDto(productList: Page?): GetMyProductsRes { + return GetMyProductsRes( + productList = productList?.map { MyProduct.toDto(it) } + ) + } + } +} diff --git a/src/main/kotlin/com/psr/psr/product/dto/response/GetProductDetailRes.kt b/src/main/kotlin/com/psr/psr/product/dto/response/GetProductDetailRes.kt index 386f36b..e4746b2 100644 --- a/src/main/kotlin/com/psr/psr/product/dto/response/GetProductDetailRes.kt +++ b/src/main/kotlin/com/psr/psr/product/dto/response/GetProductDetailRes.kt @@ -1,5 +1,8 @@ package com.psr.psr.product.dto.response +import com.psr.psr.product.entity.Product +import com.psr.psr.product.entity.ProductImg + data class GetProductDetailRes( val isOwner: Boolean, val category: String, @@ -11,4 +14,21 @@ data class GetProductDetailRes( val price: Int, val description: String, val isLike: Boolean -) +) { + companion object { + fun toDto(isOwner: Boolean, product: Product, imgList: List, numOfLikes: Int, isLike: Boolean): GetProductDetailRes { + return GetProductDetailRes( + isOwner = isOwner, + category = product.category.value, + imgList = imgList.map { i -> i.imgUrl }.toList(), + userId = product.user.id, + nickname = product.user.nickname, + numOfLikes = numOfLikes, + name = product.name, + price = product.price, + description = product.description, + isLike = isLike + ) + } + } +} diff --git a/src/main/kotlin/com/psr/psr/product/dto/response/GetProductsByUserRes.kt b/src/main/kotlin/com/psr/psr/product/dto/response/GetProductsByUserRes.kt index a4f5553..681ac5a 100644 --- a/src/main/kotlin/com/psr/psr/product/dto/response/GetProductsByUserRes.kt +++ b/src/main/kotlin/com/psr/psr/product/dto/response/GetProductsByUserRes.kt @@ -1,5 +1,7 @@ package com.psr.psr.product.dto.response +import com.psr.psr.product.entity.Product +import com.psr.psr.user.entity.User import org.springframework.data.domain.Page data class GetProductsByUserRes( @@ -7,4 +9,15 @@ data class GetProductsByUserRes( val type: String, val nickname: String, val productList: Page? -) +) { + companion object { + fun toDto(user: User, productList: Page?): GetProductsByUserRes { + return GetProductsByUserRes( + imgUrl = user.imgUrl, + type = user.type.value, + nickname = user.nickname, + productList = productList?.map { p -> MyProduct.toDto(p) } + ) + } + } +} diff --git a/src/main/kotlin/com/psr/psr/product/dto/response/GetSearchProducts.kt b/src/main/kotlin/com/psr/psr/product/dto/response/GetSearchProducts.kt index 17e9bbd..0a37a28 100644 --- a/src/main/kotlin/com/psr/psr/product/dto/response/GetSearchProducts.kt +++ b/src/main/kotlin/com/psr/psr/product/dto/response/GetSearchProducts.kt @@ -4,4 +4,12 @@ import org.springframework.data.domain.Page data class GetSearchProducts( val productList: Page -) +) { + companion object { + fun toDto(productList: Page): GetSearchProducts { + return GetSearchProducts( + productList = productList + ) + } + } +} diff --git a/src/main/kotlin/com/psr/psr/product/dto/response/MainProduct.kt b/src/main/kotlin/com/psr/psr/product/dto/response/MainProduct.kt index 123fc96..a186e2b 100644 --- a/src/main/kotlin/com/psr/psr/product/dto/response/MainProduct.kt +++ b/src/main/kotlin/com/psr/psr/product/dto/response/MainProduct.kt @@ -1,7 +1,19 @@ package com.psr.psr.product.dto.response +import com.psr.psr.product.entity.Product + data class MainProduct( val id: Long, val imgUrl: String?, val name: String -) +) { + companion object { + fun toDto(product: Product): MainProduct { + return MainProduct( + id = product.id!!, + imgUrl = product.imgs.firstOrNull()?.imgUrl, + name = product.name + ) + } + } +} diff --git a/src/main/kotlin/com/psr/psr/product/dto/response/MainTopProduct.kt b/src/main/kotlin/com/psr/psr/product/dto/response/MainTopProduct.kt index ff2cd39..c8accb3 100644 --- a/src/main/kotlin/com/psr/psr/product/dto/response/MainTopProduct.kt +++ b/src/main/kotlin/com/psr/psr/product/dto/response/MainTopProduct.kt @@ -1,8 +1,22 @@ package com.psr.psr.product.dto.response +import com.psr.psr.product.entity.Product + data class MainTopProduct( val id: Long, val category: String, val name: String, val description: String -) +) { + companion object { + fun toDto(product: Product): MainTopProduct { + return MainTopProduct( + id = product.id!!, + category = product.category.value, + name = product.name, + description = product.description + ) + } + + } +} diff --git a/src/main/kotlin/com/psr/psr/product/dto/response/MyProduct.kt b/src/main/kotlin/com/psr/psr/product/dto/response/MyProduct.kt index cf73540..599a04f 100644 --- a/src/main/kotlin/com/psr/psr/product/dto/response/MyProduct.kt +++ b/src/main/kotlin/com/psr/psr/product/dto/response/MyProduct.kt @@ -1,9 +1,23 @@ package com.psr.psr.product.dto.response +import com.psr.psr.product.entity.Product + data class MyProduct( val productId: Long, val imgUrl: String?, val category: String, val name: String, val price: Int -) +) { + companion object { + fun toDto(product: Product): MyProduct { + return MyProduct( + productId = product.id!!, + imgUrl = product.imgs.firstOrNull()?.imgUrl, + category = product.category.value, + name = product.name, + price = product.price + ) + } + } +} diff --git a/src/main/kotlin/com/psr/psr/product/entity/Product.kt b/src/main/kotlin/com/psr/psr/product/entity/Product.kt index 4b4adba..3c2e1a7 100644 --- a/src/main/kotlin/com/psr/psr/product/entity/Product.kt +++ b/src/main/kotlin/com/psr/psr/product/entity/Product.kt @@ -56,4 +56,16 @@ data class Product( this.description = request.description return this } + + companion object { + fun toEntity(user: User, request: CreateproductReq): Product { + return Product( + user = user, + name = request.name, + category = Category.getCategoryByValue(request.category), + price = request.price, + description = request.description + ) + } + } } diff --git a/src/main/kotlin/com/psr/psr/product/entity/ProductImg.kt b/src/main/kotlin/com/psr/psr/product/entity/ProductImg.kt index 9da42a1..d28c87b 100644 --- a/src/main/kotlin/com/psr/psr/product/entity/ProductImg.kt +++ b/src/main/kotlin/com/psr/psr/product/entity/ProductImg.kt @@ -20,4 +20,13 @@ data class ProductImg( @NotNull var imgUrl: String -) : BaseEntity() +) : BaseEntity() { + companion object { + fun toEntity(product: Product, imgUrl: String): ProductImg { + return ProductImg( + product = product, + imgUrl = imgUrl + ) + } + } +} diff --git a/src/main/kotlin/com/psr/psr/product/entity/ProductLike.kt b/src/main/kotlin/com/psr/psr/product/entity/ProductLike.kt index 4e3221e..ab177b5 100644 --- a/src/main/kotlin/com/psr/psr/product/entity/ProductLike.kt +++ b/src/main/kotlin/com/psr/psr/product/entity/ProductLike.kt @@ -19,4 +19,13 @@ data class ProductLike( @JoinColumn(nullable = false, name = "user_id") var user: User -): BaseEntity() +): BaseEntity() { + companion object { + fun toEntity(product: Product, user: User): ProductLike { + return ProductLike( + product = product, + user = user + ) + } + } +} diff --git a/src/main/kotlin/com/psr/psr/product/entity/ProductReport.kt b/src/main/kotlin/com/psr/psr/product/entity/ProductReport.kt index 435aea9..15dbbd8 100644 --- a/src/main/kotlin/com/psr/psr/product/entity/ProductReport.kt +++ b/src/main/kotlin/com/psr/psr/product/entity/ProductReport.kt @@ -23,4 +23,14 @@ data class ProductReport( @Enumerated(EnumType.STRING) var category: ReportCategory -): BaseEntity() +): BaseEntity() { + companion object { + fun toEntity(product: Product, user: User, category: ReportCategory): ProductReport { + return ProductReport( + product = product, + user = user, + category = category + ) + } + } +} 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 e91d657..8d5e109 100644 --- a/src/main/kotlin/com/psr/psr/product/service/ProductService.kt +++ b/src/main/kotlin/com/psr/psr/product/service/ProductService.kt @@ -10,6 +10,9 @@ import com.psr.psr.product.dto.assembler.ProductAssembler import com.psr.psr.product.dto.request.CreateproductReq import com.psr.psr.product.dto.response.* import com.psr.psr.product.entity.Product +import com.psr.psr.product.entity.ProductImg +import com.psr.psr.product.entity.ProductLike +import com.psr.psr.product.entity.ProductReport import com.psr.psr.product.repository.ProductImgRepository import com.psr.psr.product.repository.ProductLikeRepository import com.psr.psr.product.repository.ProductReportRepository @@ -52,14 +55,14 @@ class ProductService( fun getMyProducts(user: User, pageable: Pageable): GetMyProductsRes { val myProductList: Page? = productRepository.findAllByUserAndStatusOrderByCreatedAtDesc(user, ACTIVE_STATUS, pageable) - return productAssembler.toGetMyProductsDto(myProductList) + return GetMyProductsRes.toDto(myProductList) } fun getProductsByUser(userId: Long, pageable: Pageable): GetProductsByUserRes { val user: User = userRepository.findByIdAndStatus(userId, ACTIVE_STATUS) ?: throw BaseException(BaseResponseCode.NOT_FOUND_USER) val products: Page? = productRepository.findAllByUserAndStatusOrderByCreatedAtDesc(user, ACTIVE_STATUS, pageable) - return productAssembler.toGetProductsByUserResDto(user, products) + return GetProductsByUserRes.toDto(user, products) } fun getProductDetail(user: User, productId: Long): GetProductDetailRes { @@ -69,7 +72,7 @@ class ProductService( val numOfLikes = productLikeRepository.countByProductAndStatus(product, ACTIVE_STATUS) val isLike = productLikeRepository.existsByProductAndUserAndStatus(product, user, ACTIVE_STATUS) - return productAssembler.toGetProductDetailResDto(isOwner, product, imgList, numOfLikes, isLike) + return GetProductDetailRes.toDto(isOwner, product, imgList, numOfLikes, isLike) } fun reportProduct(user: User, productId: Long, category: ReportCategory) { @@ -78,27 +81,27 @@ class ProductService( if (productReportRepository.findByProductAndUserAndStatus(product, user, ACTIVE_STATUS) != null) throw BaseException(BaseResponseCode.REPORT_ALREADY_COMPLETE) - productReportRepository.save(productAssembler.toReportEntity(product, user, category)) + productReportRepository.save(ProductReport.toEntity(product, user, category)) } fun getLikeProducts(user: User, pageable: Pageable): GetLikeProductsRes { val productList: Page? = productLikeRepository.findByUserAndStatus(user, ACTIVE_STATUS, pageable)?.map { it.product } - return productAssembler.toGetLikeProductsResDto(productList) + return GetLikeProductsRes.toDto(productList) } fun getHomePage(): GetHomePageRes { // MANAGER 상품 val mainTopProductList = userRepository.findByTypeAndStatus(Type.MANAGER, ACTIVE_STATUS)!!.products val productList = productRepository.findAllByStatus(ACTIVE_STATUS) - return productAssembler.toGetHomePageResDto(mainTopProductList, productList) + return GetHomePageRes.toDto(mainTopProductList, productList) } @Transactional fun createProduct(user: User, request: CreateproductReq) { - val product = productRepository.save(productAssembler.toProductEntity(user, request)) + val product = productRepository.save(Product.toEntity(user, request)) // 이미지 있는 경우만 저장 if(request.imgList != null) - request.imgList.map { imgUrl: String -> productImgRepository.save(productAssembler.toProductImgEntity(product, imgUrl)) } + request.imgList.map { imgUrl: String -> productImgRepository.save(ProductImg.toEntity(product, imgUrl)) } } fun likeProduct(user: User, productId: Long) { @@ -108,7 +111,7 @@ class ProductService( // 없는 경우 새로 생성 if (productLike == null) { - val newProductLike = productAssembler.toProductLikeEntity(product, user) + val newProductLike = ProductLike.toEntity(product, user) productLikeRepository.save(newProductLike) } else { val status = productLike.status @@ -132,7 +135,7 @@ class ProductService( } else { productRepository.searchProductsByCreatedAt(user, keyword, pageable) } - return productAssembler.toGetSearchProductsDto(productList) + return GetSearchProducts.toDto(productList) } @@ -146,7 +149,7 @@ class ProductService( productImgRepository.deleteByProduct(product) // 이미지가 있는 경우 저장 if (request.imgList != null) { - val imgList = request.imgList.map { productAssembler.toProductImgEntity(product, it) } + val imgList = request.imgList.map { ProductImg.toEntity(product, it) } productImgRepository.saveAll(imgList) } } From 319f402d513db414ee826dc2f93777ba18da332d Mon Sep 17 00:00:00 2001 From: sojungpp Date: Sat, 9 Sep 2023 18:04:12 +0900 Subject: [PATCH 2/4] =?UTF-8?q?#168=20fix:=20ProductAssembler=20=EC=9D=98?= =?UTF-8?q?=EC=A1=B4=EC=84=B1=20=EB=B0=8F=20=ED=8C=8C=EC=9D=BC=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/dto/assembler/ProductAssembler.kt | 44 ------------------- .../psr/psr/product/service/ProductService.kt | 2 - 2 files changed, 46 deletions(-) delete mode 100644 src/main/kotlin/com/psr/psr/product/dto/assembler/ProductAssembler.kt diff --git a/src/main/kotlin/com/psr/psr/product/dto/assembler/ProductAssembler.kt b/src/main/kotlin/com/psr/psr/product/dto/assembler/ProductAssembler.kt deleted file mode 100644 index d7b05b8..0000000 --- a/src/main/kotlin/com/psr/psr/product/dto/assembler/ProductAssembler.kt +++ /dev/null @@ -1,44 +0,0 @@ -package com.psr.psr.product.dto.assembler - -import com.psr.psr.global.entity.ReportCategory -import com.psr.psr.product.dto.request.CreateproductReq -import com.psr.psr.product.dto.response.* -import com.psr.psr.product.entity.Product -import com.psr.psr.product.entity.ProductImg -import com.psr.psr.product.entity.ProductLike -import com.psr.psr.product.entity.ProductReport -import com.psr.psr.user.entity.Category -import com.psr.psr.user.entity.User -import org.springframework.data.domain.Page -import org.springframework.stereotype.Component - -@Component -class ProductAssembler { - - - fun toProductEntity(user: User, request: CreateproductReq): Product { - return Product( - user = user, - name = request.name, - category = Category.getCategoryByValue(request.category), - price = request.price, - description = request.description - ) - } - - - fun toProductLikeEntity(product: Product, user: User): ProductLike { - return ProductLike( - product = product, - user = user - ) - } - - fun toGetSearchProductsDto(productList: Page): GetSearchProducts { - return GetSearchProducts( - productList = productList - ) - } - - -} \ 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 8d5e109..785c83f 100644 --- a/src/main/kotlin/com/psr/psr/product/service/ProductService.kt +++ b/src/main/kotlin/com/psr/psr/product/service/ProductService.kt @@ -6,7 +6,6 @@ import com.psr.psr.global.Constant.UserStatus.UserStatus.INACTIVE_STATUS import com.psr.psr.global.entity.ReportCategory import com.psr.psr.global.exception.BaseException import com.psr.psr.global.exception.BaseResponseCode -import com.psr.psr.product.dto.assembler.ProductAssembler import com.psr.psr.product.dto.request.CreateproductReq import com.psr.psr.product.dto.response.* import com.psr.psr.product.entity.Product @@ -35,7 +34,6 @@ class ProductService( private val productLikeRepository: ProductLikeRepository, private val productReportRepository: ProductReportRepository, private val userRepository: UserRepository, - private val productAssembler: ProductAssembler ) { fun getProducts(user: User, category: String, pageable: Pageable): GetProductsRes { var interestCategoryList: MutableList = ArrayList() From 8b01982e118b349837963b3e7b6816a7a52b8237 Mon Sep 17 00:00:00 2001 From: sojungpp Date: Sat, 9 Sep 2023 18:06:30 +0900 Subject: [PATCH 3/4] =?UTF-8?q?#168=20refactor:=20=EB=B6=88=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=9C=20import=EB=AC=B8=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/psr/psr/product/controller/ProductController.kt | 3 --- .../com/psr/psr/product/dto/response/GetMyProductsRes.kt | 3 --- src/main/kotlin/com/psr/psr/product/entity/ProductImg.kt | 7 +------ .../psr/psr/product/repository/ProductRepositoryImpl.kt | 7 +++---- 4 files changed, 4 insertions(+), 16 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 18673ec..9572b15 100644 --- a/src/main/kotlin/com/psr/psr/product/controller/ProductController.kt +++ b/src/main/kotlin/com/psr/psr/product/controller/ProductController.kt @@ -6,9 +6,6 @@ import com.psr.psr.global.entity.ReportCategory import com.psr.psr.global.jwt.UserAccount import com.psr.psr.product.dto.request.CreateproductReq import com.psr.psr.product.dto.request.ReportProductReq -import com.psr.psr.product.dto.response.GetProductDetailRes -import com.psr.psr.product.dto.response.GetProductsByUserRes -import com.psr.psr.product.dto.response.GetProductsRes import com.psr.psr.product.dto.response.* import com.psr.psr.product.service.ProductService import jakarta.validation.Valid diff --git a/src/main/kotlin/com/psr/psr/product/dto/response/GetMyProductsRes.kt b/src/main/kotlin/com/psr/psr/product/dto/response/GetMyProductsRes.kt index ba06878..dc1fc41 100644 --- a/src/main/kotlin/com/psr/psr/product/dto/response/GetMyProductsRes.kt +++ b/src/main/kotlin/com/psr/psr/product/dto/response/GetMyProductsRes.kt @@ -1,10 +1,7 @@ package com.psr.psr.product.dto.response -import com.psr.psr.global.Constant import com.psr.psr.product.entity.Product -import com.psr.psr.user.entity.User import org.springframework.data.domain.Page -import org.springframework.data.domain.Pageable data class GetMyProductsRes( val productList: Page? diff --git a/src/main/kotlin/com/psr/psr/product/entity/ProductImg.kt b/src/main/kotlin/com/psr/psr/product/entity/ProductImg.kt index d28c87b..73fc322 100644 --- a/src/main/kotlin/com/psr/psr/product/entity/ProductImg.kt +++ b/src/main/kotlin/com/psr/psr/product/entity/ProductImg.kt @@ -1,12 +1,7 @@ package com.psr.psr.product.entity import com.psr.psr.global.entity.BaseEntity -import jakarta.persistence.Entity -import jakarta.persistence.GeneratedValue -import jakarta.persistence.GenerationType -import jakarta.persistence.Id -import jakarta.persistence.ManyToOne -import org.hibernate.annotations.SQLDelete +import jakarta.persistence.* import org.jetbrains.annotations.NotNull @Entity diff --git a/src/main/kotlin/com/psr/psr/product/repository/ProductRepositoryImpl.kt b/src/main/kotlin/com/psr/psr/product/repository/ProductRepositoryImpl.kt index 6f2e543..3632b39 100644 --- a/src/main/kotlin/com/psr/psr/product/repository/ProductRepositoryImpl.kt +++ b/src/main/kotlin/com/psr/psr/product/repository/ProductRepositoryImpl.kt @@ -5,21 +5,20 @@ 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.QProduct.product -import com.psr.psr.product.entity.QProductLike.productLike import com.psr.psr.product.entity.QProductImg.productImg +import com.psr.psr.product.entity.QProductLike.productLike import com.psr.psr.review.entity.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( From a6bd873d355277aa866146901d426495ef386cd9 Mon Sep 17 00:00:00 2001 From: sojungpp Date: Sat, 9 Sep 2023 18:12:09 +0900 Subject: [PATCH 4/4] =?UTF-8?q?#168=20refactor:=20productService=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=A3=BC=EC=84=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../psr/psr/product/service/ProductService.kt | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) 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 785c83f..c935ae9 100644 --- a/src/main/kotlin/com/psr/psr/product/service/ProductService.kt +++ b/src/main/kotlin/com/psr/psr/product/service/ProductService.kt @@ -35,27 +35,33 @@ class ProductService( private val productReportRepository: ProductReportRepository, private val userRepository: UserRepository, ) { + + // 상품 메인 조회 fun getProducts(user: User, category: String, pageable: Pageable): GetProductsRes { var interestCategoryList: MutableList = ArrayList() + // 카테고리를 선택하지 않았을 때, 유저의 관심목록에 해당하는 게시글 if(category.isEmpty()) { - // 유저의 관심목록 val userInterestList = userInterestRepository.findByUserAndStatus(user, ACTIVE_STATUS) interestCategoryList = userInterestList.stream().map { ui -> ui.category }.toList() } else { interestCategoryList.add(Category.getCategoryByName(category)) } + // 카테고리 상품들 val productList = productRepository.findAllCategoryProducts(pageable, user, interestCategoryList) + // 인기순(하트순) 상위 5개 상품들 val popularList = productRepository.findTop5PopularProducts(user, interestCategoryList) return GetProductsRes(popularList, productList) } + // 마이 게시글 fun getMyProducts(user: User, pageable: Pageable): GetMyProductsRes { val myProductList: Page? = productRepository.findAllByUserAndStatusOrderByCreatedAtDesc(user, ACTIVE_STATUS, pageable) return GetMyProductsRes.toDto(myProductList) } + // 유저 상품 목록 fun getProductsByUser(userId: Long, pageable: Pageable): GetProductsByUserRes { val user: User = userRepository.findByIdAndStatus(userId, ACTIVE_STATUS) ?: throw BaseException(BaseResponseCode.NOT_FOUND_USER) val products: Page? = productRepository.findAllByUserAndStatusOrderByCreatedAtDesc(user, ACTIVE_STATUS, pageable) @@ -63,6 +69,7 @@ class ProductService( return GetProductsByUserRes.toDto(user, products) } + // 상품 상세 조회 - 상품 fun getProductDetail(user: User, productId: Long): GetProductDetailRes { val product: Product = productRepository.findByIdAndStatus(productId, ACTIVE_STATUS) ?: throw BaseException(BaseResponseCode.NOT_FOUND_PRODUCT) val isOwner = product.user == user @@ -73,27 +80,33 @@ class ProductService( return GetProductDetailRes.toDto(isOwner, product, imgList, numOfLikes, isLike) } + // 상품 신고 fun reportProduct(user: User, productId: Long, category: ReportCategory) { val product: Product = productRepository.findByIdAndStatus(productId, ACTIVE_STATUS) ?: throw BaseException(BaseResponseCode.NOT_FOUND_PRODUCT) + // 신고한 글은 재신고 불가 if (productReportRepository.findByProductAndUserAndStatus(product, user, ACTIVE_STATUS) != null) throw BaseException(BaseResponseCode.REPORT_ALREADY_COMPLETE) productReportRepository.save(ProductReport.toEntity(product, user, category)) } + // 찜 목록 fun getLikeProducts(user: User, pageable: Pageable): GetLikeProductsRes { val productList: Page? = productLikeRepository.findByUserAndStatus(user, ACTIVE_STATUS, pageable)?.map { it.product } return GetLikeProductsRes.toDto(productList) } + // 홈 화면 조회 - 상품 fun getHomePage(): GetHomePageRes { // MANAGER 상품 val mainTopProductList = userRepository.findByTypeAndStatus(Type.MANAGER, ACTIVE_STATUS)!!.products + // 전체 상품 val productList = productRepository.findAllByStatus(ACTIVE_STATUS) return GetHomePageRes.toDto(mainTopProductList, productList) } + // 상품 등록 @Transactional fun createProduct(user: User, request: CreateproductReq) { val product = productRepository.save(Product.toEntity(user, request)) @@ -102,6 +115,7 @@ class ProductService( request.imgList.map { imgUrl: String -> productImgRepository.save(ProductImg.toEntity(product, imgUrl)) } } + // 상품 찜 fun likeProduct(user: User, productId: Long) { val product: Product = productRepository.findByIdAndStatus(productId, ACTIVE_STATUS) ?: throw BaseException(BaseResponseCode.NOT_FOUND_PRODUCT) @@ -120,30 +134,38 @@ class ProductService( } } + // 상품 삭제 fun deleteProduct(user: User, productId: Long) { val product: Product = productRepository.findByIdAndStatus(productId, ACTIVE_STATUS) ?: throw BaseException(BaseResponseCode.NOT_FOUND_PRODUCT) + // 해당 글의 작성자 여부 확인 if(product.user != user) throw BaseException(BaseResponseCode.INVALID_PRODUCT_USER) productRepository.deleteById(productId) } + // 상품 검색 fun searchProducts(user: User, keyword: String, sortType: String, pageable: Pageable): GetSearchProducts { + // 인기순 val productList: Page = if(sortType == POPULAR) { productRepository.searchProductsByLike(user, keyword, pageable) - } else { + } + // 최신순 (default) + else { productRepository.searchProductsByCreatedAt(user, keyword, pageable) } return GetSearchProducts.toDto(productList) } - + // 상품 수정 @Transactional fun modifyProduct(user: User, productId: Long, request: CreateproductReq) { val product: Product = productRepository.findByIdAndStatus(productId, ACTIVE_STATUS) ?: throw BaseException(BaseResponseCode.NOT_FOUND_PRODUCT) + // 해당 글의 작성자 여부 확인 if(product.user != user) throw BaseException(BaseResponseCode.INVALID_PRODUCT_USER) product.modifyProduct(request) + // 이미지 전체 삭제 후 재저장 productImgRepository.deleteByProduct(product) // 이미지가 있는 경우 저장 if (request.imgList != null) {