Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2/27 배포 #233

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading