Skip to content

Commit 9f00122

Browse files
committed
refactor: 상품 좋아요 비정규화
1 parent b518206 commit 9f00122

5 files changed

Lines changed: 28 additions & 24 deletions

File tree

apps/commerce-api/src/main/java/com/loopers/application/like/LikeFacade.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.loopers.domain.like.Like;
44
import com.loopers.domain.like.LikeRepository;
5+
import com.loopers.domain.product.Product;
56
import com.loopers.domain.product.ProductRepository;
67
import com.loopers.domain.user.UserRepository;
78
import com.loopers.interfaces.api.like.LikeV1Dto;
@@ -32,7 +33,7 @@ public LikeInfo doLike(LikeV1Dto.LikeRequest request) {
3233
() -> new CoreException(ErrorType.NOT_FOUND, "존재하지 않는 유저입니다.")
3334
);
3435

35-
productRepository.findById(productId).orElseThrow(
36+
Product product = productRepository.findById(productId).orElseThrow(
3637
() -> new CoreException(ErrorType.NOT_FOUND, "존재하지 않는 상품입니다.")
3738
);
3839

@@ -42,6 +43,8 @@ public LikeInfo doLike(LikeV1Dto.LikeRequest request) {
4243
Like newLike = request.toEntity();
4344
likeRepository.save(newLike);
4445

46+
product.addLikeCount();
47+
4548
return LikeInfo.from(newLike);
4649
});
4750
}

apps/commerce-api/src/main/java/com/loopers/application/product/ProductFacade.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,15 @@ public ProductInfo registerProduct(ProductV1Dto.ProductRequest request) {
3232
Product product = request.toEntity();
3333
productRepository.save(product);
3434

35-
return ProductInfo.from(product, 0);
35+
return ProductInfo.from(product);
3636
}
3737

3838
@Transactional(readOnly = true)
3939
public List<ProductInfo> findAllProducts() {
4040
List<Product> products = productRepository.findAll();
4141

4242
return products.stream()
43-
.map(product -> {
44-
int likeCount = likeRepository.countByProductId(product.getId());
45-
return ProductInfo.from(product, likeCount);
46-
})
43+
.map(ProductInfo::from)
4744
.toList();
4845
}
4946

@@ -53,9 +50,7 @@ public ProductInfo findProductById(Long id) {
5350
() -> new CoreException(ErrorType.NOT_FOUND, "찾고자 하는 상품이 존재하지 않습니다.")
5451
);
5552

56-
int likeCount = likeRepository.countByProductId(id);
57-
58-
return ProductInfo.from(product, likeCount);
53+
return ProductInfo.from(product);
5954
}
6055

6156
@Transactional(readOnly = true)
@@ -66,10 +61,7 @@ public List<ProductInfo> searchProductsByCondition(ProductV1Dto.SearchProductReq
6661
List<Product> products = productRepository.searchProductsByCondition(request);
6762

6863
return products.stream()
69-
.map(product -> {
70-
int likeCount = likeRepository.countByProductId(product.getId());
71-
return ProductInfo.from(product, likeCount);
72-
})
64+
.map(ProductInfo::from)
7365
.toList();
7466
}
7567
}

apps/commerce-api/src/main/java/com/loopers/application/product/ProductInfo.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
import java.math.BigDecimal;
66

77
public record ProductInfo(Long id, Long brandId, String name, BigDecimal price, int stock, int likeCount) {
8-
public static ProductInfo from(Product product, int likeCount) {
8+
public static ProductInfo from(Product product) {
99
return new ProductInfo(
10-
product.getId(),
11-
product.getBrandId(),
12-
product.getName(),
13-
product.getPrice(),
14-
product.getStock(),
15-
likeCount
10+
product.getId(),
11+
product.getBrandId(),
12+
product.getName(),
13+
product.getPrice(),
14+
product.getStock(),
15+
product.getLikeCount()
1616
);
17+
1718
}
1819
}

apps/commerce-api/src/main/java/com/loopers/domain/product/Product.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,24 @@ public class Product extends BaseEntity {
2929
@Column(name = "stock", nullable = false)
3030
private int stock;
3131

32+
@Column(name = "like_count", nullable = false)
33+
private int likeCount;
3234

33-
public Product(Long brandId, String name, BigDecimal price, int stock) {
35+
36+
public Product(Long brandId, String name, BigDecimal price, int stock, int likeCount) {
3437
this.brandId = brandId;
3538
this.name = name;
3639
this.price = price;
3740
this.stock = stock;
41+
this.likeCount = likeCount;
3842
}
3943

4044
public Product changePrice(BigDecimal newPrice) {
4145
this.price = newPrice;
4246
return this;
4347
}
4448

45-
public Product decreaseStock(int amount) {
49+
public void decreaseStock(int amount) {
4650
if (amount <= 0) {
4751
throw new CoreException(ErrorType.BAD_REQUEST, "재고 감소량은 0보다 커야 합니다.");
4852
}
@@ -51,11 +55,14 @@ public Product decreaseStock(int amount) {
5155
}
5256
this.stock -= amount;
5357

54-
return this;
5558
}
5659

5760
public boolean isInStock(int quantity) {
5861
return this.stock >= quantity;
5962
}
6063

64+
public int addLikeCount() {
65+
return this.likeCount++;
66+
}
67+
6168
}

apps/commerce-api/src/main/java/com/loopers/interfaces/api/product/ProductV1Dto.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public Product toEntity() {
2727
brandId,
2828
name,
2929
price,
30-
stock
30+
stock,
31+
0
3132
);
3233
}
3334
}

0 commit comments

Comments
 (0)