Skip to content

Commit

Permalink
refactor: dto 직렬화 적용 (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelSuho committed Jul 9, 2023
1 parent b71420f commit 52079ea
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
import com.querydsl.core.annotations.QueryProjection;
import lombok.*;
import project.trendpick_pro.domain.product.entity.product.Product;

import java.io.Serializable;
//import project.trendpick_pro.global.search.entity.ProductSearch;

@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ProductListResponse {
public class ProductListResponse implements Serializable {

private Long id;
private String name;
private String brand;
private String mainFile;
private int price;

private int discountRate;

private int discountedPrice;

@Builder
Expand All @@ -32,15 +32,25 @@ public ProductListResponse(Long id, String name, String brand, String mainFile,
}

public static ProductListResponse of(Product product) {
return ProductListResponse.builder()
.id(product.getId())
.name(product.getName())
.brand(product.getBrand().getName())
.mainFile(product.getFile().getFileName())
.price(product.getProductOption().getPrice())
.discountedPrice(product.getDiscountedPrice())
.discountRate(product.getDiscountRate())
.build();
if (product.getDiscountedPrice() == 0 && product.getDiscountRate() == 0) {
return ProductListResponse.builder()
.id(product.getId())
.name(product.getName())
.brand(product.getBrand().getName())
.mainFile(product.getFile().getFileName())
.price(product.getProductOption().getPrice())
.build();
} else {
return ProductListResponse.builder()
.id(product.getId())
.name(product.getName())
.brand(product.getBrand().getName())
.mainFile(product.getFile().getFileName())
.price(product.getProductOption().getPrice())
.discountedPrice(product.getDiscountedPrice())
.discountRate(product.getDiscountRate())
.build();
}
}

// public static ProductListResponse of(ProductSearch product) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import project.trendpick_pro.domain.product.entity.product.Product;
import project.trendpick_pro.domain.tags.tag.entity.Tag;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ProductResponse {
public class ProductResponse implements Serializable {

private Long id;
private String name;
Expand All @@ -27,17 +28,14 @@ public class ProductResponse {
private List<String> sizes;
private List<String> colors;
private int price;
private int stock;
private List<Tag> tags = new ArrayList<>();

private List<String> tags = new ArrayList<>();
private int discountRate;

private int discountedPrice;

@Builder
@QueryProjection
public ProductResponse(Long id, String name, String mainCategory, String subCategory, String brand, String description,
String mainFile, List<String> subFiles, List<String> sizes, List<String> colors, int price, int stock, List<Tag> tags, double discountRate, int discountedPrice) {
String mainFile, List<String> subFiles, List<String> sizes, List<String> colors, int price, List<String> tags, double discountRate, int discountedPrice) {
this.id = id;
this.name = name;
this.mainCategory = mainCategory;
Expand All @@ -49,30 +47,45 @@ public ProductResponse(Long id, String name, String mainCategory, String subCate
this.sizes = sizes;
this.colors = colors;
this.price = price;
this.stock = stock;
this.tags = tags;
this.discountRate = (int) discountRate;
this.discountedPrice = discountedPrice;
}

public static ProductResponse of (Product product) {
return ProductResponse.builder()
.id(product.getId())
.name(product.getName())
.mainCategory(product.getMainCategory().getName())
.subCategory(product.getSubCategory().getName())
.brand(product.getBrand().getName())
.description(product.getDescription())
.mainFile(product.getFile().getFileName())
.subFiles(subFiles(product.getFile().getChild()))
.sizes(product.getProductOption().getSizes())
.colors(product.getProductOption().getColors())
.price(product.getProductOption().getPrice())
.stock(product.getProductOption().getStock())
.tags(new ArrayList<>(product.getTags()))
.discountedPrice(product.getDiscountedPrice())
.discountRate(product.getDiscountRate())
.build();
if (product.getDiscountedPrice() == 0 && product.getDiscountRate() == 0) {
return ProductResponse.builder()
.id(product.getId())
.name(product.getName())
.mainCategory(product.getMainCategory().getName())
.subCategory(product.getSubCategory().getName())
.brand(product.getBrand().getName())
.description(product.getDescription())
.mainFile(product.getFile().getFileName())
.subFiles(subFiles(product.getFile().getChild()))
.sizes(product.getProductOption().getSizes())
.colors(product.getProductOption().getColors())
.price(product.getProductOption().getPrice())
.tags(product.getTags().stream().map(Tag::getName).toList())
.build();
} else {
return ProductResponse.builder()
.id(product.getId())
.name(product.getName())
.mainCategory(product.getMainCategory().getName())
.subCategory(product.getSubCategory().getName())
.brand(product.getBrand().getName())
.description(product.getDescription())
.mainFile(product.getFile().getFileName())
.subFiles(subFiles(product.getFile().getChild()))
.sizes(product.getProductOption().getSizes())
.colors(product.getProductOption().getColors())
.price(product.getProductOption().getPrice())
.tags(product.getTags().stream().map(Tag::getName).toList())
.discountedPrice(product.getDiscountedPrice())
.discountRate(product.getDiscountRate())
.build();
}
}

private static List<String> subFiles(List<CommonFile> subFiles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import project.trendpick_pro.domain.product.entity.product.ProductStatus;
import project.trendpick_pro.domain.product.entity.productOption.dto.ProductOptionSaveRequest;
import project.trendpick_pro.domain.product.exception.ProductStockOutException;

import java.io.Serializable;
import java.util.List;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ProductOption {
public class ProductOption implements Serializable {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ElementCollection
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "product_option_sizes", joinColumns = @JoinColumn(name = "product_option_id"))
@Column(name = "size")
private List<String> sizes;

@ElementCollection
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "product_option_colors", joinColumns = @JoinColumn(name = "product_option_id"))
@Column(name = "color")
private List<String> colors;
Expand All @@ -34,8 +36,15 @@ public class ProductOption {
@Column(name = "price", nullable = false)
private int price;

@Enumerated(EnumType.STRING)
private ProductStatus status;

public void connectStatus(ProductStatus status){
this.status = status;
}

@Builder
public ProductOption(List<String> size, List<String> color, int stock, int price) {
private ProductOption(List<String> size, List<String> color, int stock, int price) {
this.sizes = size;
this.colors = color;
this.stock = stock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ public class ProductOptionSaveRequest {
@Min(value = 1, message = "가격을 입력해주세요.(1원 이상부터 입력할 수 있습니다.)")
private int price;

private String status;

@Builder
public ProductOptionSaveRequest(List<String> sizes, List<String> colors, int stock, int price) {
public ProductOptionSaveRequest(List<String> sizes, List<String> colors, int stock, int price, String status) {
this.sizes = sizes;
this.colors = colors;
this.stock = stock;
this.price = price;
this.status = status;
}

public static ProductOptionSaveRequest of (List<String> sizes, List<String> colors, int stock, int price) {
public static ProductOptionSaveRequest of (List<String> sizes, List<String> colors, int stock, int price, String status) {
return ProductOptionSaveRequest.builder()
.sizes(sizes)
.colors(colors)
.stock(stock)
.price(price)
.status(status)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.support.PageableExecutionUtils;
import project.trendpick_pro.domain.product.entity.product.ProductStatus;
import project.trendpick_pro.domain.product.entity.product.dto.request.ProductSearchCond;
import project.trendpick_pro.domain.product.entity.product.dto.response.*;
import project.trendpick_pro.domain.product.entity.productOption.QProductOption;
Expand Down Expand Up @@ -52,8 +53,8 @@ public Page<ProductListResponse> findAllByCategoryId(ProductSearchCond cond, Pag
.leftJoin(product.productOption, productOption)
.leftJoin(product.file, commonFile)
.where(
mainCategoryEq(cond)
.and(subCategoryEq(cond))
mainCategoryEq(cond),
subCategoryEq(cond)
)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
Expand Down

0 comments on commit 52079ea

Please sign in to comment.