Skip to content

Commit

Permalink
refactor: 상품 자동 완성 검색 관련 dto 클래스 record로 타입 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Go-Jaecheol committed Jun 6, 2024
1 parent fc2e981 commit 0075c0d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 65 deletions.
50 changes: 8 additions & 42 deletions src/main/java/com/funeat/product/dto/SearchProductDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,17 @@

import com.funeat.product.domain.Product;

public class SearchProductDto {

private final Long id;
private final String name;
private final Long price;
private final String image;
private final Double averageRating;
private final String categoryType;

public SearchProductDto(final Long id, final String name, final Long price, final String image,
final Double averageRating, final String categoryType) {
this.id = id;
this.name = name;
this.price = price;
this.image = image;
this.averageRating = averageRating;
this.categoryType = categoryType;
}
public record SearchProductDto(
Long id,
String name,
Long price,
String image,
Double averageRating,
String categoryType
) {

public static SearchProductDto toDto(final Product product) {
return new SearchProductDto(product.getId(), product.getName(), product.getPrice(), product.getImage(),
product.getAverageRating(), product.getCategory().getType().getName());
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public Long getPrice() {
return price;
}

public String getImage() {
return image;
}

public Double getAverageRating() {
return averageRating;
}

public String getCategoryType() {
return categoryType;
}
}
21 changes: 4 additions & 17 deletions src/main/java/com/funeat/product/dto/SearchProductsResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,12 @@

import java.util.List;

public class SearchProductsResponse {

private final boolean hasNext;
private final List<SearchProductDto> products;

private SearchProductsResponse(final boolean hasNext, final List<SearchProductDto> products) {
this.hasNext = hasNext;
this.products = products;
}
public record SearchProductsResponse(
boolean hasNext,
List<SearchProductDto> products
) {

public static SearchProductsResponse toResponse(final boolean hasNext, final List<SearchProductDto> products) {
return new SearchProductsResponse(hasNext, products);
}

public boolean isHasNext() {
return hasNext;
}

public List<SearchProductDto> getProducts() {
return products;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,12 @@
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;
import io.restassured.response.Response;

import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -451,8 +448,8 @@ class searchProducts_성공_테스트 {

// when
final var 응답1 = 상품_자동_완성_검색_요청("망고", 0L);
final var result = 응답1.as(SearchProductsResponse.class).getProducts();
final var lastId = result.get(result.size() - 1).getId();
final var result = 응답1.as(SearchProductsResponse.class).products();
final var lastId = result.get(result.size() - 1).id();
final var 응답2 = 상품_자동_완성_검색_요청("망고", lastId);

// then
Expand Down Expand Up @@ -710,7 +707,7 @@ class getProductRecipes_성공_테스트 {
.getList("products", SearchProductDto.class);

assertThat(actualHasNext).isEqualTo(hasNext);
assertThat(actualProducts).extracting(SearchProductDto::getId)
assertThat(actualProducts).extracting(SearchProductDto::id)
.isEqualTo(productIds);
}

Expand Down

0 comments on commit 0075c0d

Please sign in to comment.