Skip to content

Commit

Permalink
Merge pull request #169 from PSR-Co/refactor/#168-assembler-to-dto
Browse files Browse the repository at this point in the history
  • Loading branch information
sojungpp authored Sep 9, 2023
2 parents aae0bce + a6bd873 commit 3131e74
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
138 changes: 0 additions & 138 deletions src/main/kotlin/com/psr/psr/product/dto/assembler/ProductAssembler.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package com.psr.psr.product.dto.response

import com.psr.psr.product.entity.Product

data class GetHomePageRes(
val mainTopProductList: List<MainTopProduct>?,
val recentProductList: List<MainProduct>?,
val popularProductList: List<MainProduct>?
)
) {
companion object {
fun toDto(mainTopProductList: List<Product>?, productList: List<Product>?): 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()
)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<MyProduct>?
)
) {
companion object {
fun toDto(productList: Page<Product>?): GetLikeProductsRes {
return GetLikeProductsRes(
productList = productList?.map { pl -> MyProduct.toDto(pl) }
)
}
}
}
Original file line number Diff line number Diff line change
@@ -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 GetMyProductsRes(
val productList: Page<MyProduct>?
)
) {
companion object {
fun toDto(productList: Page<Product>?): GetMyProductsRes {
return GetMyProductsRes(
productList = productList?.map { MyProduct.toDto(it) }
)
}
}
}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<ProductImg>, 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
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
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(
val imgUrl: String?,
val type: String,
val nickname: String,
val productList: Page<MyProduct>?
)
) {
companion object {
fun toDto(user: User, productList: Page<Product>?): GetProductsByUserRes {
return GetProductsByUserRes(
imgUrl = user.imgUrl,
type = user.type.value,
nickname = user.nickname,
productList = productList?.map { p -> MyProduct.toDto(p) }
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ import org.springframework.data.domain.Page

data class GetSearchProducts(
val productList: Page<ProductDetail>
)
) {
companion object {
fun toDto(productList: Page<ProductDetail>): GetSearchProducts {
return GetSearchProducts(
productList = productList
)
}
}
}
14 changes: 13 additions & 1 deletion src/main/kotlin/com/psr/psr/product/dto/response/MainProduct.kt
Original file line number Diff line number Diff line change
@@ -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
)
}
}
}
Original file line number Diff line number Diff line change
@@ -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
)
}

}
}
16 changes: 15 additions & 1 deletion src/main/kotlin/com/psr/psr/product/dto/response/MyProduct.kt
Original file line number Diff line number Diff line change
@@ -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
)
}
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/com/psr/psr/product/entity/Product.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
}
18 changes: 11 additions & 7 deletions src/main/kotlin/com/psr/psr/product/entity/ProductImg.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,4 +15,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
)
}
}
}
Loading

0 comments on commit 3131e74

Please sign in to comment.