Skip to content

Commit

Permalink
update: Updated Order MVC
Browse files Browse the repository at this point in the history
  • Loading branch information
koreanMike513 committed Feb 4, 2025
1 parent 23d004b commit 74342c3
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class Order extends BaseTimeEntity {

private int quantity;

private double rate;

@Enumerated(EnumType.STRING)
private OrderStatus status;

Expand All @@ -56,8 +58,6 @@ public class Order extends BaseTimeEntity {
@JoinColumn(name = "voucher_id")
private Voucher voucher;

private LocalDateTime collectionTime;

public BigDecimal calculateTotalCost() {
return (voucher != null)
? voucher.apply(food.calculateCost(quantity), food.getCurrency())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class OrderCreateRequestDTO {
@JsonProperty("store_id")
private Long storeId;

@JsonProperty("currency_id")
private Long currencyId;

@JsonProperty("total_cost")
private BigDecimal totalCost;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class OrderDTO {
@JsonProperty("quantity")
private int quantity;

@JsonProperty("rate")
private double rate;

@JsonProperty("status")
private String status;

Expand All @@ -44,9 +47,6 @@ public class OrderDTO {
@JsonProperty("created_at")
private LocalDateTime createdAt;

@JsonProperty("collection_time")
private LocalDateTime collectionTime;

@Builder
@QueryProjection
public OrderDTO(
Expand All @@ -56,10 +56,10 @@ public OrderDTO(
String currencyCode,
String currencySymbol,
int quantity,
double rate,
String status,
Long payment,
Long voucher,
LocalDateTime collectionTime,
LocalDateTime createdAt
) {

Expand All @@ -69,10 +69,10 @@ public OrderDTO(
this.currencyCode = currencyCode;
this.currencySymbol = currencySymbol;
this.quantity = quantity;
this.rate = rate;
this.status = status;
this.payment = payment;
this.voucher = voucher;
this.collectionTime = collectionTime;
this.createdAt = createdAt;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.f_lab.joyeuse_planete.orders.repository;

import com.f_lab.joyeuse_planete.core.domain.Order;
import com.f_lab.joyeuse_planete.orders.domain.OrderSearchCondition;
import com.f_lab.joyeuse_planete.orders.dto.request.OrderCreateRequestDTO;
import com.f_lab.joyeuse_planete.orders.dto.response.OrderDTO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -10,4 +12,5 @@ public interface OrderRepositoryCustom {

// Page<OrderDTO> findOrders(Long memberId, OrderSearchCondition condition, Pageable pageable);
Page<OrderDTO> findOrders(OrderSearchCondition condition, Pageable pageable);
Order saveOrder(OrderCreateRequestDTO request);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.f_lab.joyeuse_planete.orders.repository;

import com.f_lab.joyeuse_planete.core.domain.Order;
import com.f_lab.joyeuse_planete.core.domain.OrderStatus;

import com.f_lab.joyeuse_planete.orders.domain.OrderSearchCondition;
import com.f_lab.joyeuse_planete.orders.dto.request.OrderCreateRequestDTO;
import com.f_lab.joyeuse_planete.orders.dto.response.OrderDTO;
import com.f_lab.joyeuse_planete.orders.dto.response.QOrderDTO;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -44,6 +48,29 @@ public OrderRepositoryCustomImpl(EntityManager em) {
this.queryFactory = new JPAQueryFactory(em);
}

@Override
public Order saveOrder(OrderCreateRequestDTO request) {
long orderId = queryFactory
.insert(order)
.columns(
order.food.id,
order.totalCost,
order.quantity,
order.status,
order.voucher.id
)
.values(
request.getFoodId(),
request.getTotalCost(),
request.getQuantity(),
OrderStatus.READY,
request.getVoucherId()
)
.execute();

return queryFactory.selectFrom(order).where(order.id.eq(orderId)).fetchFirst();
}

@Override
public Page<OrderDTO> findOrders(OrderSearchCondition condition, Pageable pageable) {
List<OrderDTO> results = queryFactory
Expand All @@ -54,10 +81,10 @@ public Page<OrderDTO> findOrders(OrderSearchCondition condition, Pageable pageab
food.currency.currencyCode,
food.currency.currencySymbol,
order.quantity,
order.rate,
order.status.stringValue(),
order.payment.id.as("paymentId"),
order.voucher.id.as("voucherId"),
order.collectionTime.as("collectionTime"),
order.createdAt.as("createdAt")
))
.from(order)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public void updateOrderStatus(Long orderId, OrderStatus status) {

@Transactional
public OrderCreateResponseDTO createFoodOrder(OrderCreateRequestDTO request) {
Order order = request.toEntity();
Order order;
try {
orderRepository.save(order);
order = orderRepository.saveOrder(request);

} catch (JoyeusePlaneteApplicationException e) {
LogUtil.exception("OrderService.createFoodOrder", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ private OrderDTO from(Order order) {
.status(order.getStatus().name())
.payment(order.getPayment() != null ? order.getPayment().getId() : null)
.voucher(order.getVoucher() != null ? order.getVoucher().getId() : null)
.collectionTime(order.getCollectionTime())
.createdAt(order.getCreatedAt())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import com.f_lab.joyeuse_planete.core.domain.Order;
import com.f_lab.joyeuse_planete.core.exceptions.JoyeusePlaneteApplicationException;
import com.f_lab.joyeuse_planete.core.kafka.exceptions.RetryableException;
import com.f_lab.joyeuse_planete.core.kafka.service.KafkaService;
import com.f_lab.joyeuse_planete.orders.dto.request.OrderCreateRequestDTO;
import com.f_lab.joyeuse_planete.orders.dto.response.OrderCreateResponseDTO;
Expand Down Expand Up @@ -43,9 +42,10 @@ void testCreateOrderSuccess() {
// given
OrderCreateResponseDTO expected = createOrderCreateResponseDTO("PROCESSING");
OrderCreateRequestDTO request = createOrderCreateRequestDTO();
Order order = createOrder();

// when
when(orderRepository.save(any(Order.class))).thenReturn(null);
when(orderRepository.saveOrder(any())).thenReturn(order);
doNothing().when(kafkaService).sendKafkaEvent(anyString(), any(Object.class));
OrderCreateResponseDTO response = orderService.createFoodOrder(request);

Expand All @@ -60,7 +60,7 @@ void testCreateOrderRepositoryThrowExceptionFail1() {
OrderCreateRequestDTO request = createOrderCreateRequestDTO();

// when
when(orderRepository.save(any(Order.class))).thenThrow(new RuntimeException());
when(orderRepository.saveOrder(any())).thenThrow(new RuntimeException());

// then
assertThatThrownBy(() -> orderService.createFoodOrder(request))
Expand All @@ -74,7 +74,7 @@ void testCreateOrderRepositoryThrowExceptionFail2() {
OrderCreateRequestDTO request = createOrderCreateRequestDTO();

// when
when(orderRepository.save(any(Order.class))).thenThrow(new JoyeusePlaneteApplicationException());
when(orderRepository.saveOrder(any())).thenThrow(new JoyeusePlaneteApplicationException());

// then
assertThatThrownBy(() -> orderService.createFoodOrder(request))
Expand All @@ -86,9 +86,10 @@ void testCreateOrderRepositoryThrowExceptionFail2() {
void testCreateOrderRepositoryKafkaServiceFail() {
// given
OrderCreateRequestDTO request = createOrderCreateRequestDTO();
Order order = createOrder();

// when
when(orderRepository.save(any(Order.class))).thenReturn(null);
when(orderRepository.saveOrder(any())).thenReturn(order);
doThrow(JoyeusePlaneteApplicationException.class).when(kafkaService).sendKafkaEvent(any(), any());

// then
Expand All @@ -113,6 +114,10 @@ private OrderCreateRequestDTO createOrderCreateRequestDTO() {
.build();
}

private Order createOrder() {
return Order.builder().id(1L).build();
}

private OrderCreateResponseDTO createOrderCreateResponseDTO(String message) {
return new OrderCreateResponseDTO(message);
}
Expand Down

0 comments on commit 74342c3

Please sign in to comment.