Skip to content

Commit

Permalink
refactor : 주문 도메인 데이터 추가 (#37)
Browse files Browse the repository at this point in the history
- 총 도시락 배달 갯수
- 남은 도시락 배달 갯수
  • Loading branch information
JiwonKKang authored Aug 21, 2024
1 parent 0031ec7 commit 24f906d
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,15 @@ public Response<MealDeliveryResponse> nextDeliveryMeal(@RequestParam Long orderI
@PatchMapping("/{mealDeliveryId}/{deliveryState}")
public Response<Void> changeDeliveryState(
@PathVariable Long mealDeliveryId, @PathVariable DeliveryState deliveryState) {
mealDeliveryService.changeDeliveryState(
TargetMealDelivery.from(mealDeliveryId), deliveryState);

switch (deliveryState) {
case DELIVERING ->
mealDeliveryService.startMealDelivery(TargetMealDelivery.from(mealDeliveryId));
case DELIVERED ->
mealDeliveryService.completeMealDelivery(
TargetMealDelivery.from(mealDeliveryId));
}

return Response.success("배송 상태 변경 성공");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import core.startup.mealtoktok.domain.order.OrderDetail;
import core.startup.mealtoktok.domain.order.OrderSearchCond;
import core.startup.mealtoktok.domain.order.OrderService;
import core.startup.mealtoktok.domain.order.OrderState;
import core.startup.mealtoktok.domain.order.Orderer;
import core.startup.mealtoktok.domain.order.TargetOrder;
import core.startup.mealtoktok.domain.user.User;
Expand Down Expand Up @@ -55,9 +56,9 @@ public Response<OrderDetailResponse> orderDetail(
}

@GetMapping("/{orderId}/state")
public Response<OrderResponse> orderState(
public Response<OrderState> orderState(
@AuthenticationPrincipal User currentUser, @PathVariable Long orderId) {
orderService.getOrderState(Orderer.from(currentUser), TargetOrder.from(orderId));
return null;
return Response.success(
orderService.getOrderState(Orderer.from(currentUser), TargetOrder.from(orderId)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import core.startup.mealtoktok.common.dto.Response;
import core.startup.mealtoktok.common.dto.SliceResult;
import core.startup.mealtoktok.domain.order.OrderSearchCond;
import core.startup.mealtoktok.domain.order.OrderState;
import core.startup.mealtoktok.domain.order.TargetOrder;
import core.startup.mealtoktok.domain.user.User;

Expand All @@ -27,5 +28,5 @@ Response<SliceResult<OrderResponse>> searchOrders(
Response<OrderDetailResponse> orderDetail(User currentUser, Long orderId);

@Operation(summary = "주문 상태 조회")
Response<OrderResponse> orderState(User currentUser, Long orderId);
Response<OrderState> orderState(User currentUser, Long orderId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public record OrderResponse(
String specialInstruction,
Orderer orderer,
OrderPrice orderPrice,
Integer totalMealDeliveryCount,
Integer remainingMealDeliveryCount,
LocalDateTime orderTime) {

public static OrderResponse from(Order order) {
Expand All @@ -25,6 +27,8 @@ public static OrderResponse from(Order order) {
order.getSpecialInstruction(),
order.getOrderer(),
order.getOrderPrice(),
order.getTotalMealDeliveryCount(),
order.getRemainingMealDeliveryCount(),
order.getOrderTime());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package core.startup.mealtoktok.domain.mealdelivery;

public interface MealDeliveryCountManager {

void decrease(Long orderId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class MealDeliveryService {
private final MealDeliveryReader mealDeliveryReader;
private final MealDeliveryUpdater mealDeliveryUpdater;
private final FullDiningManager fullDiningManager;
private final MealDeliveryCountManager mealDeliveryCountManager;

public MealDelivery getDeliveringMeal(Recipient recipient) {
return mealDeliveryReader.read(recipient, DeliveryState.DELIVERING);
Expand All @@ -34,11 +35,17 @@ public MealDelivery getMealDelivery(TargetMealDelivery targetMealDelivery) {
return mealDeliveryReader.read(targetMealDelivery);
}

public void changeDeliveryState(
TargetMealDelivery targetMealDelivery, DeliveryState deliveryState) {
public void startMealDelivery(TargetMealDelivery targetMealDelivery) {
MealDelivery mealDelivery = mealDeliveryReader.read(targetMealDelivery);
mealDeliveryUpdater.changeDeliveryState(mealDelivery, deliveryState);
// TODO :알림 발송 alarmSender.send(orderer, deliveryState);
mealDeliveryUpdater.changeDeliveryState(mealDelivery, DeliveryState.DELIVERING);
// TODO :알림 발송 alarmSender.send(orderer, DeliveryState.DELIVERING);
}

public void completeMealDelivery(TargetMealDelivery targetMealDelivery) {
MealDelivery mealDelivery = mealDeliveryReader.read(targetMealDelivery);
mealDeliveryUpdater.changeDeliveryState(mealDelivery, DeliveryState.DELIVERED);
mealDeliveryCountManager.decrease(mealDelivery.getOrderId());
// TODO :알림 발송 alarmSender.send(orderer, DeliveryState.DELIVERED);
}

public void changeCollectingState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public enum MealDeliveryErrorCode implements BaseErrorCode {

@Override
public ErrorReason getErrorReason() {
return null;
return ErrorReason.of(status, errorCode, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import core.startup.mealtoktok.domain.order.exception.OrderDomainException;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
Expand All @@ -20,23 +22,63 @@ public class Order {
private String specialInstruction;
private Orderer orderer;
private OrderPrice orderPrice;
private Integer totalMealDeliveryCount;
private Integer remainingMealDeliveryCount;
private LocalDateTime orderTime;

public static Order create(
Orderer orderer,
OrderType orderType,
String specialInstruction,
OrderPrice orderPrice) {
OrderPrice orderPrice,
Integer mealDeliverySize) {
return Order.builder()
.orderType(orderType)
.orderState(OrderState.PENDING)
.specialInstruction(specialInstruction)
.orderer(orderer)
.orderPrice(orderPrice)
.totalMealDeliveryCount(mealDeliverySize)
.remainingMealDeliveryCount(mealDeliverySize)
.build();
}

public void cancelPayment() {
if (this.orderState != OrderState.PENDING) {
throw new OrderDomainException("결제가 완료된 주문은 취소할 수 없습니다.");
}
this.orderState = OrderState.PAYMENT_CANCELED;
}

public void completePayment() {
this.orderState = OrderState.PAYMENT_COMPLETED;
}

public void acceptOrder() {
if (this.orderState != OrderState.PAYMENT_COMPLETED) {
throw new OrderDomainException("결제가 완료된 주문만 접수할 수 있습니다.");
}

this.orderState = OrderState.ORDER_ACCEPTED;
}

public void completeDelivery() {
if (this.orderState != OrderState.PAYMENT_COMPLETED) {
throw new OrderDomainException("주문이 완료된 뒤에 배달을 완료할 수 있습니다.");
}

this.orderState = OrderState.DELIVERY_COMPLETED;
}

public boolean isOrderBy(Orderer orderer) {
return this.orderer.isSameUser(orderer);
}

public void completeOneDelivery() {
this.remainingMealDeliveryCount--;

if (this.remainingMealDeliveryCount == 0) {
completeDelivery();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public TargetOrder append(Orderer orderer, OrderContent orderContent) {
orderer,
orderContent.orderType(),
orderContent.specialInstruction(),
orderContent.orderPrice());
orderContent.orderPrice(),
orderContent.orderedMeals().size());
return orderRepository.save(order);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface OrderRepository {
Order find(TargetOrder targetOrder);

SliceResult<Order> findByCondition(Orderer orderer, OrderSearchCond cond, Cursor cursor);

void update(Order order);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public enum OrderState {
PENDING,
PAYMENT_COMPLETED,
ORDER_ACCEPTED,
PAYMENT_CANCELED,
DELIVERY_COMPLETED,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.startup.mealtoktok.domain.order;

import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;

import core.startup.mealtoktok.domain.mealdelivery.MealDeliveryCountManager;

@Component
@RequiredArgsConstructor
public class OrderUpdater implements MealDeliveryCountManager {

private final OrderRepository orderRepository;

@Override
public void decrease(Long orderId) {
Order order = orderRepository.find(TargetOrder.from(orderId));
order.completeOneDelivery();
orderRepository.update(order);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.startup.mealtoktok.domain.order.exception;

import core.startup.mealtoktok.common.exception.DomainException;

public class OrderDomainException extends DomainException {

public OrderDomainException(String message) {
super(OrderErrorCode.ORDER_DOMAIN_ERROR.setMessage(message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,33 @@
import static core.startup.mealtoktok.common.consts.MealTokTokConstant.BAD_REQUEST;
import static core.startup.mealtoktok.common.consts.MealTokTokConstant.NOT_FOUND;

import lombok.RequiredArgsConstructor;
import lombok.AllArgsConstructor;

import core.startup.mealtoktok.common.exception.BaseErrorCode;
import core.startup.mealtoktok.common.exception.ErrorReason;

@RequiredArgsConstructor
@AllArgsConstructor
public enum OrderErrorCode implements BaseErrorCode {
ORDER_NOT_FOUND(NOT_FOUND, "ORDER_404_1", "주문 정보를 찾지 못했습니다."),
ORDER_DOMAIN_ERROR(BAD_REQUEST, "ORDER_400_1"),
ORDERER_NOT_MATCH(BAD_REQUEST, "ORDER_400_1", "주문자가 일치하지 않습니다.");

private final Integer status;
private final String errorCode;
private final String message;
private String message;

OrderErrorCode(Integer status, String errorCode) {
this.status = status;
this.errorCode = errorCode;
}

@Override
public ErrorReason getErrorReason() {
return ErrorReason.of(status, errorCode, message);
}

public BaseErrorCode setMessage(String message) {
this.message = message;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,19 @@ public class OrderEntity extends BaseTimeEntity {

@Embedded private OrderPriceVO orderPrice;

private Integer totalMealDeliveryCount;

private Integer remainingMealDeliveryCount;

public static OrderEntity from(Order order) {
return OrderEntity.builder()
.orderType(order.getOrderType())
.orderState(order.getOrderState())
.specialInstruction(order.getSpecialInstruction())
.orderer(OrdererVO.from(order.getOrderer()))
.orderPrice(OrderPriceVO.from(order.getOrderPrice()))
.totalMealDeliveryCount(order.getTotalMealDeliveryCount())
.remainingMealDeliveryCount(order.getRemainingMealDeliveryCount())
.createdAt(order.getOrderTime())
.build();
}
Expand All @@ -63,7 +69,14 @@ public Order toDomain() {
.orderState(orderState)
.orderer(orderer.toDomain())
.orderPrice(orderPrice.toDomain())
.totalMealDeliveryCount(totalMealDeliveryCount)
.remainingMealDeliveryCount(remainingMealDeliveryCount)
.orderTime(createdAt)
.build();
}

public void update(Order order) {
this.orderState = order.getOrderState();
this.remainingMealDeliveryCount = order.getRemainingMealDeliveryCount();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ public SliceResult<Order> findByCondition(
return SliceResult.of(
orderEntities.map(OrderEntity::toDomain).toList(), orderEntities.hasNext());
}

@Override
public void update(Order order) {
OrderEntity orderEntity =
orderJpaRepository
.findById(order.getOrderId())
.orElseThrow(() -> OrderNotFoundException.EXCEPTION);
orderEntity.update(order);
}
}

0 comments on commit 24f906d

Please sign in to comment.