Skip to content

Commit

Permalink
Merge pull request #444 from TrandPick/refactor/443_refactoringOrder
Browse files Browse the repository at this point in the history
Refactor/443 refactoring order
  • Loading branch information
jooooonj authored Sep 19, 2023
2 parents fe9e10a + 678e6f2 commit 5130e78
Show file tree
Hide file tree
Showing 49 changed files with 617 additions and 542 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/work-2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: application-secret.yml 생성
- name: application-secret.yml 생성한다.
env:
ACTIONS_STEP_DEBUG: true
APPLICATION_SECRET: ${{ secrets.APPLICATION_SECRET_YML }}
Expand Down
13 changes: 0 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
# Stage 1: Pinpoint setup
# FROM alpine:3.14 as pinpoint-setup
# WORKDIR /usr/local
# ADD https://github.com/pinpoint-apm/pinpoint/releases/download/v2.5.2/pinpoint-agent-2.5.2.tar.gz /usr/local
# RUN tar -zxvf pinpoint-agent-2.5.2.tar.gz && \
# sed -i 's/profiler.transport.grpc.collector.ip=127.0.0.1/profiler.transport.grpc.collector.ip=10.41.183.156/g' pinpoint-agent-2.5.2/pinpoint-root.config && \
# sed -i 's/profiler.collector.ip=127.0.0.1/profiler.collector.ip=10.41.183.156/g' pinpoint-agent-2.5.2/pinpoint-root.config

# Stage 2: Build final image
FROM openjdk:17-jdk-alpine
WORKDIR /app
# COPY --from=pinpoint-setup /usr/local/pinpoint-agent-2.5.2/ ./pinpoint-agent
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar", \
# "-javaagent:/app/pinpoint-agent/pinpoint-bootstrap-2.5.2.jar", \
# "-Dpinpoint.applicationName=TrendPick", \
# "-Dpinpoint.config=/app/pinpoint-agent/pinpoint-root.config", \
"-Dspring.profiles.active=prod", \
"/app/app.jar"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 👖 TrendPick, 취향에 맞게 상품을 추천 해주는 서비스
## 👖 TrendPick, 취향에 맞게 상품을 추천 해주는 서비스
<div align="center">
<p align="center">
<img src="https://github.com/TrandPick/TrendPick_Pro/assets/62290451/0ecc6545-4151-472c-9121-f531a4de69ae" width="700" height="400"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.util.UriComponentsBuilder;
import project.trendpick_pro.global.kafka.view.service.ViewService;

@Slf4j
@Controller
@RequiredArgsConstructor
public class CommonController {

private final ViewService viewService;

@GetMapping("/")
public String homePage(HttpSession session) {
viewService.requestIncrementViewCount(session);
String mainCategory = "전체";
return "redirect:" + UriComponentsBuilder
.fromPath("/trendpick/products/list")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import project.trendpick_pro.domain.coupon.entity.dto.response.CouponCardByApplyResponse;
import project.trendpick_pro.domain.coupon.service.CouponCardService;
import project.trendpick_pro.global.util.rsData.RsData;

import java.time.LocalDateTime;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,15 @@ && validateStatus()
&& validateMinimumPurchaseAmount(orderItem.getOrderPrice());
}

public void use(OrderItem orderItem, LocalDateTime dateTime) {
settingStatusAndDate(dateTime);
orderItem.applyCouponCard(this);
public void use(LocalDateTime dateTime) {
this.status = CouponStatus.USED;
this.usedDate = dateTime;
}

public void cancel(OrderItem orderItem){
this.status = CouponStatus.AVAILABLE;
this.usedDate = null;
orderItem.cancelCouponCard();
this.coupon.decreaseIssueCount();
}

private void settingStatusAndDate(LocalDateTime dateTime) {
this.status = CouponStatus.USED;
this.usedDate = dateTime;
}

private boolean validateMinimumPurchaseAmount(int price){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@
@RequiredArgsConstructor
@Service
public class CouponCardServiceImpl implements CouponCardService {

private final CouponCardRepository couponCardRepository;
private final CouponRepository couponRepository;
private final OrderItemRepository orderItemRepository;

@Transactional
@Override
public RsData issue(Member member, Long couponId, LocalDateTime dateTime) {
Coupon coupon = couponRepository.findById(couponId).orElseThrow(
() -> new CouponNotFoundException("존재하지 않는 쿠폰입니다."));
Coupon coupon = findCoupon(couponId);
int count = couponCardRepository.countByCouponIdAndMemberId(couponId, member.getId());

RsData<CouponCard> validateResult = validateCouponCard(count, coupon, dateTime);
Expand All @@ -43,32 +41,36 @@ public RsData issue(Member member, Long couponId, LocalDateTime dateTime) {
return RsData.of("S-1", coupon.getName() + " 쿠폰이 발급되었습니다.");
}


@Transactional
@Override
public RsData apply(Long couponCardId, Long orderItemId, LocalDateTime dateTime) {
OrderItem orderItem = orderItemRepository.findById(orderItemId).orElseThrow(
() -> new OrderItemNotFoundException("주문되지 않은 상품입니다."));
CouponCard couponCard = couponCardRepository.findById(couponCardId).orElseThrow(
() -> new CouponNotFoundException("존재하지 않는 쿠폰입니다."));
OrderItem orderItem = findOrderItem(orderItemId);
CouponCard couponCard = findCouponCard(couponCardId);

if (!couponCard.validate(orderItem, dateTime))
return RsData.of("F-1", "해당 주문상품에 해당 쿠폰을 적용할 수 없습니다.");
couponCard.use(orderItem, dateTime);
orderItem.applyCouponCard(couponCard);
return RsData.of("S-1", "쿠폰이 적용되었습니다.");
}

private CouponCard findCouponCard(Long couponCardId) {
CouponCard couponCard = couponCardRepository.findById(couponCardId).orElseThrow(
() -> new CouponNotFoundException("존재하지 않는 쿠폰입니다."));
return couponCard;
}

@Transactional
@Override
public RsData cancel(Long orderItemId) {
OrderItem orderItem = orderItemRepository.findById(orderItemId).orElseThrow(
() -> new OrderItemNotFoundException("주문되지 않은 상품입니다."));
OrderItem orderItem = findOrderItem(orderItemId);
orderItem.getCouponCard().cancel(orderItem);
return RsData.of("S-1", "쿠폰이 취소되었습니다.");
}

@Override
public List<CouponCardByApplyResponse> showCouponCardsByOrderItem(Long orderItemId) {
OrderItem orderItem = orderItemRepository.findById(orderItemId).orElseThrow(
() -> new OrderItemNotFoundException("주문되지 않은 상품입니다."));
OrderItem orderItem = findOrderItem(orderItemId);
List<CouponCard> couponCards = couponCardRepository.findAllByBrand(orderItem.getProduct().getProductOption().getBrand().getName());
return createCouponCardByApplyResponseList(couponCards, orderItem);
}
Expand All @@ -81,7 +83,7 @@ private CouponCard settingCouponCard(Member member, LocalDateTime dateTime, Coup
return savedCouponCard;
}

private static RsData<CouponCard> validateCouponCard(int count, Coupon coupon, LocalDateTime dateTime) {
private RsData<CouponCard> validateCouponCard(int count, Coupon coupon, LocalDateTime dateTime) {
if(count > 0)
return RsData.of("F-3", "이미 발급 받으신 쿠폰입니다.");
if(!coupon.validateLimitCount())
Expand All @@ -97,5 +99,15 @@ private List<CouponCardByApplyResponse> createCouponCardByApplyResponseList(List
.map(couponCard -> CouponCardByApplyResponse.of(couponCard, orderItem))
.toList();
}

private OrderItem findOrderItem(Long orderItemId) {
return orderItemRepository.findById(orderItemId).orElseThrow(
() -> new OrderItemNotFoundException("주문되지 않은 상품입니다."));
}

private Coupon findCoupon(Long couponId) {
return couponRepository.findById(couponId).orElseThrow(
() -> new CouponNotFoundException("존재하지 않는 쿠폰입니다."));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import project.trendpick_pro.domain.member.entity.Member;
import project.trendpick_pro.domain.orders.entity.Order;
import project.trendpick_pro.domain.orders.entity.dto.request.CartToOrderRequest;
import project.trendpick_pro.domain.orders.entity.dto.request.ProductOrderRequest;
import project.trendpick_pro.domain.orders.entity.dto.response.OrderDetailResponse;
Expand All @@ -28,7 +29,10 @@ public class OrderController {
@PreAuthorize("hasAuthority({'MEMBER'})")
@GetMapping("{orderId}/form")
public String showOrderForm(@PathVariable("orderId") Long orderId, Model model){
model.addAttribute("order", orderService.findById(orderId));
Order order = orderService.findById(orderId);
if(order.getPaymentKey() != null || order.getOrderState().equals("주문취소"))
return rq.redirectWithMsg("/", "이미 처리된 주문입니다.");
model.addAttribute("order", order);
return "trendpick/orders/order-form";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class Order extends BaseTimeEntity {
@Enumerated(EnumType.STRING)
private OrderStatus orderStatus;

@Column(name = "total_price", nullable = false)
@Column(name = "total_price")
private int totalPrice = 0;

public void connectUser(Member member) {
Expand All @@ -49,7 +49,7 @@ public void connectUser(Member member) {
public void connectPayment(String paymentMethod, String paymentKey) {
this.paymentMethod = paymentMethod;
this.paymentKey = paymentKey;
productDiscount();
setDiscountAmount();
}

public void connectDelivery(Delivery delivery) {
Expand All @@ -66,6 +66,15 @@ public static Order createOrder(Member member, Delivery delivery, List<OrderItem
return order;
}

public static Order createTempOrder(Member member, Delivery delivery){
Order order = new Order();
order.connectUser(member);
order.connectDelivery(delivery);
order.orderStatus = OrderStatus.TEMP;
order.paymentMethod = "";
return order;
}

public void updateStatus(OrderStatus status) {
this.orderStatus = status;
}
Expand All @@ -81,9 +90,8 @@ public int getTotalDiscountedPrice(){
public void cancel() {
this.orderStatus = OrderStatus.CANCELED;
this.delivery.canceledDelivery();
for (OrderItem orderItem : this.orderItems) {
for (OrderItem orderItem : this.orderItems)
orderItem.cancel();
}
}

public void cancelTemp() {
Expand Down Expand Up @@ -112,13 +120,13 @@ public String getDeliveryState(){
};
}

private void productDiscount(){
private void setDiscountAmount(){ //할인 금액 적용
for (OrderItem orderItem : getOrderItems()) {
this.totalPrice -= orderItem.getDiscountPrice();
}
}

private void settingOrderItems(List<OrderItem> orderItems) {
public void settingOrderItems(List<OrderItem> orderItems) {
for (OrderItem orderItem : orderItems) {
this.addOrderItem(orderItem);
this.totalPrice += orderItem.getTotalPrice();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import project.trendpick_pro.domain.common.base.BaseTimeEntity;
import project.trendpick_pro.domain.coupon.entity.CouponCard;
import project.trendpick_pro.domain.product.entity.product.Product;
import project.trendpick_pro.domain.product.exception.ProductStockOutException;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down Expand Up @@ -69,21 +70,19 @@ public void connectOrder(Order order) {
public void applyCouponCard(CouponCard couponCard) {
this.couponCard = couponCard;
int discountPercent = couponCard.getCoupon().getDiscountPercent();
int price = getOrderPrice() * discountPercent / 100; //쿠폰은 상품 한 개 가격에서 할인 적용
discount(price);
this.discountPrice = getOrderPrice() * discountPercent / 100; //쿠폰은 상품 한 개 가격에서 할인 적용
this.totalPrice -= this.discountPrice;
}

public void cancelCouponCard(){
this.couponCard = null;
this.totalPrice += this.discountPrice;
this.discountPrice = 0;
}

private void discount(int price){
this.discountPrice += price;
this.totalPrice -= this.discountPrice;
}

private OrderItem(Product product, int quantity, String size, String color) {
if(product.getProductOption().getStock() < quantity)
throw new ProductStockOutException("상품의 재고가 부족하여 주문이 불가능합니다.");
this.product = product;
this.orderPrice = (product.getDiscountedPrice() > 0) ? product.getDiscountedPrice() : product.getProductOption().getPrice();
this.quantity = quantity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Locale;

@Getter
@Builder
public class OrderItemDto {
private Long productId;
private String productName;
Expand All @@ -17,17 +18,6 @@ public class OrderItemDto {
private int price;
private Long cartItemId;

@Builder
private OrderItemDto(Long productId, String productName, int quantity, String size, String color, int price, Long cartItemId) {
this.productId = productId;
this.productName = productName;
this.quantity = quantity;
this.size = size;
this.color = color;
this.price = price;
this.cartItemId = cartItemId;
}

public static OrderItemDto of(Product product, int quantity, String size, String color) {
return OrderItemDto.builder()
.productId(product.getId())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package project.trendpick_pro.domain.orders.repository;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import project.trendpick_pro.domain.orders.entity.Order;

import java.time.LocalDateTime;
import java.util.List;

public interface OrderRepository extends JpaRepository<Order, Long>, OrderRepositoryCustom {

@Query("select o from Order o " +
"where o.paymentKey IS NULL " +
"and o.createdDate <= :expireDate")
List<Order> findAllByPaymentKeyIsNullAndCreatedDateIsBefore(@Param("expireDate") LocalDateTime expireDate, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import project.trendpick_pro.domain.orders.entity.dto.request.CartToOrderRequest;
import project.trendpick_pro.domain.orders.entity.dto.response.OrderDetailResponse;
import project.trendpick_pro.domain.orders.entity.dto.response.OrderResponse;
import project.trendpick_pro.global.kafka.outbox.entity.OrderMaterial;
import project.trendpick_pro.global.util.rsData.RsData;

import java.time.LocalDate;
Expand All @@ -17,7 +18,7 @@
public interface OrderService {
RsData cartToOrder(Member member, CartToOrderRequest request);
RsData productToOrder(Member member, Long id, int quantity, String size, String color);
void tryOrder(String id) throws JsonProcessingException;
void tryOrder(String id, List<OrderMaterial> orderMaterials) throws JsonProcessingException;
RsData cancel(Long orderId);
void delete(Long id);
RsData<OrderDetailResponse> findOrderItems(Member member, Long orderId);
Expand Down
Loading

0 comments on commit 5130e78

Please sign in to comment.