Skip to content

Commit

Permalink
Merge pull request #349 from TrandPick/refactor/348_cache
Browse files Browse the repository at this point in the history
[#348] 부하테스트 후 코드 개선
  • Loading branch information
mmunkyeong authored Jul 9, 2023
2 parents 2d5ac17 + 52079ea commit 03ba36b
Show file tree
Hide file tree
Showing 19 changed files with 164 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "index_member",
@Table(name = "member",
indexes = {@Index(name = "index_member_email", columnList="email", unique = true)})
public class Member extends BaseTimeEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ public RsData<Order> productToOrder(Member member, Long id, int quantity, String
@Transactional
@KafkaListener(topicPattern = "orders", groupId = "group_id")
public void orderToOrder(@Payload String Id) throws JsonProcessingException {
// delay 2sec
try {
Thread.sleep(750);
} catch (InterruptedException e) {
Expand All @@ -125,6 +124,9 @@ public void orderToOrder(@Payload String Id) throws JsonProcessingException {
try {
for (OrderItem orderItem : order.getOrderItems()) {
orderItem.getProduct().getProductOption().decreaseStock(orderItem.getQuantity());
if (orderItem.getProduct().getProductOption().getStock() == 0) {
productService.deleteCache(orderItem.getProduct().getProductOption());
}
}
order.modifyStatus(OrderStatus.ORDERED);
sendMessage("Success", order.getId(), member.getEmail());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

@Slf4j
@Controller
Expand Down Expand Up @@ -85,7 +86,7 @@ public String registerProduct(@ModelAttribute("ProductRequest") ProductRequest p
@PostMapping("/register")
public String register(@ModelAttribute @Valid ProductRequest productRequest,
@RequestParam("mainFile") MultipartFile mainFile,
@RequestParam("subFiles") List<MultipartFile> subFiles) throws IOException {
@RequestParam("subFiles") List<MultipartFile> subFiles) throws IOException, ExecutionException, InterruptedException {
RsData<Long> id = productService.register(productRequest, mainFile, subFiles);
return rq.redirectWithMsg("/trendpick/products/" + id.getData(), id.getMsg());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package project.trendpick_pro.domain.product.entity.product;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum ProductStatus {

SALE("SALE"),
SOLD_OUT("SOLD_OUT"),
STOP("STOP");

private String text;

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package project.trendpick_pro.domain.product.entity.product.dto.request;

import lombok.Getter;
import project.trendpick_pro.domain.product.entity.product.ProductStatus;

@Getter
public class ProductSearchCond {

private String mainCategory;
private String subCategory;
private String keyword;
private Integer sortCode;

public ProductSearchCond(String mainCategory, String subCategory, Integer sortCode) {
this.mainCategory = mainCategory;
this.subCategory = subCategory;
this.sortCode = sortCode;
}

public ProductSearchCond(String mainCategory, String subCategory) {
this.mainCategory = mainCategory;
Expand Down
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
Loading

0 comments on commit 03ba36b

Please sign in to comment.