diff --git a/src/main/java/com/funeat/product/dto/CategoryDto.java b/src/main/java/com/funeat/product/dto/CategoryDto.java new file mode 100644 index 00000000..13fa0121 --- /dev/null +++ b/src/main/java/com/funeat/product/dto/CategoryDto.java @@ -0,0 +1,32 @@ +package com.funeat.product.dto; + +import com.funeat.product.domain.Category; + +public class CategoryDto { + + private final Long id; + private final String name; + private final String image; + + private CategoryDto(final Long id, final String name, final String image) { + this.id = id; + this.name = name; + this.image = image; + } + + public static CategoryDto toDto(final Category category) { + return new CategoryDto(category.getId(), category.getName(), category.getImage()); + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public String getImage() { + return image; + } +} diff --git a/src/main/java/com/funeat/product/dto/ProductResponse.java b/src/main/java/com/funeat/product/dto/ProductResponse.java index d3c0ed26..6bc17fda 100644 --- a/src/main/java/com/funeat/product/dto/ProductResponse.java +++ b/src/main/java/com/funeat/product/dto/ProductResponse.java @@ -3,6 +3,7 @@ import com.funeat.product.domain.Product; import com.funeat.tag.domain.Tag; import com.funeat.tag.dto.TagDto; + import java.util.ArrayList; import java.util.List; @@ -15,10 +16,11 @@ public class ProductResponse { private final String content; private final Double averageRating; private final Long reviewCount; + private final CategoryDto category; private final List tags; public ProductResponse(final Long id, final String name, final Long price, final String image, final String content, - final Double averageRating, final Long reviewCount, final List tags) { + final Double averageRating, final Long reviewCount, final CategoryDto category, final List tags) { this.id = id; this.name = name; this.price = price; @@ -26,16 +28,18 @@ public ProductResponse(final Long id, final String name, final Long price, final this.content = content; this.averageRating = averageRating; this.reviewCount = reviewCount; + this.category = category; this.tags = tags; } public static ProductResponse toResponse(final Product product, final List tags) { - List tagDtos = new ArrayList<>(); - for (Tag tag : tags) { + final CategoryDto categoryDto = CategoryDto.toDto(product.getCategory()); + final List tagDtos = new ArrayList<>(); + for (final Tag tag : tags) { tagDtos.add(TagDto.toDto(tag)); } return new ProductResponse(product.getId(), product.getName(), product.getPrice(), product.getImage(), - product.getContent(), product.getAverageRating(), product.getReviewCount(), tagDtos); + product.getContent(), product.getAverageRating(), product.getReviewCount(), categoryDto, tagDtos); } public Long getId() { @@ -66,6 +70,10 @@ public Long getReviewCount() { return reviewCount; } + public CategoryDto getCategory() { + return category; + } + public List getTags() { return tags; } diff --git a/src/test/java/com/funeat/acceptance/product/ProductAcceptanceTest.java b/src/test/java/com/funeat/acceptance/product/ProductAcceptanceTest.java index 87c0d300..fb236595 100644 --- a/src/test/java/com/funeat/acceptance/product/ProductAcceptanceTest.java +++ b/src/test/java/com/funeat/acceptance/product/ProductAcceptanceTest.java @@ -98,6 +98,7 @@ import com.funeat.product.dto.SearchProductResultDto; import com.funeat.product.dto.SearchProductResultsResponse; import com.funeat.product.dto.SearchProductsResponse; +import com.funeat.product.dto.CategoryDto; import com.funeat.recipe.dto.RecipeDto; import com.funeat.tag.dto.TagDto; import io.restassured.response.ExtractableResponse; @@ -673,6 +674,7 @@ class getProductRecipes_성공_테스트 { private void 상품_상세_정보_조회_결과를_검증한다(final ExtractableResponse response) { final var actual = response.as(ProductResponse.class); + final var actualCategory = response.jsonPath().getObject("category", CategoryDto.class); final var actualTags = response.jsonPath() .getList("tags", TagDto.class); @@ -684,6 +686,7 @@ class getProductRecipes_성공_테스트 { soft.assertThat(actual.getContent()).isEqualTo("맛있는 삼각김밥"); soft.assertThat(actual.getAverageRating()).isEqualTo(3.0); soft.assertThat(actual.getReviewCount()).isEqualTo(3L); + soft.assertThat(actualCategory.getName()).isEqualTo("간편식사"); soft.assertThat(actualTags).extracting("id").containsExactly(2L, 1L); }); }