Skip to content

Commit

Permalink
Feat: 주문번호 증가 시퀀스 테이블로 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
marinesnow34 committed Mar 26, 2024
1 parent 2d84c83 commit 3305c1b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public class OrderNumberSequence {
@MapsId
@JoinColumn(name = "id") // Order 테이블과 조인하는 컬럼 지정
private Store store;

public void increaseOrderNumber() {
this.orderNumber++;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.readyvery.readyverydemo.domain.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;

import com.readyvery.readyverydemo.domain.OrderNumberSequence;

import jakarta.persistence.LockModeType;

public interface OrderNumberSequenceRepository extends JpaRepository<OrderNumberSequence, Long> {
@Lock(LockModeType.PESSIMISTIC_WRITE)
Optional<OrderNumberSequence> findOrderNumberByStoreId(Long storeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public enum ExceptionCode {
INVALID_INPUT(400, "Invalid input."),
UNAUTHORIZED(400, "Already User"),
AUTH_ERROR(403, "Auth Error"),
POINT_NOT_FOUND(403, "Point not found");
POINT_NOT_FOUND(403, "Point not found"),
ORDER_NUMBER_NOT_FOUND(403, "Order number not found");

private int status;
private String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.readyvery.readyverydemo.domain.FoodieOption;
import com.readyvery.readyverydemo.domain.FoodieOptionCategory;
import com.readyvery.readyverydemo.domain.Order;
import com.readyvery.readyverydemo.domain.OrderNumberSequence;
import com.readyvery.readyverydemo.domain.Point;
import com.readyvery.readyverydemo.domain.Progress;
import com.readyvery.readyverydemo.domain.Receipt;
Expand All @@ -41,6 +42,7 @@
import com.readyvery.readyverydemo.domain.repository.CouponRepository;
import com.readyvery.readyverydemo.domain.repository.FoodieOptionRepository;
import com.readyvery.readyverydemo.domain.repository.FoodieRepository;
import com.readyvery.readyverydemo.domain.repository.OrderNumberSequenceRepository;
import com.readyvery.readyverydemo.domain.repository.OrdersRepository;
import com.readyvery.readyverydemo.domain.repository.PointRepository;
import com.readyvery.readyverydemo.domain.repository.ReceiptRepository;
Expand Down Expand Up @@ -94,6 +96,7 @@ public class OrderServiceImpl implements OrderService {
private final CouponRepository couponRepository;
private final PointRepository pointRepository;
private final PointServiceFacade pointServiceFacade;
private final OrderNumberSequenceRepository orderNumberSequenceRepository;

@Override
public FoodyDetailRes getFoody(Long storeId, Long foodyId, Long inout) {
Expand Down Expand Up @@ -566,14 +569,12 @@ private void applyTosspaymentDto(Order order, TosspaymentDto tosspaymentDto) {
}
}

private synchronized String getOrderNumber(Order order) {
long todayOrder = ordersRepository.countByCreatedAtBetweenAndProgressNotAndStore(
order.getCreatedAt().toLocalDate().atStartOfDay(),
order.getCreatedAt().toLocalDate().atTime(23, 59, 59),
Progress.REQUEST,
order.getStore()
) + 1;
return Long.toString(todayOrder);
private String getOrderNumber(Order order) {
OrderNumberSequence sequence = orderNumberSequenceRepository.findOrderNumberByStoreId(order.getStore().getId())
.orElseThrow(() -> new BusinessLogicException(ExceptionCode.ORDER_NUMBER_NOT_FOUND));
sequence.increaseOrderNumber();
orderNumberSequenceRepository.save(sequence);
return Long.toString(sequence.getOrderNumber());
}

private void verifyOrder(Order order, Long amount) {
Expand Down

0 comments on commit 3305c1b

Please sign in to comment.