Skip to content

Commit

Permalink
refactor:
Browse files Browse the repository at this point in the history
  • Loading branch information
Kang1221 committed Sep 1, 2024
1 parent b541c29 commit 32cd19a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/main/java/co/orange/ddanzi/repository/ItemRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
public interface ItemRepository extends JpaRepository<Item, String> {
List<Item> findAllBySeller(User user);

Integer countAllBySeller(User user);

@Query("SELECT MAX(CAST(SUBSTRING(i.id, 14, 2) AS integer)) FROM Item i WHERE i.product = :product")
Integer findMaxSequenceNumberByProduct(@Param("product") Product product);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface OrderRepository extends JpaRepository<Order, String> {

@Query("SELECT o from Order o where o.item = :item")
Optional<Order> findByItem(@Param("item") Item item);

Integer countAllByBuyer(User user);
}
4 changes: 4 additions & 0 deletions src/main/java/co/orange/ddanzi/service/ItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ private List<SelectedOption> setSelectedOptionList(Order order){
return selectedOptionList;
}

public Integer getMyItemCount(User user){
return itemRepository.countAllBySeller(user);
}

public List<MyItem> getMyItemList(User user){
List<Item> itemList = itemRepository.findAllBySeller(user);
List<MyItem> myItemList = new ArrayList<>();
Expand Down
31 changes: 7 additions & 24 deletions src/main/java/co/orange/ddanzi/service/MyPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@
public class MyPageService {
private final AuthUtils authUtils;
private final HomeService homeService;
private final DiscountRepository discountRepository;
private final OrderRepository orderRepository;
private final PaymentRepository paymentRepository;
private final InterestProductRepository interestProductRepository;

@Autowired
OrderService orderService;
@Autowired
ItemService itemService;

Expand All @@ -52,39 +51,23 @@ public ApiResponse<?> getMyPage(){
@Transactional
public ApiResponse<?> getMyOrder(){
User user = authUtils.getUser();
List<Order> orderList = orderRepository.findByBuyer(user);

List<MyOrder> orderProductList = new ArrayList<>();

for (Order order : orderList) {
Product product = order.getItem().getProduct();
Discount discount = discountRepository.findById(product.getId()).orElse(null);
Payment payment = paymentRepository.findByBuyerAndItem(user, order.getItem()).orElseThrow(() -> new PaymentNotFoundException());
MyOrder myOrder = MyOrder.builder()
.productId(product.getId())
.orderId(order.getId())
.productName(product.getName())
.imgUrl(product.getImgUrl())
.originPrice(product.getOriginPrice())
.salePrice(product.getOriginPrice()-discount.getDiscountPrice())
.paidAt(payment.getEndedAt())
.build();
Integer totalCount = orderService.getMyOrderCount(user);
List<MyOrder> orderProductList = orderService.getMyOrderList(user);

orderProductList.add(myOrder);
}
return ApiResponse.onSuccess(Success.GET_MY_ORDER_LIST_SUCCESS, MyOrderResponseDto.builder()
.totalCount(orderList.size())
.totalCount(totalCount)
.orderProductList(orderProductList)
.build());
}

@Transactional
public ApiResponse<?> getMyItem(){
User user = authUtils.getUser();
Integer totalCount = itemService.getMyItemCount(user);
List<MyItem> myItemList = itemService.getMyItemList(user);

MyItemResponseDto responseDto = MyItemResponseDto.builder()
.totalCount(myItemList.size())
.totalCount(totalCount)
.itemProductList(myItemList)
.build();
return ApiResponse.onSuccess(Success.GET_MY_ITEM_LIST_SUCCESS, responseDto);
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/co/orange/ddanzi/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import co.orange.ddanzi.domain.product.Product;
import co.orange.ddanzi.domain.product.enums.ItemStatus;
import co.orange.ddanzi.domain.user.User;
import co.orange.ddanzi.dto.mypage.MyOrder;
import co.orange.ddanzi.dto.order.CheckProductResponseDto;
import co.orange.ddanzi.dto.order.CreateOrderRequestDto;
import co.orange.ddanzi.common.response.ApiResponse;
Expand All @@ -26,6 +27,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
Expand Down Expand Up @@ -197,9 +199,32 @@ private OrderResponseDto setOrderResponseDto(User user, Order order, Item item,
return responseDto;
}

public void deleteOrderOfUser(User user){

public Integer getMyOrderCount(User user){
return orderRepository.countAllByBuyer(user);
}

public List<MyOrder> getMyOrderList(User user){
List<Order> orderList = orderRepository.findByBuyer(user);

List<MyOrder> orderProductList = new ArrayList<>();

for (Order order : orderList) {
Product product = order.getItem().getProduct();
Discount discount = discountRepository.findById(product.getId()).orElse(null);
Payment payment = paymentRepository.findByBuyerAndItem(user, order.getItem()).orElseThrow(() -> new PaymentNotFoundException());
MyOrder myOrder = MyOrder.builder()
.productId(product.getId())
.orderId(order.getId())
.productName(product.getName())
.imgUrl(product.getImgUrl())
.originPrice(product.getOriginPrice())
.salePrice(product.getOriginPrice()-discount.getDiscountPrice())
.paidAt(payment.getEndedAt())
.build();

orderProductList.add(myOrder);
}
return orderProductList;
}

}

0 comments on commit 32cd19a

Please sign in to comment.