Skip to content

Commit

Permalink
refactor: 상품 상세 정보에 카테고리 정보 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
70825 committed Apr 3, 2024
1 parent 2f712af commit de45ffc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/funeat/product/dto/CategoryDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 12 additions & 4 deletions src/main/java/com/funeat/product/dto/ProductResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,27 +16,30 @@ public class ProductResponse {
private final String content;
private final Double averageRating;
private final Long reviewCount;
private final CategoryDto category;
private final List<TagDto> 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<TagDto> tags) {
final Double averageRating, final Long reviewCount, final CategoryDto category, final List<TagDto> tags) {
this.id = id;
this.name = name;
this.price = price;
this.image = image;
this.content = content;
this.averageRating = averageRating;
this.reviewCount = reviewCount;
this.category = category;
this.tags = tags;
}

public static ProductResponse toResponse(final Product product, final List<Tag> tags) {
List<TagDto> tagDtos = new ArrayList<>();
for (Tag tag : tags) {
final CategoryDto categoryDto = CategoryDto.toDto(product.getCategory());
final List<TagDto> 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() {
Expand Down Expand Up @@ -66,6 +70,10 @@ public Long getReviewCount() {
return reviewCount;
}

public CategoryDto getCategory() {
return category;
}

public List<TagDto> getTags() {
return tags;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -673,6 +674,7 @@ class getProductRecipes_성공_테스트 {

private void 상품_상세_정보_조회_결과를_검증한다(final ExtractableResponse<Response> 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);

Expand All @@ -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);
});
}
Expand Down

0 comments on commit de45ffc

Please sign in to comment.