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: Product 상세 조회 구현 #14

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -61,7 +61,7 @@ class SecurityConfiguration(
"/v1/members/login", "/signup", "/v1/members/email/**", "/v1/access",
"/v1/categories/**", "/v1/products/**", "/v1/productLines/**", "/v2/productLines/**",
"/productLinePagingSlice", "/productLinePagingOffset",
"/v3/products",
"/v3/products/**",
"/v1/orderdetails", "/v1/orders", "membersPagingOffset", "membersPagingSlice",
"/v1/orderdetails", "/v1/orders", "/v2/orders", "/v3/orders", "/v1/orders/list",
"/v1/orders/list", "/ordersPagingOffset", "/ordersPagingSlice", "/v2/orders/list",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@ import org.springframework.http.ResponseEntity
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import org.store.clothstar.category.service.CategoryService
import org.store.clothstar.common.dto.MessageDTO
import org.store.clothstar.product.dto.request.ProductCreateRequest
import org.store.clothstar.product.dto.response.ProductResponse
import org.store.clothstar.product.service.ProductApplicationService
import org.store.clothstar.product.service.ProductService
import java.net.URI

@Tag(name = "Products", description = "Products(상품 옵션) 관련 API 입니다.")
@RequestMapping("/v3/products")
@RestController
private class ProductController (
private val productApplicationService: ProductApplicationService,
private val productService: ProductService,
private val categoryService: CategoryService,
) {
) {
@PostMapping
@Operation(summary = "상품 등록",
description = "카테고리 아이디, 상품 이름, 내용, 가격, 상태를 입력하여 상품을 신규 등록한다.")
Expand All @@ -41,4 +37,11 @@ private class ProductController (
return ResponseEntity(messageDTO, HttpStatus.CREATED)

}

@GetMapping("/{productId}")
@Operation(summary = "상품 상세 조회", description = "상품 ID를 사용하여 특정 상품의 상세 정보를 조회한다.")
fun getProductDetails(@PathVariable productId: Long): ResponseEntity<ProductResponse> {
val productResponse = productApplicationService.getProductDetails(productId)
return ResponseEntity(productResponse, HttpStatus.OK)
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/org/store/clothstar/product/domain/Item.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.store.clothstar.product.domain

import jakarta.persistence.*
import org.hibernate.annotations.BatchSize
import org.store.clothstar.product.domain.type.DisplayStatus
import org.store.clothstar.product.domain.type.SaleStatus

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.store.clothstar.product.domain

import jakarta.persistence.Embeddable
import org.hibernate.annotations.BatchSize

/**
* { "optionId": 1, "name": "색상", "value": "중청", "valueId": 1 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.store.clothstar.product.domain

import jakarta.persistence.*
import org.hibernate.annotations.BatchSize
import org.store.clothstar.product.domain.type.ProductColor


Expand Down
12 changes: 9 additions & 3 deletions src/main/kotlin/org/store/clothstar/product/domain/Product.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.store.clothstar.product.domain

import jakarta.persistence.*
import org.hibernate.annotations.BatchSize
import org.hibernate.annotations.Fetch
import org.hibernate.annotations.FetchMode
import org.store.clothstar.common.entity.BaseEntity
import org.store.clothstar.product.domain.type.DisplayStatus
import org.store.clothstar.product.domain.type.ProductColor
Expand All @@ -24,17 +27,18 @@ class Product(
var name: String,
var content: String,
var price: Int,
// 색상 목록

// 색상 목록
@ElementCollection(targetClass = ProductColor::class)
@CollectionTable(name = "product_colors", joinColumns = [JoinColumn(name = "product_id")])
@Enumerated(EnumType.STRING)
@Fetch(FetchMode.SUBSELECT)
var productColors: MutableSet<ProductColor> = mutableSetOf(),

// 이미지 목록
@ElementCollection
@CollectionTable(name = "product_image", joinColumns = [JoinColumn(name = "product_line_id")])
var imageList: MutableList<ProductImage> = mutableListOf(),
var imageList: MutableSet<ProductImage> = mutableSetOf(),

// 기타 정보
var saleCount: Long = 0,
Expand All @@ -44,9 +48,11 @@ class Product(
var saleStatus: SaleStatus, // 판매 상태

// 연관 관계 (1:N)
@Fetch(FetchMode.SUBSELECT)
@OneToMany(mappedBy = "product", cascade = [CascadeType.ALL], fetch = FetchType.LAZY)
var productOptions: MutableSet<ProductOption> = mutableSetOf(),
var productOptions: MutableList<ProductOption> = mutableListOf(),

@Fetch(FetchMode.SUBSELECT)
@OneToMany(mappedBy = "product", fetch = FetchType.LAZY, cascade = [CascadeType.ALL])
var items: MutableList<Item> = mutableListOf(),
) : BaseEntity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.store.clothstar.product.domain
import jakarta.persistence.Embeddable
import jakarta.persistence.EnumType
import jakarta.persistence.Enumerated
import org.hibernate.annotations.BatchSize
import org.store.clothstar.product.domain.type.ImageType

@Embeddable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.store.clothstar.product.domain

import jakarta.persistence.*
import org.hibernate.annotations.BatchSize


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class ProductResponse(
id = product.productId!!,
name = product.name,
description = product.content,
imageList = product.imageList.map { ImageResponse.from(it) },
productColors = product.productColors.toSet(),
price = product.price,
displayStatus = product.displayStatus,
saleStatus = product.saleStatus,
productColors = product.productColors.toSet(),
imageList = product.imageList.map { ImageResponse.from(it) },
productOptions = product.productOptions.map { ProductOptionResponse.from(it) },
items = product.items.map { ItemResponse.from(it) }
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.store.clothstar.product.repository

import org.springframework.data.jpa.repository.EntityGraph
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import org.store.clothstar.product.domain.Product
import java.util.*

@Repository
interface ProductRepository : JpaRepository<Product, Long> {
fun findByProductIdIn(productLineIds: List<Long>): List<Product?>

fun findWithDetailsByProductId(productId: Long): Optional<Product>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import org.springframework.web.multipart.MultipartFile
import org.store.clothstar.member.domain.Member
import org.store.clothstar.member.domain.vo.MemberShoppingActivity
import org.store.clothstar.member.service.MemberService
import org.store.clothstar.product.domain.Product
import org.store.clothstar.product.domain.ProductImage
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 ProductApplicationService(
Expand Down Expand Up @@ -41,7 +43,7 @@ class ProductApplicationService(
val mainImageUrl = "https://on.com2us.com/wp-content/uploads/2023/12/VxdEKDNZCp9hAW5TU5-3MZTePLGSdlYKzEZUyVLDB-Cyo950Ee19yaOL8ayxgJzEfMYfzfLcRYuwkmKEs2cg0w.webp"
product.imageList.add(ProductImage(mainImageUrl, mainImageUrl, ImageType.MAIN))

// 3. 서브 이미지 업로드 및 저장
// 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"
Expand All @@ -61,4 +63,10 @@ class ProductApplicationService(
product.items.add(item)
}
}

@Transactional(readOnly = true)
fun getProductDetails(productId: Long): ProductResponse {

return productService.getProductDetails(productId)
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
package org.store.clothstar.product.service

import jakarta.persistence.EntityNotFoundException
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.server.ResponseStatusException
import org.store.clothstar.product.domain.Product
import org.store.clothstar.product.dto.response.ProductResponse
import org.store.clothstar.product.repository.ItemRepository
import org.store.clothstar.product.repository.OptionValueRepository
import org.store.clothstar.product.repository.ProductOptionRepository
import org.store.clothstar.product.repository.ProductRepository

@Service
class ProductService(
private val productRepository: ProductRepository,
) {
@Transactional
fun createProduct(product: Product): Product {
return productRepository.save(product)
}

@Transactional(readOnly = true)
fun getProductDetails(productId: Long): ProductResponse {
val product = productRepository.findWithDetailsByProductId(productId)
.orElseThrow { EntityNotFoundException("상품을 찾을 수 없습니다.") }

return ProductResponse.from(product)
}

fun findByProductIdIn(productIds: List<Long>): List<Product> {
return productRepository.findByProductIdIn(productIds).map {
it ?: throw IllegalArgumentException("상품을 조회할 수 없습니다.")
}
}

@Transactional
fun createProduct(product: Product): Product {
return productRepository.save(product)
}
}
6 changes: 3 additions & 3 deletions src/main/resources/application-db.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ spring:
jpa:
properties:
hibernate:
default_batch_fetch_size: 1000
default_batch_fetch_size: 200

--- # local 공통 설정
spring:
Expand Down Expand Up @@ -67,8 +67,8 @@ spring:
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.MySQLDialect
properties:
hibernate:
format_sql: true
format_sql: true
show_sql: true
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jasypt:
spring:
profiles:
active:
- local, mybatis
- local
group:
local:
- db-local
Expand Down
Loading