diff --git a/market-api/src/main/java/com/server/member/domain/member/dto/TradeHistoryResponse.java b/market-api/src/main/java/com/server/member/domain/member/dto/TradeHistoryResponse.java index d78444e..b690f69 100644 --- a/market-api/src/main/java/com/server/member/domain/member/dto/TradeHistoryResponse.java +++ b/market-api/src/main/java/com/server/member/domain/member/dto/TradeHistoryResponse.java @@ -4,6 +4,7 @@ public record TradeHistoryResponse( Long tradeHistoryId, String buyerName, String sellerName, + Long productId, String productTitle, int productOriginPrice, int productDiscountPrice, diff --git a/market-api/src/main/java/com/server/member/infrastructure/member/TradeHistoryQueryRepository.java b/market-api/src/main/java/com/server/member/infrastructure/member/TradeHistoryQueryRepository.java index 321aa1c..87a90f7 100644 --- a/market-api/src/main/java/com/server/member/infrastructure/member/TradeHistoryQueryRepository.java +++ b/market-api/src/main/java/com/server/member/infrastructure/member/TradeHistoryQueryRepository.java @@ -1,62 +1,46 @@ package com.server.member.infrastructure.member; +import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.jpa.impl.JPAQueryFactory; import com.server.member.domain.member.QMember; import com.server.member.domain.member.dto.TradeHistoryResponse; -import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.function.Function; +import static com.querydsl.core.types.Projections.constructor; import static com.server.market.domain.product.QProduct.product; import static com.server.member.domain.member.QTradeHistory.tradeHistory; -import static com.querydsl.core.types.Projections.constructor; @RequiredArgsConstructor @Repository public class TradeHistoryQueryRepository { + private static final String BUYER = "buyer"; + private static final String SELLER = "seller"; + private final JPAQueryFactory jpaQueryFactory; public List findTradeHistories(final Long memberId, final boolean isSeller) { if (isSeller) { - return findSellerHistories(memberId); + return findHistories(memberId, tradeHistory.sellerId::eq); } - return findBuyerHistories(memberId); - } - - private List findSellerHistories(final Long sellerId) { - QMember buyer = new QMember("buyer"); - QMember seller = new QMember("seller"); - - return jpaQueryFactory.select( - constructor(TradeHistoryResponse.class, - tradeHistory.id, - buyer.nickname, - seller.nickname, - product.description.title, - tradeHistory.productOriginPrice, - tradeHistory.productDiscountPrice, - tradeHistory.usingCouponIds - ) - ).from(tradeHistory) - .leftJoin(buyer).on(tradeHistory.buyerId.eq(buyer.id)) - .leftJoin(seller).on(tradeHistory.sellerId.eq(seller.id)) - .leftJoin(product).on(tradeHistory.productId.eq(product.id)) - .where(tradeHistory.sellerId.eq(sellerId)) - .fetch(); + return findHistories(memberId, tradeHistory.buyerId::eq); } - private List findBuyerHistories(final Long buyerId) { - QMember buyer = new QMember("buyer"); - QMember seller = new QMember("seller"); + private List findHistories(final Long memberId, final Function memberPredicate) { + QMember buyer = new QMember(BUYER); + QMember seller = new QMember(SELLER); return jpaQueryFactory.select( constructor(TradeHistoryResponse.class, tradeHistory.id, buyer.nickname, seller.nickname, + product.id, product.description.title, tradeHistory.productOriginPrice, tradeHistory.productDiscountPrice, @@ -66,7 +50,7 @@ private List findBuyerHistories(final Long buyerId) { .leftJoin(buyer).on(tradeHistory.buyerId.eq(buyer.id)) .leftJoin(seller).on(tradeHistory.sellerId.eq(seller.id)) .leftJoin(product).on(tradeHistory.productId.eq(product.id)) - .where(tradeHistory.buyerId.eq(buyerId)) + .where(memberPredicate.apply(memberId)) .fetch(); } } diff --git a/market-api/src/test/java/com/server/member/infrastructure/member/TradeHistoryFakeRepository.java b/market-api/src/test/java/com/server/member/infrastructure/member/TradeHistoryFakeRepository.java index 99a70fb..2c8811a 100644 --- a/market-api/src/test/java/com/server/member/infrastructure/member/TradeHistoryFakeRepository.java +++ b/market-api/src/test/java/com/server/member/infrastructure/member/TradeHistoryFakeRepository.java @@ -57,6 +57,7 @@ public List findHistories(final Long memberId, final boole it.getId(), "buyer", "seller", + 1L, "title", it.getProductOriginPrice(), it.getProductDiscountPrice(), diff --git a/market-api/src/test/java/com/server/member/ui/member/MemberControllerTest.java b/market-api/src/test/java/com/server/member/ui/member/MemberControllerTest.java index a590eb7..fdf0605 100644 --- a/market-api/src/test/java/com/server/member/ui/member/MemberControllerTest.java +++ b/market-api/src/test/java/com/server/member/ui/member/MemberControllerTest.java @@ -1,6 +1,5 @@ package com.server.member.ui.member; -import com.fasterxml.jackson.databind.ObjectMapper; import com.server.helper.MockBeanInjection; import com.server.market.domain.product.vo.Location; import com.server.market.domain.product.vo.ProductStatus; @@ -41,14 +40,11 @@ class MemberControllerTest extends MockBeanInjection { @Autowired private MockMvc mockMvc; - @Autowired - private ObjectMapper objectMapper; - @Test void 거래_내역을_조회한다() throws Exception { // given when(memberService.findTradeHistories(anyLong(), anyLong(), anyBoolean())) - .thenReturn(List.of(new TradeHistoryResponse(1L, "buyerNickname", "sellerNickname", "productTitle", 10000, 10, "1,2,3"))); + .thenReturn(List.of(new TradeHistoryResponse(1L, "buyerNickname", "sellerNickname", 1L, "productTitle", 10000, 10, "1,2,3"))); // when & then mockMvc.perform(get("/api/members/{memberId}/histories", 1L) @@ -66,11 +62,11 @@ class MemberControllerTest extends MockBeanInjection { fieldWithPath("[0].tradeHistoryId").description("거래 내역 id"), fieldWithPath("[0].buyerName").description("구매자 닉네임"), fieldWithPath("[0].sellerName").description("판매자 닉네임"), + fieldWithPath("[0].productId").description("상품 id"), fieldWithPath("[0].productTitle").description("상품 제목"), fieldWithPath("[0].productOriginPrice").description("상품 정상가"), fieldWithPath("[0].productDiscountPrice").description("상품 할인해서 구매한 가격"), fieldWithPath("[0].usingCouponIds").description("사용한 쿠폰 ids, String 타입으로 ',' 이용해서 묶음") - ) )); }