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

[feature/161-orders-get] 주문 수정 화면 조회 api 생성 #182

Merged
merged 2 commits into from
Mar 15, 2023
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
Expand Up @@ -3,15 +3,9 @@
import com.codepatissier.keki.common.BaseException;
import com.codepatissier.keki.common.BaseResponse;

import com.codepatissier.keki.order.dto.GetOrderStore;
import com.codepatissier.keki.order.dto.GetStoreDessertAndOptions;
import com.codepatissier.keki.order.dto.GetOrderHistoryReq;
import com.codepatissier.keki.order.dto.GetOrderHistoryRes;
import com.codepatissier.keki.order.dto.PatchOrderStatusReq;
import com.codepatissier.keki.order.dto.*;
import com.codepatissier.keki.order.entity.OrderStatus;

import com.codepatissier.keki.order.dto.GetOrder;

import com.codepatissier.keki.order.service.OrderService;
import com.codepatissier.keki.user.service.AuthService;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
Expand All @@ -27,7 +21,6 @@
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Objects;

import static com.codepatissier.keki.common.BaseResponseStatus.*;
Expand All @@ -52,7 +45,7 @@ public class OrderController {
@GetMapping("{orderIdx}")
public BaseResponse<GetOrder> getOrder(@PathVariable("orderIdx") Long orderIdx){
try {
return new BaseResponse<>(orderService.getOrder(authService.getUserIdx(), orderIdx));
return new BaseResponse<>(orderService.getOrderReturn(authService.getUserIdx(), orderIdx));
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
Expand Down Expand Up @@ -114,4 +107,16 @@ public BaseResponse<GetOrderStore> getStoreDessertsAndOptions(@PathVariable("sto
return new BaseResponse<>(e.getStatus());
}
}

/**
* 주문 수정 화면 조회
*/
@GetMapping("/{orderIdx}/editView")
public BaseResponse<GetEditOrder> getEditOrderView(@PathVariable("orderIdx") Long orderIdx){
try {
return new BaseResponse<>(this.orderService.getEditOrderView(orderIdx, authService.getUserIdx()));
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codepatissier.keki.order.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class GetEditOrder {
GetOrder getOrder;
List<GetStoreDessertAndOptions> getStoreDessertAndOptions;

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
@AllArgsConstructor
@NoArgsConstructor
public class GetOrder {
Long orderIdx;
String orderStatus;
Long dessertIdx;
String dessertName;

// 주문 금액
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,39 @@ public void changeOrderStatus(Long userIdx, PatchOrderStatusReq patchOrderStatus
orderRepository.save(order);
}

public GetOrder getOrder(Long userIdx, Long orderIdx) throws BaseException{
// 주문 상세 return 값
public GetOrder getOrderReturn(Long userIdx, Long orderIdx) throws BaseException{
// TODO: 겹치는 부분이 3줄 이상인데 extract method 는 어떠신지?
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE_STATUS).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
Order order = orderRepository.findById(orderIdx).orElseThrow(() -> new BaseException(INVALID_ORDER_IDX));

if(!order.getUser().equals(user)) throw new BaseException(NO_MATCH_ORDER_USER);
return getOrder(order);

}

// 주문 상세 조회
private GetOrder getOrder(Order order) {
List<GetOrderImg> orderImgs = orderImgRepository.findByOrderAndStatusEquals(order, ACTIVE_STATUS).stream()
.map(getOrder -> new GetOrderImg(getOrder.getOrderImgIdx(), getOrder.getImgUrl())).collect(Collectors.toList());
List<GetOptionOrder> optionOrders = optionOrderRepository.findByOrderAndStatusEquals(order, ACTIVE_STATUS).stream()
.map(getOptionOrder -> new GetOptionOrder(getOptionOrder.getOption().getOptionIdx(), getOptionOrder.getOption().getDescription(), getOptionOrder.getOption().getPrice())).collect(Collectors.toList());

// TODO: 아직 판매자 계좌 번호 저장 이전
return new GetOrder(order.getOrderStatus().getName(), order.getDessert().getDessertName(),
return new GetOrder(order.getOrderIdx(), order.getOrderStatus().getName(), order.getDessert().getDessertIdx(), order.getDessert().getDessertName(),
order.getDessert().getDessertPrice(), order.getExtraPrice(), order.getTotalPrice(), order.getRequest(), order.getPickupDate(), order.getStore().getStoreIdx(), order.getStore().getUser().getNickname(), null, order.getStore().getAddress(), orderImgs, optionOrders);

}

// 주문 조회
public GetOrderStore getStoreDessertsAndOptions(Long storeIdx) throws BaseException{
Store store = this.storeRepository.findByStoreIdxAndStatus(storeIdx, ACTIVE_STATUS).orElseThrow(() -> new BaseException(INVALID_STORE_IDX));
// TODO: 아직 판매자 계좌 번호 저장 이전
return new GetOrderStore(store.getStoreIdx(), store.getUser().getNickname(),
null, store.getAddress(),getStoreDessertAndOptionList(store));
}

// 판매자 디저트 + 옵션 불러오기
private List<GetStoreDessertAndOptions> getStoreDessertAndOptionList(Store store) throws BaseException{
List<Dessert> desserts = this.dessertRepository.findByStoreAndStatusOrderByDessertIdx(store, ACTIVE_STATUS);
List<GetStoreDessertAndOptions> dessertsAndOptions = new ArrayList<>();
// 디저트 별의 option 들을 찾아서 한번에 저장하는 것이 필요함.
Expand All @@ -94,11 +106,8 @@ public GetOrderStore getStoreDessertsAndOptions(Long storeIdx) throws BaseExcept
this.optionRepository.findByDessertAndStatusOrderByOptionIdx(dessert, ACTIVE_STATUS).stream()
.map(option -> new OptionDTO(option.getOptionIdx(), option.getDescription(), option.getPrice())).collect(Collectors.toList())));
}

// TODO: 아직 판매자 계좌 번호 저장 이전
return new GetOrderStore(store.getStoreIdx(), store.getUser().getNickname(),
null, store.getAddress(),dessertsAndOptions);
}
return dessertsAndOptions;
}

// 주문 내역 조회
public GetOrderHistoryRes getOrderHistory(Long userIdx, GetOrderHistoryReq orderStatusReq) throws BaseException {
Expand Down Expand Up @@ -140,4 +149,14 @@ private NumOfOrder getCountByOrderStatus(User user) throws BaseException{
} else throw new BaseException(INVALID_USER_IDX);
return numOfOrder;
}

// 주문 수정 조회
public GetEditOrder getEditOrderView(Long orderIdx, Long userIdx) throws BaseException{
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE_STATUS).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
Order order = orderRepository.findById(orderIdx).orElseThrow(() -> new BaseException(INVALID_ORDER_IDX));
if(!order.getUser().equals(user)) throw new BaseException(NO_MATCH_ORDER_USER);
if(!order.getOrderStatus().equals(ORDER_WAITING)) throw new BaseException(NO_MATCH_ORDER_STATUS);

return new GetEditOrder(this.getOrder(order), this.getStoreDessertAndOptionList(order.getStore()));
}
}