Skip to content

Commit

Permalink
refactor: 이미지 업로드 시 @ModelAttribute 를 사용하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Combi153 committed Apr 26, 2024
1 parent 5685eb6 commit a62bd4d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.petqua.presentation.product.dto.CreateReviewRequest
import com.petqua.presentation.product.dto.ReadAllProductReviewsRequest
import com.petqua.presentation.product.dto.UpdateReviewRecommendationRequest
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.security.SecurityRequirement
import io.swagger.v3.oas.annotations.tags.Tag
Expand All @@ -20,12 +19,11 @@ import org.springframework.http.MediaType.APPLICATION_JSON_VALUE
import org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestPart
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile

@Tag(name = "ProductReview", description = "상품 후기 관련 API 명세")
@RestController
Expand All @@ -44,15 +42,12 @@ class ProductReviewController(
fun create(
@Auth loginMember: LoginMember,
@PathVariable productId: Long,
@Parameter(description = "application/json 형식의 dto, key = review")
@RequestPart(value = "review") request: CreateReviewRequest,
@Parameter(description = "multipart/form-data 형식의 이미지 리스트, key = images")
@RequestPart(value = "images", required = false) images: List<MultipartFile>?,
@ModelAttribute request: CreateReviewRequest,
): ResponseEntity<ProductReviewsResponse> {
val command = request.toCommand(
memberId = loginMember.memberId,
productId = productId,
images = images ?: listOf()
images = request.images
)
productReviewFacadeService.create(command)
return ResponseEntity.status(CREATED).build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ data class CreateReviewRequest(
example = "아주 좋네요"
)
val content: String,

@Schema(
description = "상품 후기 이미지 목록",
)
val images: List<MultipartFile> = listOf(),
) {
fun toCommand(memberId: Long, productId: Long, images: List<MultipartFile>): ProductReviewCreateCommand {
return ProductReviewCreateCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@ import io.restassured.module.kotlin.extensions.When
import io.restassured.response.Response
import org.springframework.http.MediaType.APPLICATION_JSON_VALUE
import org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE
import org.springframework.web.multipart.MultipartFile

fun requestCreateProductReview(
accessToken: String,
productId: Long,
request: CreateReviewRequest,
images: List<MultipartFile>,
): Response {
val requestGivenSpec = Given {
log().all()
contentType(MULTIPART_FORM_DATA_VALUE)
auth().preemptive().oauth2(accessToken)
pathParam("productId", productId)
multiPart("review", request, APPLICATION_JSON_VALUE)
multiPart("score", request.score)
multiPart("content", request.content)
}

images.forEach { image ->
request.images.forEach { image ->
requestGivenSpec.multiPart("images", image.name, image.bytes, image.contentType)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class ProductReviewControllerTest(
request = CreateReviewRequest(
score = 5,
content = "this product is good",
images = listOf(image1, image2),
),
images = listOf(image1, image2),
)

Then("201 Created 를 응답한다") {
Expand Down Expand Up @@ -116,8 +116,8 @@ class ProductReviewControllerTest(
request = CreateReviewRequest(
score = 5,
content = "this product is good",
images = listOf(),
),
images = listOf(),
)

Then("201 Created 를 응답한다") {
Expand Down Expand Up @@ -150,11 +150,11 @@ class ProductReviewControllerTest(
request = CreateReviewRequest(
score = 5,
content = "this product is good",
),
images = listOf(
image1, image1, image1, image1, image1,
image1, image1, image1, image1, image1,
image2
images = listOf(
image1, image1, image1, image1, image1,
image1, image1, image1, image1, image1,
image2
),
),
)

Expand All @@ -179,8 +179,8 @@ class ProductReviewControllerTest(
request = CreateReviewRequest(
score = 0,
content = "this product is good",
images = listOf(image1),
),
images = listOf(image1),
)

Then("예외를 응답한다") {
Expand All @@ -204,8 +204,8 @@ class ProductReviewControllerTest(
request = CreateReviewRequest(
score = 5,
content = "this product is good".repeat(30),
images = listOf(image1),
),
images = listOf(image1),
)

Then("예외를 응답한다") {
Expand Down

0 comments on commit a62bd4d

Please sign in to comment.