Skip to content

Commit 457c03b

Browse files
authored
Merge pull request #2 from sieun0322/feature/week3-domain-modeling
[volume-3] 도메인 모델링 및 구현
2 parents b3b0ca4 + 9bbe684 commit 457c03b

77 files changed

Lines changed: 2953 additions & 160 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.Brand;
4+
import com.loopers.domain.brand.BrandService;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.stereotype.Component;
7+
8+
@RequiredArgsConstructor
9+
@Component
10+
public class BrandFacade {
11+
private final BrandService brandService;
12+
13+
public BrandInfo getBrandDetail(long id) {
14+
Brand brand = brandService.getBrand(id);
15+
return BrandInfo.from(brand);
16+
}
17+
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.loopers.application.brand;
2+
3+
import com.loopers.domain.brand.Brand;
4+
import com.loopers.support.error.CoreException;
5+
import com.loopers.support.error.ErrorType;
6+
7+
public record BrandInfo(Long id, String name, String story) {
8+
public static BrandInfo from(Brand model) {
9+
if (model == null) throw new CoreException(ErrorType.NOT_FOUND, "유저정보를 찾을수 없습니다.");
10+
return new BrandInfo(
11+
model.getId(),
12+
model.getName(),
13+
model.getStory()
14+
);
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.loopers.application.like;
2+
3+
public record LikeInfo(long likeCount, boolean isLiked) {
4+
public static LikeInfo from(long likeCount, boolean isLiked) {
5+
return new LikeInfo(
6+
likeCount,
7+
isLiked
8+
);
9+
}
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.loopers.application.order;
2+
3+
import com.loopers.interfaces.api.order.OrderCreateV1Dto;
4+
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
8+
public record CreateOrderCommand(Long userId, Map<Long, Long> orderItemInfo) {
9+
public record ItemCommand(Long productId, Map<Long, Long> quantity) {
10+
}
11+
12+
public static CreateOrderCommand from(Long userId, OrderCreateV1Dto.OrderRequest request) {
13+
return new CreateOrderCommand(
14+
userId,
15+
request.items().stream()
16+
.collect(Collectors.toMap(
17+
OrderCreateV1Dto.OrderItemRequest::productId,
18+
OrderCreateV1Dto.OrderItemRequest::quantity,
19+
Long::sum
20+
)));
21+
}
22+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.loopers.application.order;
2+
3+
import com.loopers.domain.order.CreateOrderService;
4+
import com.loopers.domain.order.Order;
5+
import com.loopers.domain.order.OrderService;
6+
import com.loopers.domain.point.PointService;
7+
import com.loopers.domain.product.Product;
8+
import com.loopers.domain.product.ProductService;
9+
import com.loopers.domain.product.ProductStockService;
10+
import com.loopers.domain.user.User;
11+
import com.loopers.domain.user.UserPointService;
12+
import com.loopers.domain.user.UserService;
13+
import lombok.RequiredArgsConstructor;
14+
import org.springframework.data.domain.Page;
15+
import org.springframework.stereotype.Component;
16+
import org.springframework.transaction.annotation.Transactional;
17+
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
@RequiredArgsConstructor
22+
@Component
23+
public class OrderFacade {
24+
private final UserService userService;
25+
private final PointService pointService;
26+
private final ProductService productService;
27+
private final OrderService orderService;
28+
private final CreateOrderService createOrderService;
29+
30+
private final ProductStockService productStockService;
31+
private final UserPointService userPointService;
32+
33+
@Transactional(readOnly = true)
34+
public Page<Order> getOrderList(Long userId,
35+
String sortType,
36+
int page,
37+
int size) {
38+
return orderService.getOrders(userId, sortType, page, size);
39+
}
40+
41+
@Transactional(readOnly = true)
42+
public OrderInfo getOrderDetail(Long orderId) {
43+
Order order = orderService.getOrder(orderId);
44+
return OrderInfo.from(order);
45+
}
46+
47+
@Transactional
48+
public OrderInfo createOrder(CreateOrderCommand command) {
49+
50+
Map<Long, Long> quantityMap = command.orderItemInfo();
51+
User user = userService.getActiveUser(command.userId());
52+
List<Product> productList = productService.getExistingProducts(quantityMap.keySet());
53+
54+
productStockService.deduct(productList, quantityMap);
55+
userPointService.use(user, productList, quantityMap);
56+
57+
Order savedOrder = createOrderService.save(user, productList, quantityMap);
58+
return OrderInfo.from(savedOrder);
59+
}
60+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.loopers.application.order;
2+
3+
import com.loopers.domain.order.Order;
4+
import com.loopers.support.error.CoreException;
5+
import com.loopers.support.error.ErrorType;
6+
7+
import java.math.BigDecimal;
8+
import java.time.ZonedDateTime;
9+
import java.util.List;
10+
11+
public record OrderInfo(long id, String status, BigDecimal totalPrice
12+
, ZonedDateTime orderAt, List<OrderItemInfo> orderItemInfo) {
13+
public static OrderInfo from(Order model) {
14+
if (model == null) throw new CoreException(ErrorType.NOT_FOUND, "주문정보를 찾을수 없습니다.");
15+
return new OrderInfo(
16+
model.getId(),
17+
model.getStatus().name(),
18+
model.getTotalPrice(),
19+
model.getOrderAt(),
20+
OrderItemInfo.from(model.getOrderItems())
21+
);
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.loopers.application.order;
2+
3+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
4+
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
5+
import com.loopers.domain.order.OrderItem;
6+
import com.loopers.support.error.CoreException;
7+
import com.loopers.support.error.ErrorType;
8+
9+
import java.math.BigDecimal;
10+
import java.util.List;
11+
12+
public record OrderItemInfo(Long id, long quantity
13+
, @JsonSerialize(using = ToStringSerializer.class) BigDecimal unitPrice,
14+
@JsonSerialize(using = ToStringSerializer.class) BigDecimal totalPrice) {
15+
public static List<OrderItemInfo> from(List<OrderItem> model) {
16+
if (model == null) throw new CoreException(ErrorType.NOT_FOUND, "주문상세정보를 찾을수 없습니다.");
17+
return model.stream().map(item -> new OrderItemInfo(
18+
item.getId(),
19+
item.getQuantity(),
20+
item.getUnitPrice(),
21+
item.getTotalPrice()
22+
)).toList();
23+
}
24+
}

apps/commerce-api/src/main/java/com/loopers/application/point/PointFacade.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.loopers.application.point;
22

33
import com.loopers.domain.point.PointService;
4-
import com.loopers.domain.user.UserModel;
4+
import com.loopers.domain.user.User;
55
import com.loopers.domain.user.UserService;
6-
import com.loopers.support.error.CoreException;
7-
import com.loopers.support.error.ErrorType;
86
import lombok.RequiredArgsConstructor;
97
import org.springframework.stereotype.Component;
108

@@ -16,14 +14,13 @@ public class PointFacade {
1614
private final UserService userService;
1715
private final PointService pointService;
1816

19-
public BigDecimal getPoint(String userId) {
20-
UserModel user = userService.getUser(userId);
21-
return pointService.getAmount(user.getUserId());
17+
public BigDecimal getPoint(Long userId) {
18+
User user = userService.getActiveUser(userId);
19+
return user.getPoint().getAmount();
2220
}
2321

24-
public BigDecimal charge(String userId, BigDecimal chargeAmt) {
25-
UserModel user = userService.getUser(userId);
26-
if (user == null) throw new CoreException(ErrorType.NOT_FOUND, "유저정보를 찾을수 없습니다.");
22+
public BigDecimal charge(Long userId, BigDecimal chargeAmt) {
23+
User user = userService.getActiveUser(userId);
2724
return pointService.charge(user, chargeAmt);
2825
}
2926

apps/commerce-api/src/main/java/com/loopers/application/point/PointInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.loopers.application.point;
22

3-
import com.loopers.domain.point.PointModel;
3+
import com.loopers.domain.point.Point;
44

55
import java.math.BigDecimal;
66

77
public record PointInfo(String userId, BigDecimal amount) {
8-
public static PointInfo from(PointModel model) {
8+
public static PointInfo from(Point model) {
99
if (model == null) return null;
1010
return new PointInfo(
1111
model.getUser().getUserId(),
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.loopers.application.product;
2+
3+
import com.loopers.application.brand.BrandInfo;
4+
import com.loopers.application.like.LikeInfo;
5+
import com.loopers.domain.product.Product;
6+
import com.loopers.support.error.CoreException;
7+
import com.loopers.support.error.ErrorType;
8+
9+
import java.math.BigDecimal;
10+
11+
public record ProductDetailInfo(Long id, String name, BigDecimal price, long stock
12+
, BrandInfo brandInfo, LikeInfo likeInfo) {
13+
public static ProductDetailInfo from(Product model, boolean isLiked) {
14+
if (model == null) throw new CoreException(ErrorType.NOT_FOUND, "상품정보를 찾을수 없습니다.");
15+
return new ProductDetailInfo(
16+
model.getId(),
17+
model.getName(),
18+
model.getPrice(),
19+
model.getStock(),
20+
BrandInfo.from(model.getBrand()),
21+
LikeInfo.from(model.getLikeCount(), isLiked)
22+
);
23+
}
24+
}

0 commit comments

Comments
 (0)