Skip to content

Commit

Permalink
feat : mealDelivery 도메인 orderType 추가
Browse files Browse the repository at this point in the history
- 배송목록 조회시에 배송목록 한개당 orderType의 조회를 위해 APi 요청을 1개씩 또 보내야하는 이슈 -> 비정규화 진행
  • Loading branch information
JiwonKKang committed Aug 21, 2024
1 parent 24f906d commit 9d97b06
Show file tree
Hide file tree
Showing 23 changed files with 271 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public Response<Void> changeDeliveryState(
@PathVariable Long mealDeliveryId, @PathVariable DeliveryState deliveryState) {

switch (deliveryState) {
case DELIVERY_REQUESTED ->
mealDeliveryService.reserveMealDelivery(
TargetMealDelivery.from(mealDeliveryId));
case DELIVERING ->
mealDeliveryService.startMealDelivery(TargetMealDelivery.from(mealDeliveryId));
case DELIVERED ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import core.startup.mealtoktok.domain.mealdelivery.DeliveryDateTime;
import core.startup.mealtoktok.domain.mealdelivery.DeliveryState;
import core.startup.mealtoktok.domain.mealdelivery.MealDelivery;
import core.startup.mealtoktok.domain.mealdelivery.OrderTypeForDelivery;
import core.startup.mealtoktok.domain.mealdelivery.OrderedMeal;

import com.fasterxml.jackson.annotation.JsonInclude;
Expand All @@ -13,6 +14,7 @@
public record MealDeliveryResponse(
Long mealDeliveryId,
Long orderId,
OrderTypeForDelivery orderType,
OrderedMeal orderedMeal,
DeliveryState deliveryState,
DeliveryDateTime deliveryDateTime) {
Expand All @@ -21,6 +23,7 @@ public static MealDeliveryResponse from(MealDelivery mealDelivery) {
return new MealDeliveryResponse(
mealDelivery.getMealDeliveryId(),
mealDelivery.getOrderId(),
mealDelivery.getOrderType(),
mealDelivery.getOrderedMeal(),
mealDelivery.getDeliveryState(),
mealDelivery.getDeliveryDateTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,53 @@

import java.time.LocalDateTime;

public record DeliveryDateTime(
LocalDateTime deliveryStartTime, LocalDateTime deliveryCompleteTime) {
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class DeliveryDateTime {

private final LocalDateTime deliveryRequestTime;
private final LocalDateTime deliveryStartTime;
private final LocalDateTime deliveryCompleteTime;

public LocalDateTime getDeliveryRequestTime() {
return deliveryRequestTime;
}

public LocalDateTime getDeliveryStartTime() {
return deliveryStartTime;
}

public LocalDateTime getDeliveryCompleteTime() {
return deliveryCompleteTime;
}

public static DeliveryDateTime of(
LocalDateTime deliveryStartTime, LocalDateTime deliveryCompleteTime) {
return new DeliveryDateTime(deliveryStartTime, deliveryCompleteTime);
LocalDateTime deliveryReservedTime,
LocalDateTime deliveryStartTime,
LocalDateTime deliveryCompleteTime) {
return new DeliveryDateTime(deliveryReservedTime, deliveryStartTime, deliveryCompleteTime);
}

public static DeliveryDateTime deliveryRequest(DeliveryDateTime deliveryDateTime) {
return new DeliveryDateTime(LocalDateTime.now(), null, null);
}

public static DeliveryDateTime start(LocalDateTime deliveryStartTime) {
return new DeliveryDateTime(deliveryStartTime, null);
public static DeliveryDateTime start(DeliveryDateTime deliveryDateTime) {
return new DeliveryDateTime(
deliveryDateTime.getDeliveryRequestTime(), LocalDateTime.now(), null);
}

public static DeliveryDateTime complete(
LocalDateTime deliveryStartTime, LocalDateTime deliveryCompleteTime) {
return new DeliveryDateTime(deliveryStartTime, deliveryCompleteTime);
public static DeliveryDateTime complete(DeliveryDateTime deliveryDateTime) {
return new DeliveryDateTime(
deliveryDateTime.getDeliveryRequestTime(),
deliveryDateTime.getDeliveryStartTime(),
LocalDateTime.now());
}

public static DeliveryDateTime init() {
return new DeliveryDateTime(null, null);
return new DeliveryDateTime(null, null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public enum DeliveryState {
PENDING,
DELIVERY_REQUESTED,
DELIVERING,
DELIVERED
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,56 @@
package core.startup.mealtoktok.domain.mealdelivery;

import java.time.LocalDateTime;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

import core.startup.mealtoktok.domain.mealdelivery.exception.MealDeliveryDomainException;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
public class MealDelivery {

private Long mealDeliveryId;
private Long orderId;
private OrderTypeForDelivery orderType;
private OrderedMeal orderedMeal;
private DeliveryState deliveryState;
private DeliveryDateTime deliveryDateTime;

public void startDelivery(LocalDateTime startDateTime) {
this.deliveryDateTime = DeliveryDateTime.start(startDateTime);
public void deliveryRequest() {
this.deliveryDateTime = DeliveryDateTime.deliveryRequest(deliveryDateTime);
this.deliveryState = DeliveryState.DELIVERY_REQUESTED;
}

public void startDelivery() {
if (deliveryState != DeliveryState.DELIVERY_REQUESTED) {
throw new MealDeliveryDomainException("배송예약없이 배송을 시작할 수 없습니다.");
}

this.deliveryDateTime = DeliveryDateTime.start(deliveryDateTime);
this.deliveryState = DeliveryState.DELIVERING;
}

public void completeDelivery(LocalDateTime completeDateTime) {
this.deliveryDateTime =
DeliveryDateTime.complete(deliveryDateTime.deliveryStartTime(), completeDateTime);
public void completeDelivery() {
if (deliveryState != DeliveryState.DELIVERING) {
throw new MealDeliveryDomainException("배송중이 아닌 주문은 배송완료처리할 수 없습니다.");
}

this.deliveryDateTime = DeliveryDateTime.complete(deliveryDateTime);
this.deliveryState = DeliveryState.DELIVERED;
}

public static MealDelivery create(Long orderId, OrderedMeal orderedMeal) {
public static MealDelivery create(
Long orderId, OrderTypeForDelivery orderTypeForDelivery, OrderedMeal orderedMeal) {
return new MealDelivery(
null, orderId, orderedMeal, DeliveryState.PENDING, DeliveryDateTime.init());
null,
orderId,
orderTypeForDelivery,
orderedMeal,
DeliveryState.PENDING,
DeliveryDateTime.init());
}

public boolean hasFullDiningOption() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public MealDelivery getMealDelivery(TargetMealDelivery targetMealDelivery) {
return mealDeliveryReader.read(targetMealDelivery);
}

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

public void startMealDelivery(TargetMealDelivery targetMealDelivery) {
MealDelivery mealDelivery = mealDeliveryReader.read(targetMealDelivery);
mealDeliveryUpdater.changeDeliveryState(mealDelivery, DeliveryState.DELIVERING);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package core.startup.mealtoktok.domain.mealdelivery;

import java.time.LocalDateTime;

import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
Expand All @@ -14,8 +12,9 @@ public class MealDeliveryUpdater {

public void changeDeliveryState(MealDelivery mealDelivery, DeliveryState deliveryState) {
switch (deliveryState) {
case DELIVERING -> mealDelivery.startDelivery(LocalDateTime.now());
case DELIVERED -> mealDelivery.completeDelivery(LocalDateTime.now());
case DELIVERY_REQUESTED -> mealDelivery.deliveryRequest();
case DELIVERING -> mealDelivery.startDelivery();
case DELIVERED -> mealDelivery.completeDelivery();
}
mealDeliveryRepository.update(mealDelivery);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package core.startup.mealtoktok.domain.mealdelivery;

public enum OrderTypeForDelivery {
IMMEDIATE,
SCHEDULED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.startup.mealtoktok.domain.mealdelivery.exception;

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

public class MealDeliveryDomainException extends DomainException {

public MealDeliveryDomainException(String message) {
super(MealDeliveryErrorCode.MEAL_DELIVERY_DOMAIN_ERROR.setMessage(message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@

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 MealDeliveryErrorCode implements BaseErrorCode {
MEAL_DELIVERY_NOT_FOUND(NOT_FOUND, "MEAL_DELIVERY_404_1", "배송 정보를 찾지 못했습니다"),
NEXT_MEAL_DELIVERY_NOT_FOUND(NOT_FOUND, "MEAL_DELIVERY_404_2", "다음 배송 정보를 찾지 못했습니다"),
FULL_DINING_NOT_FOUND(NOT_FOUND, "FULL_DINING_404_1", "풀대접 정보를 찾지 못했습니다");
FULL_DINING_NOT_FOUND(NOT_FOUND, "FULL_DINING_404_1", "풀대접 정보를 찾지 못했습니다"),
MEAL_DELIVERY_DOMAIN_ERROR(500, "MEAL_DELIVERY_500_1");

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

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

private String message;

public BaseErrorCode setMessage(String message) {
this.message = message;
return this;
}

@Override
public ErrorReason getErrorReason() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ public record OrderContent(

public List<MealDelivery> toMealDeliveries(TargetOrder targetOrder) {
return orderedMeals.stream()
.map(orderedMeal -> MealDelivery.create(targetOrder.orderId(), orderedMeal))
.map(
orderedMeal ->
MealDelivery.create(
targetOrder.orderId(),
orderType.toOrderTypeForDelivery(),
orderedMeal))
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package core.startup.mealtoktok.domain.order;

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

public enum OrderType {
IMMEDIATE,
SCHEDULED
SCHEDULED;

public OrderTypeForDelivery toOrderTypeForDelivery() {
return switch (this) {
case IMMEDIATE -> OrderTypeForDelivery.IMMEDIATE;
case SCHEDULED -> OrderTypeForDelivery.SCHEDULED;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import core.startup.mealtoktok.domain.dishstore.OperatingHour;
import core.startup.mealtoktok.domain.user.AddressWithCoordinate;
import core.startup.mealtoktok.domain.user.Coordinate;
import core.startup.mealtoktok.infra.user.entity.AddressVO;
import core.startup.mealtoktok.infra.global.entity.AddressVO;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core.startup.mealtoktok.infra.user.entity;
package core.startup.mealtoktok.infra.global.entity;

import jakarta.persistence.Embeddable;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package core.startup.mealtoktok.infra.global.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import core.startup.mealtoktok.common.dto.Image;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Getter
@Table(name = "image")
public class ImageEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String imageUrl;

public static ImageEntity from(Image image) {

if (image == null) {
return null;
}

return new ImageEntity(image.getId() != null ? image.getId() : null, image.getImageUrl());
}

public Image toImage() {
return new Image(id, imageUrl);
}
}
Loading

0 comments on commit 9d97b06

Please sign in to comment.