Skip to content

Commit

Permalink
refactor: 기존 product 로직 Seller 엔드포인트 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
Ogu1208 committed Aug 17, 2024
1 parent 93dc0a6 commit 099afcc
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,6 @@ import org.store.clothstar.product.service.ProductApplicationService
private class ProductController(
private val productApplicationService: ProductApplicationService,
) {
@PostMapping
@Operation(
summary = "상품 등록",
description = "카테고리 아이디, 상품 이름, 내용, 가격, 상태를 입력하여 상품을 신규 등록한다."
)
fun createProduct(
@RequestPart(value = "mainImage", required = false) mainImage: MultipartFile,
@RequestPart(value = "subImages", required = false) subImages: List<MultipartFile>?,
@RequestPart(value = "dto") @Validated productCreateRequest: ProductCreateRequest
): ResponseEntity<MessageDTO> {
// 상품 등록
productApplicationService.createProduct(mainImage, subImages, productCreateRequest);

val messageDTO = MessageDTO(
HttpStatus.CREATED.value(),
"상품 생성이 정상적으로 처리됐습니다."
)

return ResponseEntity(messageDTO, HttpStatus.CREATED)
}

// 상품 전체 Offset 페이징 조회
@GetMapping("/offset")
Expand All @@ -69,7 +49,7 @@ private class ProductController(
@GetMapping("/{productId}")
@Operation(summary = "상품 상세 조회", description = "상품 ID를 사용하여 특정 상품의 상세 정보를 조회한다.")
fun getProductDetails(@PathVariable productId: Long): ResponseEntity<ProductResponse> {
val productResponse = productApplicationService.getProductDetails(productId)
val productResponse = productApplicationService.getProductDetails(productId, false)
return ResponseEntity(productResponse, HttpStatus.OK)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import org.store.clothstar.product.dto.request.UpdateDisplayStatusRequest
import org.store.clothstar.product.dto.request.UpdateStockRequest
import org.store.clothstar.product.dto.response.ProductResponse
import org.store.clothstar.product.service.ProductApplicationService
import org.store.clothstar.product.service.ProductSellerApplicationService

@Tag(name = "ProductSellers", description = "ProductSellers(판매자) 관련 API 입니다.")
@RestController
@RequestMapping("/v3/sellers/products")
class ProductSellerController(
private val productApplicationService: ProductApplicationService
private val productSellerApplicationService: ProductSellerApplicationService
) {
// 판매자 상품 등록
@PostMapping
Expand All @@ -32,7 +33,7 @@ class ProductSellerController(
@RequestPart(value = "dto") @Validated productCreateRequest: ProductCreateRequest
): ResponseEntity<MessageDTO> {
// 상품 등록
productApplicationService.createProduct(mainImage, subImages, productCreateRequest);
productSellerApplicationService.createProduct(mainImage, subImages, productCreateRequest);

val messageDTO = MessageDTO(
HttpStatus.CREATED.value(),
Expand All @@ -46,7 +47,7 @@ class ProductSellerController(
@GetMapping("/{productId}")
@Operation(summary = "상품 상세 조회", description = "상품 ID를 사용하여 해당 상품의 상세 정보를 조회합니다.")
fun getProductDetails(@PathVariable productId: Long): ResponseEntity<ProductResponse> {
val productResponse = productApplicationService.getProductDetails(productId, true)
val productResponse = productSellerApplicationService.getProductDetails(productId, true)
return ResponseEntity.ok(productResponse)
}

Expand All @@ -56,7 +57,7 @@ class ProductSellerController(
@PathVariable productId: Long,
@RequestBody request: UpdateDisplayStatusRequest
): ResponseEntity<MessageDTO> {
productApplicationService.updateProductDisplayStatus(productId, request.displayStatus)
productSellerApplicationService.updateProductDisplayStatus(productId, request.displayStatus)

val messageDTO = MessageDTO(
HttpStatus.OK.value(),
Expand All @@ -72,7 +73,7 @@ class ProductSellerController(
@PathVariable itemId: Long,
@RequestBody request: UpdateDisplayStatusRequest
): ResponseEntity<MessageDTO> {
productApplicationService.updateItemDisplayStatus(productId, itemId, request.displayStatus)
productSellerApplicationService.updateItemDisplayStatus(productId, itemId, request.displayStatus)

val messageDTO = MessageDTO(
HttpStatus.OK.value(),
Expand All @@ -89,7 +90,7 @@ class ProductSellerController(
@PathVariable itemId: Long,
@RequestBody request: UpdateStockRequest
): ResponseEntity<MessageDTO> {
productApplicationService.updateItemStock(productId, itemId, request.stock)
productSellerApplicationService.updateItemStock(productId, itemId, request.stock)

val messageDTO = MessageDTO(
HttpStatus.OK.value(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@ package org.store.clothstar.product.service
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Slice
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.multipart.MultipartFile
import org.store.clothstar.member.authentication.domain.CustomUserDetails
import org.store.clothstar.member.service.MemberService
import org.store.clothstar.product.domain.ProductImage
import org.store.clothstar.product.domain.type.DisplayStatus
import org.store.clothstar.product.domain.type.ImageType
import org.store.clothstar.product.dto.request.ProductCreateRequest
import org.store.clothstar.product.dto.response.ProductListResponse
import org.store.clothstar.product.dto.response.ProductResponse

Expand All @@ -24,53 +17,6 @@ class ProductApplicationService(
private val itemService: ItemService,
private val memberService: MemberService
) {
@Transactional
fun createProduct(
mainImage: MultipartFile,
subImages: List<MultipartFile>?,
productCreateRequest: ProductCreateRequest
) {
val member = SecurityContextHolder.getContext().authentication.principal as CustomUserDetails

// if(memberService.isSeller(member.id)) {
// throw IllegalArgumentException("판매자만 상품을 등록할 수 있습니다.")
// }

// 1. 상품 생성
val product = productCreateRequest.toProductEntity()

// 상품 저장
val savedProduct = productService.createProduct(product)

// S3에 이미지 업로드 및 URL 생성
// 2. 메인 이미지 업로드 및 저장
// val mainImageUrl = s3Service.uploadFile(mainImage)
// val mainImageUrl = s3Service.uploadFile(mainImage)
val mainImageUrl =
"https://on.com2us.com/wp-content/uploads/2023/12/VxdEKDNZCp9hAW5TU5-3MZTePLGSdlYKzEZUyVLDB-Cyo950Ee19yaOL8ayxgJzEfMYfzfLcRYuwkmKEs2cg0w.webp"
product.imageList.add(ProductImage(mainImageUrl, mainImageUrl, ImageType.MAIN))

// 3. 서브 이미지 업로드 및® 저장
subImages?.forEach { subImage ->
// val subImageUrl = s3Service.uploadFile(subImage)
val subImageUrl =
"https://on.com2us.com/wp-content/uploads/2023/12/%EC%98%A4%EA%B5%AC%EC%99%80-%EB%B9%84%EB%B0%80%EC%9D%98%EC%88%B2-002.jpg"
product.imageList.add(ProductImage(subImageUrl, subImageUrl, ImageType.SUB))
}


// 4. 상품 옵션 생성
val productOptions = productCreateRequest.productOptions.map { optionRequest ->
val productOption = productOptionService.createProductOption(product, optionRequest)
product.productOptions.add(productOption)
}

// 5. 상품 아이템 생성
productCreateRequest.items.forEach { itemRequest ->
val item = itemService.createItem(product, itemRequest)
product.items.add(item)
}
}

@Transactional(readOnly = true)
fun getProductDetails(productId: Long, isSeller: Boolean): ProductResponse {
Expand All @@ -86,24 +32,4 @@ class ProductApplicationService(
fun getAllProductsSlicePaging(pageable: Pageable, keyword: String?): Slice<ProductListResponse> {
return productService.getAllProductsSlicePaging(pageable, keyword)
}

@Transactional
fun updateProductDisplayStatus(productId: Long, displayStatus: DisplayStatus) {
val product = productService.getProductById(productId)
product.updateDisplayStatus(displayStatus)
}

@Transactional
fun updateItemDisplayStatus(productId: Long, itemId: Long, displayStatus: DisplayStatus) {
val item = itemService.getItemByIdAndProductId(itemId, productId)
item.updateDisplayStatus(displayStatus)
}

@Transactional
fun updateItemStock(productId: Long, itemId: Long, stock: Int) {
// val Member = getPrin

val item = itemService.getItemByIdAndProductId(itemId, productId)
item.updateStock(stock)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.store.clothstar.product.service

import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.multipart.MultipartFile
import org.store.clothstar.member.authentication.domain.CustomUserDetails
import org.store.clothstar.member.service.MemberService
import org.store.clothstar.product.domain.ProductImage
import org.store.clothstar.product.domain.type.DisplayStatus
import org.store.clothstar.product.domain.type.ImageType
import org.store.clothstar.product.dto.request.ProductCreateRequest
import org.store.clothstar.product.dto.response.ProductResponse

@Service
class ProductSellerApplicationService (
private val productService: ProductService,
private val productOptionService: ProductOptionService,
private val itemService: ItemService,
private val memberService: MemberService
) {
@Transactional
fun createProduct(
mainImage: MultipartFile,
subImages: List<MultipartFile>?,
productCreateRequest: ProductCreateRequest
) {
val member = SecurityContextHolder.getContext().authentication.principal as CustomUserDetails

// if(memberService.isSeller(member.id)) {
// throw IllegalArgumentException("판매자만 상품을 등록할 수 있습니다.")
// }

// 1. 상품 생성
val product = productCreateRequest.toProductEntity()

// 상품 저장
val savedProduct = productService.createProduct(product)

// S3에 이미지 업로드 및 URL 생성
// 2. 메인 이미지 업로드 및 저장
// val mainImageUrl = s3Service.uploadFile(mainImage)
// val mainImageUrl = s3Service.uploadFile(mainImage)
val mainImageUrl =
"https://on.com2us.com/wp-content/uploads/2023/12/VxdEKDNZCp9hAW5TU5-3MZTePLGSdlYKzEZUyVLDB-Cyo950Ee19yaOL8ayxgJzEfMYfzfLcRYuwkmKEs2cg0w.webp"
product.imageList.add(ProductImage(mainImageUrl, mainImageUrl, ImageType.MAIN))

// 3. 서브 이미지 업로드 및® 저장
subImages?.forEach { subImage ->
// val subImageUrl = s3Service.uploadFile(subImage)
val subImageUrl =
"https://on.com2us.com/wp-content/uploads/2023/12/%EC%98%A4%EA%B5%AC%EC%99%80-%EB%B9%84%EB%B0%80%EC%9D%98%EC%88%B2-002.jpg"
product.imageList.add(ProductImage(subImageUrl, subImageUrl, ImageType.SUB))
}


// 4. 상품 옵션 생성
val productOptions = productCreateRequest.productOptions.map { optionRequest ->
val productOption = productOptionService.createProductOption(product, optionRequest)
product.productOptions.add(productOption)
}

// 5. 상품 아이템 생성
productCreateRequest.items.forEach { itemRequest ->
val item = itemService.createItem(product, itemRequest)
product.items.add(item)
}
}

@Transactional(readOnly = true)
fun getProductDetails(productId: Long, isSeller: Boolean): ProductResponse {
return productService.getProductDetails(productId, isSeller)
}

@Transactional
fun updateProductDisplayStatus(productId: Long, displayStatus: DisplayStatus) {
val product = productService.getProductById(productId)
product.updateDisplayStatus(displayStatus)
}

@Transactional
fun updateItemDisplayStatus(productId: Long, itemId: Long, displayStatus: DisplayStatus) {
val item = itemService.getItemByIdAndProductId(itemId, productId)
item.updateDisplayStatus(displayStatus)
}

@Transactional
fun updateItemStock(productId: Long, itemId: Long, stock: Int) {
// val Member = getPrin

val item = itemService.getItemByIdAndProductId(itemId, productId)
item.updateStock(stock)
}
}

0 comments on commit 099afcc

Please sign in to comment.