Skip to content

Commit

Permalink
Merge pull request #232 from marinesnow34/test
Browse files Browse the repository at this point in the history
�Feat: 주문 번호 중복 문제 시퀀스 테이블 사용
  • Loading branch information
marinesnow34 authored Mar 26, 2024
2 parents aa09623 + 3305c1b commit e8147f1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.readyvery.readyverydemo.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.MapsId;
import jakarta.persistence.OneToOne;
import lombok.Getter;

@Getter
@Entity
public class OrderNumberSequence {
@Id
private Long id;

@Column(name = "order_number")
private Long orderNumber;

@OneToOne
@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 e8147f1

Please sign in to comment.