Skip to content

Commit be7a2e8

Browse files
authored
Merge pull request #77 from rnqhstmd/main
[volume-3] 도메인 모델링 및 구현
2 parents 02c10f1 + ac853f9 commit be7a2e8

78 files changed

Lines changed: 2415 additions & 530 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.loopers.application.brand;
2+
3+
import com.loopers.domain.brand.BrandService;
4+
import lombok.RequiredArgsConstructor;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
@Component
9+
@RequiredArgsConstructor
10+
@Transactional(readOnly = true)
11+
public class BrandFacade {
12+
13+
private final BrandService brandService;
14+
15+
public BrandInfo getBrand(Long id) {
16+
return BrandInfo.from(brandService.getBrand(id));
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.loopers.application.brand;
2+
3+
import com.loopers.domain.brand.Brand;
4+
5+
public record BrandInfo (
6+
Long id,
7+
String name
8+
) {
9+
public static BrandInfo from(Brand brand) {
10+
return new BrandInfo(brand.getId(), brand.getName());
11+
}
12+
}

apps/commerce-api/src/main/java/com/loopers/application/example/ExampleFacade.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

apps/commerce-api/src/main/java/com/loopers/application/example/ExampleInfo.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.loopers.application.like;
2+
3+
public record LikeCommand(
4+
String userId,
5+
Long productId
6+
) {
7+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.loopers.application.like;
2+
3+
import com.loopers.domain.like.LikeService;
4+
import com.loopers.domain.product.Product;
5+
import com.loopers.domain.product.ProductService;
6+
import com.loopers.domain.user.User;
7+
import com.loopers.domain.user.UserService;
8+
import lombok.RequiredArgsConstructor;
9+
import org.springframework.stereotype.Component;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
@Component
13+
@RequiredArgsConstructor
14+
@Transactional(readOnly = true)
15+
public class LikeFacade {
16+
17+
private final LikeService likeService;
18+
private final UserService userService;
19+
private final ProductService productService;
20+
21+
@Transactional
22+
public void addLike(String userId, Long productId) {
23+
User user = userService.getUserByUserId(userId);
24+
Product product = productService.getProduct(productId);
25+
likeService.addLike(user, product);
26+
}
27+
28+
@Transactional
29+
public void removeLike(String userId, Long productId) {
30+
User user = userService.getUserByUserId(userId);
31+
Product product = productService.getProduct(productId);
32+
likeService.removeLike(user, product);
33+
}
34+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.loopers.application.order;
2+
3+
import com.loopers.domain.order.Order;
4+
import com.loopers.domain.order.OrderService;
5+
import com.loopers.domain.point.PointService;
6+
import com.loopers.domain.product.Product;
7+
import com.loopers.domain.product.ProductService;
8+
import com.loopers.domain.user.User;
9+
import com.loopers.domain.user.UserService;
10+
import com.loopers.support.error.CoreException;
11+
import com.loopers.support.error.ErrorType;
12+
import lombok.RequiredArgsConstructor;
13+
import org.springframework.stereotype.Component;
14+
import org.springframework.transaction.annotation.Transactional;
15+
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.function.Function;
19+
import java.util.stream.Collectors;
20+
21+
@Component
22+
@RequiredArgsConstructor
23+
@Transactional(readOnly = true)
24+
public class OrderFacade {
25+
26+
private final OrderService orderService;
27+
private final UserService userService;
28+
private final ProductService productService;
29+
private final PointService pointService;
30+
31+
@Transactional
32+
public OrderInfo placeOrder(OrderPlaceCommand command) {
33+
User user = userService.getUserByUserId(command.userId());
34+
35+
List<Long> productIds = command.items().stream()
36+
.map(OrderPlaceCommand.OrderItemCommand::productId)
37+
.toList();
38+
39+
List<Product> products = productService.getProductsByIds(productIds);
40+
Map<Long, Product> productMap = products.stream()
41+
.collect(Collectors.toMap(Product::getId, Function.identity()));
42+
43+
validateAndDecreaseStock(command.items(), productMap);
44+
45+
Order order = Order.create(user);
46+
47+
for (OrderPlaceCommand.OrderItemCommand item : command.items()) {
48+
Product product = productMap.get(item.productId());
49+
order.addOrderItem(product, item.quantity());
50+
}
51+
52+
Long totalAmount = order.getTotalAmountValue();
53+
pointService.usePoint(user.getUserIdValue(), totalAmount);
54+
55+
order.completePayment();
56+
57+
Order savedOrder = orderService.save(order);
58+
59+
return OrderInfo.from(savedOrder);
60+
}
61+
62+
private void validateAndDecreaseStock(
63+
List<OrderPlaceCommand.OrderItemCommand> items,
64+
Map<Long, Product> productMap
65+
) {
66+
for (OrderPlaceCommand.OrderItemCommand item : items) {
67+
Product product = productMap.get(item.productId());
68+
69+
if (product == null) {
70+
throw new CoreException(ErrorType.NOT_FOUND, "상품을 찾을 수 없습니다.");
71+
}
72+
73+
if (!product.isStockAvailable(item.quantity())) {
74+
throw new CoreException(ErrorType.BAD_REQUEST,
75+
String.format("상품 '%s'의 재고가 부족합니다.", product.getName()));
76+
}
77+
78+
product.decreaseStock(item.quantity());
79+
}
80+
}
81+
82+
public List<OrderInfo> getMyOrders(String userId) {
83+
User user = userService.getUserByUserId(userId);
84+
List<Order> orders = orderService.getOrdersByUser(user);
85+
86+
return orders.stream()
87+
.map(OrderInfo::from)
88+
.toList();
89+
}
90+
91+
public OrderInfo getOrderDetail(Long orderId, String userId) {
92+
User user = userService.getUserByUserId(userId);
93+
Order order = orderService.getOrderByIdAndUser(orderId, user);
94+
95+
return OrderInfo.from(order);
96+
}
97+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.loopers.application.order;
2+
3+
import com.loopers.domain.order.Order;
4+
import com.loopers.domain.order.OrderStatus;
5+
import com.loopers.domain.order.OrderItem;
6+
7+
import java.time.ZonedDateTime;
8+
import java.util.List;
9+
10+
public record OrderInfo(
11+
Long orderId,
12+
String userId,
13+
Long totalAmount,
14+
OrderStatus status,
15+
ZonedDateTime paidAt,
16+
List<OrderItemInfo> items
17+
) {
18+
public record OrderItemInfo(
19+
Long productId,
20+
String productName,
21+
Integer quantity,
22+
Long unitPrice,
23+
Long totalPrice
24+
) {
25+
public static OrderItemInfo from(OrderItem item) {
26+
return new OrderItemInfo(
27+
item.getProduct().getId(),
28+
item.getProduct().getName(),
29+
item.getQuantity(),
30+
item.getUnitPriceValue(),
31+
item.calculateAmount().getValue()
32+
);
33+
}
34+
}
35+
36+
public static OrderInfo from(Order order) {
37+
return new OrderInfo(
38+
order.getId(),
39+
order.getUser().getUserIdValue(),
40+
order.getTotalAmountValue(),
41+
order.getStatus(),
42+
order.getPaidAt(),
43+
order.getOrderItems().stream()
44+
.map(OrderItemInfo::from)
45+
.toList()
46+
);
47+
}
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.loopers.application.order;
2+
3+
import java.util.List;
4+
5+
public record OrderPlaceCommand(
6+
String userId,
7+
List<OrderItemCommand> items
8+
) {
9+
public record OrderItemCommand(
10+
Long productId,
11+
Integer quantity
12+
) {}
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.loopers.application.point;
2+
3+
public record PointCommand(
4+
String userId,
5+
Long amount
6+
) {
7+
}

0 commit comments

Comments
 (0)