Skip to content

Commit

Permalink
Merge pull request #178 from THEGOODs-repo/refactor/170
Browse files Browse the repository at this point in the history
โ™ป๏ธ Refactor: ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก ์กฐํšŒ API ์ˆ˜์ •
  • Loading branch information
Eunjin3395 authored May 2, 2024
2 parents f0d87cf + db1af62 commit 9f72ba6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 74 deletions.
112 changes: 60 additions & 52 deletions src/main/java/com/umc/TheGoods/converter/cart/CartConverter.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.umc.TheGoods.converter.cart;

import com.umc.TheGoods.converter.item.ItemConverter;
import com.umc.TheGoods.domain.enums.CartStatus;
import com.umc.TheGoods.domain.item.Item;
import com.umc.TheGoods.domain.item.ItemOption;
import com.umc.TheGoods.domain.order.Cart;
import com.umc.TheGoods.web.dto.cart.CartResponseDTO;
import com.umc.TheGoods.web.dto.item.ItemResponseDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -28,58 +31,63 @@ public static Cart toCartWithoutItemOption(Item item, Integer amount) {
}


// public static CartResponseDTO.cartViewListDTO toCartViewListDTO(List<Cart> cartList) {
//
// List<CartResponseDTO.cartViewDTO> cartViewDTOList = new ArrayList<>();
//
// cartList.forEach(cart -> {
// if (!cartViewDTOList.isEmpty()) {
// CartResponseDTO.cartViewDTO cartViewDTO = cartViewDTOList.get(cartViewDTOList.size() - 1);
// if (cartViewDTO.getItemId().equals(cart.getItem().getId())) { // cartViewDTO ๋‚ด์˜ cartOptionViewDTO ์ถ”๊ฐ€
//
// } else { // ์„ธ๋กœ์šด cartViewDTO ์ถ”๊ฐ€
// CartResponseDTO.cartOptionViewDTO cartOptionViewDTO = toCartOptionViewDTO(cart);
// }
// }
//
// });
//
// return CartResponseDTO.cartViewListDTO.builder()
// .cartViewDTOList(cartList.stream().map(CartConverter::toCartViewDTO).collect(Collectors.toList()))
// .build();
// }

// public static CartResponseDTO.cartViewDTO toCartViewDTO(Cart cart) {
// List<CartResponseDTO.cartDetailViewDTO> cartDetailViewDTOList = cart.getCartDetailList().stream()
// .map(CartConverter::toCartDetailViewDTO).collect(Collectors.toList());
//
// List<ItemResponseDTO.ItemImgResponseDTO> itemImgResponseDTOList = cart.getItem().getItemImgList().stream()
// .map(ItemConverter::getItemImgDTO)
// .filter(ItemResponseDTO.ItemImgResponseDTO::getIsThumbNail).collect(Collectors.toList());
//
// String itemImgUrl = itemImgResponseDTOList.get(0).getItemImgUrl();
//
// return CartResponseDTO.cartViewDTO.builder()
// .cartId(cart.getId())
// .sellerName(cart.getItem().getMember().getNickname())
// .itemId(cart.getItem().getId())
// .itemName(cart.getItem().getName())
// .itemImg(itemImgUrl)
// .deliveryFee(cart.getItem().getDeliveryFee())
// .cartDetailViewDTOList(cartDetailViewDTOList)
// .build();
// }
//
// public static CartResponseDTO.cartOptionViewDTO toCartOptionViewDTO(Cart cart) {
// return CartResponseDTO.cartOptionViewDTO.builder()
// .cartId(cart.getId())
// .optionId(cart.getItemOption() == null ? null : cart.getItemOption().getId())
// .optionName(cart.getItemOption() == null ? null : cart.getItemOption().getName()) // ์˜ต์…˜์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์˜ต์…˜ ์ด๋ฆ„ ์„ค์ •
// .price(cart.getItemOption() == null ? cart.getItem().getPrice() : cart.getItemOption().getPrice())
// .amount(cart.getAmount())
// .build();
// }
//
public static CartResponseDTO.cartViewListDTO toCartViewListDTO(List<Cart> cartList) {

List<CartResponseDTO.cartViewDTO> cartViewDTOList = new ArrayList<>();

cartList.forEach(cart -> {
if (!cartViewDTOList.isEmpty()) {
// cartViewDTOList์˜ ๋งˆ์ง€๋ง‰ DTO์™€ cart๋ฅผ ๋น„๊ตํ•ด cartViewDTO๋ฅผ ์ƒˆ๋กœ ์ถ”๊ฐ€ํ• ์ง€, ๊ธฐ์กด cartViewDTO ๋‚ด์— cartOptionViewDTO๋งŒ ์ถ”๊ฐ€ํ• ์ง€ ๊ฒฐ์ •
CartResponseDTO.cartViewDTO cartViewDTO = cartViewDTOList.get(cartViewDTOList.size() - 1);
if (cartViewDTO.getItemId().equals(cart.getItem().getId())) { // cartViewDTO ๋‚ด์˜ cartOptionViewDTO ์ถ”๊ฐ€
cartViewDTO.addCartOptionViewDTO(toCartOptionViewDTO(cart));
} else { // cartViewDTOList์— ์ƒˆ๋กœ์šด cartViewDTO ์ƒ์„ฑ ๋ฐ ์ถ”๊ฐ€
CartResponseDTO.cartViewDTO newCartViewDTO = toCartViewDTO(cart);
cartViewDTOList.add(newCartViewDTO);
}
} else { // ์ฒซ cartViewDTO ์ถ”๊ฐ€
CartResponseDTO.cartViewDTO newCartViewDTO = toCartViewDTO(cart);
cartViewDTOList.add(newCartViewDTO);
}

});

return CartResponseDTO.cartViewListDTO.builder()
.cartViewDTOList(cartViewDTOList)
.build();
}

public static CartResponseDTO.cartViewDTO toCartViewDTO(Cart cart) {

List<CartResponseDTO.cartOptionViewDTO> cartOptionViewDTOList = new ArrayList<>();
cartOptionViewDTOList.add(toCartOptionViewDTO(cart));

List<ItemResponseDTO.ItemImgResponseDTO> itemImgResponseDTOList = cart.getItem().getItemImgList().stream()
.map(ItemConverter::getItemImgDTO)
.filter(ItemResponseDTO.ItemImgResponseDTO::getIsThumbNail).collect(Collectors.toList());

String itemImgUrl = itemImgResponseDTOList.get(0).getItemImgUrl();

return CartResponseDTO.cartViewDTO.builder()
.sellerName(cart.getItem().getMember().getNickname())
.itemId(cart.getItem().getId())
.itemName(cart.getItem().getName())
.itemImg(itemImgUrl)
.deliveryFee(cart.getItem().getDeliveryFee())
.cartOptionViewDTOList(cartOptionViewDTOList)
.build();
}

public static CartResponseDTO.cartOptionViewDTO toCartOptionViewDTO(Cart cart) {
return CartResponseDTO.cartOptionViewDTO.builder()
.cartId(cart.getId())
.optionId(cart.getItemOption() == null ? null : cart.getItemOption().getId())
.optionName(cart.getItemOption() == null ? null : cart.getItemOption().getName()) // ์˜ต์…˜์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์˜ต์…˜ ์ด๋ฆ„ ์„ค์ •
.price(cart.getItemOption() == null ? cart.getItem().getPrice() : cart.getItemOption().getPrice())
.amount(cart.getAmount())
.build();
}

public static CartResponseDTO.cartStockDTO toCartStockDTO(List<Cart> cartList) {

List<CartResponseDTO.cartOptionStockDTO> cartOptionStockDTOList = cartList.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface CartQueryService {
boolean isExistCart(Long cartId);

List<Cart> getCartsByItem(Long itemId, Member member);

List<Cart> getCartsByMember(Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@ public List<Cart> getCartsByItem(Long itemId, Member member) {
return cartRepository.findAllByMemberAndItemIdAndCartStatus(member, itemId, CartStatus.ACTIVE);
}

@Override
public List<Cart> getCartsByMember(Member member) {
return cartRepository.findAllByMemberAndCartStatusOrderByItemIdAsc(member, CartStatus.ACTIVE);
}


}
36 changes: 18 additions & 18 deletions src/main/java/com/umc/TheGoods/web/controller/CartController.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,24 @@ public ApiResponse<String> add(@RequestBody @Valid CartRequestDTO.cartAddDTOList
return ApiResponse.onSuccess("์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋‹ด๊ธฐ ์„ฑ๊ณต");
}

// @GetMapping
// @Operation(summary = "๋‚˜์˜ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก ์กฐํšŒ API", description = "๋‚˜์˜ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋Š” API ์ž…๋‹ˆ๋‹ค. (๊ตฌ๋งค์ž ํšŒ์›์šฉ)")
// @ApiResponses(value = {
// @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, ์„ฑ๊ณต"),
// })
// public ApiResponse<CartResponseDTO.cartViewListDTO> cartView(Authentication authentication) {
// // ๋น„ํšŒ์›์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ ๋ถˆ๊ฐ€
// if (authentication == null) {
// throw new MemberHandler(ErrorStatus._UNAUTHORIZED);
// }
//
// // request์—์„œ member id ์ถ”์ถœํ•ด Member ์—”ํ‹ฐํ‹ฐ ์ฐพ๊ธฐ
// Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));
//
// List<Cart> cartList = cartQueryService.getCartList(member);
//
// return ApiResponse.onSuccess(CartConverter.toCartViewListDTO(cartList));
// }
@GetMapping
@Operation(summary = "๋‚˜์˜ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก ์กฐํšŒ API", description = "๋‚˜์˜ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋Š” API ์ž…๋‹ˆ๋‹ค. (๊ตฌ๋งค์ž ํšŒ์›์šฉ)")
@ApiResponses(value = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, ์„ฑ๊ณต"),
})
public ApiResponse<CartResponseDTO.cartViewListDTO> cartView(Authentication authentication) {
// ๋น„ํšŒ์›์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ ๋ถˆ๊ฐ€
if (authentication == null) {
throw new MemberHandler(ErrorStatus._UNAUTHORIZED);
}

// request์—์„œ member id ์ถ”์ถœํ•ด Member ์—”ํ‹ฐํ‹ฐ ์ฐพ๊ธฐ
Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND));

List<Cart> cartList = cartQueryService.getCartsByMember(member);

return ApiResponse.onSuccess(CartConverter.toCartViewListDTO(cartList));
}

@PutMapping
@Operation(summary = "์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ˆ˜๋Ÿ‰ ์ˆ˜์ • API", description = "ํ•ด๋‹น ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋‚ด์—ญ์˜ ๋‹ด์€ ์ˆ˜๋Ÿ‰์„ ์ˆ˜์ •ํ•˜๋Š” API ์ž…๋‹ˆ๋‹ค.")
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/umc/TheGoods/web/dto/cart/CartResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@ public static class cartViewListDTO {
@NoArgsConstructor
@AllArgsConstructor
public static class cartViewDTO {
Long cartId;
String sellerName;
Long itemId;
String itemName;
String itemImg;
Integer deliveryFee;
List<cartDetailViewDTO> cartDetailViewDTOList;
List<cartOptionViewDTO> cartOptionViewDTOList;

public void addCartOptionViewDTO(cartOptionViewDTO cartOptionViewDTO) {
this.cartOptionViewDTOList.add(cartOptionViewDTO);
}
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class cartDetailViewDTO {
Long cartDetailId;
public static class cartOptionViewDTO {
Long cartId;
Long optionId;
String optionName;
Long price;
Expand Down

0 comments on commit 9f72ba6

Please sign in to comment.