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] 주문 화면 조회 #178

Merged
merged 6 commits into from
Mar 14, 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,13 +3,14 @@
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.entity.OrderStatus;

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

import com.codepatissier.keki.order.service.OrderService;
import com.codepatissier.keki.user.service.AuthService;
Expand All @@ -26,6 +27,7 @@
import org.springframework.web.bind.annotation.*;


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

import static com.codepatissier.keki.common.BaseResponseStatus.*;
Expand Down Expand Up @@ -100,4 +102,16 @@ public BaseResponse<?> changeOrderStatus(PatchOrderStatusReq patchOrderStatusReq
return new BaseResponse<>(e.getStatus());
}
}

/**
* 주문 화면 조회
*/
@GetMapping("/view/{storeIdx}")
public BaseResponse<GetOrderStore> getStoreDessertsAndOptions(@PathVariable("storeIdx") Long storeIdx){
try{
return new BaseResponse<>(this.orderService.getStoreDessertsAndOptions(storeIdx));
}catch (BaseException e){
return new BaseResponse<>(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.codepatissier.keki.order.dto;

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

import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class GetOrderStore {
Long storeIdx;
String storeName;
String accountName;
String storeAccount;
List<GetStoreDessertAndOptions> getStoreDessertsAndOptions;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.codepatissier.keki.order.dto;

import com.codepatissier.keki.dessert.dto.OptionDTO;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class GetStoreDessertAndOptions {

Long dessertIdx;
String dessertName;
Integer dessertPrice;
List<OptionDTO> options;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.codepatissier.keki.common.BaseException;

import com.codepatissier.keki.dessert.dto.OptionDTO;
import com.codepatissier.keki.dessert.entity.Dessert;
import com.codepatissier.keki.dessert.repository.DessertRepository;
import com.codepatissier.keki.dessert.repository.OptionRepository;
import com.codepatissier.keki.common.Role;
import com.codepatissier.keki.order.dto.*;

Expand All @@ -10,11 +14,14 @@
import com.codepatissier.keki.order.repository.OptionOrderRepository;
import com.codepatissier.keki.order.repository.OrderImgRepository;
import com.codepatissier.keki.order.repository.OrderRepository;
import com.codepatissier.keki.store.entity.Store;
import com.codepatissier.keki.store.repository.StoreRepository;
import com.codepatissier.keki.user.entity.User;
import com.codepatissier.keki.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -32,10 +39,13 @@ public class OrderService {
private final UserRepository userRepository;
private final OrderImgRepository orderImgRepository;
private final OptionOrderRepository optionOrderRepository;
private final DessertRepository dessertRepository;
private final StoreRepository storeRepository;
private final OptionRepository optionRepository;

// 주문 취소
public void cancelOrder(Long userIdx, Long orderIdx) throws BaseException{
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE_STATUS).orElseThrow(() -> new BaseException(INVALID_USER_AND_STATUS));
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);
Expand All @@ -47,7 +57,7 @@ public void cancelOrder(Long userIdx, Long orderIdx) throws BaseException{

// [판매자] 주문 상태 변경
public void changeOrderStatus(Long userIdx, PatchOrderStatusReq patchOrderStatusReq) throws BaseException{
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE_STATUS).orElseThrow(() -> new BaseException(INVALID_USER_AND_STATUS));
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE_STATUS).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋㅋㅋㅋㅋㅋ감사해유,,🫶

Order order = orderRepository.findById(patchOrderStatusReq.getOrderIdx()).orElseThrow(() -> new BaseException(INVALID_ORDER_IDX));

if(!order.getStore().getUser().equals(user)) throw new BaseException(NO_MATCH_ORDER_USER);
Expand All @@ -57,7 +67,7 @@ public void changeOrderStatus(Long userIdx, PatchOrderStatusReq patchOrderStatus

public GetOrder getOrder(Long userIdx, Long orderIdx) throws BaseException{
// TODO: 겹치는 부분이 3줄 이상인데 extract method 는 어떠신지?
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE_STATUS).orElseThrow(() -> new BaseException(INVALID_USER_AND_STATUS));
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);
Expand All @@ -67,12 +77,29 @@ public GetOrder getOrder(Long userIdx, Long orderIdx) throws BaseException{
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: 아직 판매자 계좌 번호는 DB에 없는 건가요?
// TODO: 아직 판매자 계좌 번호 저장 이전
return new GetOrder(order.getOrderStatus().getName(), 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));
List<Dessert> desserts = this.dessertRepository.findByStoreAndStatusOrderByDessertIdx(store, ACTIVE_STATUS);
List<GetStoreDessertAndOptions> dessertsAndOptions = new ArrayList<>();
// 디저트 별의 option 들을 찾아서 한번에 저장하는 것이 필요함.
for (Dessert dessert: desserts){
dessertsAndOptions.add(new GetStoreDessertAndOptions(dessert.getDessertIdx(), dessert.getDessertName(), dessert.getDessertPrice(),
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);
}

// 주문 내역 조회
public GetOrderHistoryRes getOrderHistory(Long userIdx, GetOrderHistoryReq orderStatusReq) throws BaseException {
User user = userRepository.findByUserIdxAndStatusEquals(userIdx, ACTIVE_STATUS).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
Expand Down