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

[feat] 유저 상품 목록 API 생성 #46

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -2,14 +2,12 @@ package com.psr.psr.product.controller

import com.psr.psr.global.dto.BaseResponse
import com.psr.psr.global.jwt.UserAccount
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.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*
import java.util.*

@RestController
Expand All @@ -34,5 +32,13 @@ class ProductController(
return BaseResponse(productService.getMyProducts(userAccount.getUser()));
}

/**
* 유저 상품 목록
*/
@GetMapping("/users/{userId}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용자 객체 필요한게 아니면, AuthenticationPrincipal 사용 안해도 될거에요 !

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정말요?! IsLogin 처럼 필수인줄 알았네여 나도 지워야겠다

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스크린샷 2023-08-04 오전 1 14 46
유레카 !!!!! 반영했습니다 꾸벅

fun getProductsByUser(@AuthenticationPrincipal userAccount: UserAccount, @PathVariable userId: Long): BaseResponse<GetProductsByUserRes> {
return BaseResponse(productService.getProductsByUser(userId))
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.psr.psr.product.dto.assembler

import com.psr.psr.product.dto.response.GetProductsByUserRes
import com.psr.psr.product.dto.response.MyProduct
import com.psr.psr.product.entity.product.Product
import com.psr.psr.user.entity.User
import org.springframework.stereotype.Component

@Component
Expand All @@ -14,7 +16,16 @@ class ProductAssembler {
name = product.name,
price = product.price
)
}

fun toGetProductsByUserResDto(user: User, productList: List<MyProduct>?): GetProductsByUserRes {
return GetProductsByUserRes(
imgKey = user.imgKey,
type = user.type.value,
nickname = user.nickname,
productList = productList
)
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.psr.psr.product.dto.response

data class GetProductsByUserRes(
val imgKey: String?,
val type: String,
val nickname: String,
val productList: List<MyProduct>?
)
16 changes: 16 additions & 0 deletions src/main/kotlin/com/psr/psr/product/service/ProductService.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.psr.psr.product.service

import com.psr.psr.global.Constant.USER_STATUS.USER_STATUS.ACTIVE_STATUS
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.response.GetProductsByUserRes
import com.psr.psr.product.dto.response.GetProductsRes
import com.psr.psr.product.dto.response.MyProduct
import com.psr.psr.product.entity.product.Product
Expand All @@ -10,13 +13,15 @@ import com.psr.psr.product.repository.product.ProductRepository
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.stereotype.Service

@Service
class ProductService(
private val productRepository: ProductRepository,
private val userInterestRepository: UserInterestRepository,
private val productImgRepository: ProductImgRepository,
private val userRepository: UserRepository,
private val productAssembler: ProductAssembler
) {
fun getProducts(user: User, category: String): GetProductsRes {
Expand Down Expand Up @@ -44,4 +49,15 @@ class ProductService(
}
}

fun getProductsByUser(userId: Long): GetProductsByUserRes {
val user: User = userRepository.findByIdAndStatus(userId, ACTIVE_STATUS) ?: throw BaseException(BaseResponseCode.NOT_FOUND_USER)
val products: List<Product>? = productRepository.findAllByUserAndStatusOrderByCreatedAtDesc(user, ACTIVE_STATUS)

val productList = products?.map { p: Product ->
val productImg = productImgRepository.findTop1ByProductEqualsAndStatusEqualsOrderByCreatedAtDesc(p, ACTIVE_STATUS)
productAssembler.toMyProductDto(p, productImg.imgKey)
}
return productAssembler.toGetProductsByUserResDto(user, productList)
}

}
Loading