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

refactor: 마이페이지에 구매 혹은 판매한 상품의 id를 반환을 추가한다 #122

Merged
merged 2 commits into from
Jun 8, 2024
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 @@ -4,6 +4,7 @@ public record TradeHistoryResponse(
Long tradeHistoryId,
String buyerName,
String sellerName,
Long productId,
String productTitle,
int productOriginPrice,
int productDiscountPrice,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TradeHistoryResponse> findTradeHistories(final Long memberId, final boolean isSeller) {
if (isSeller) {
return findSellerHistories(memberId);
return findHistories(memberId, tradeHistory.sellerId::eq);
}

return findBuyerHistories(memberId);
}

private List<TradeHistoryResponse> 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<TradeHistoryResponse> findBuyerHistories(final Long buyerId) {
QMember buyer = new QMember("buyer");
QMember seller = new QMember("seller");
private List<TradeHistoryResponse> findHistories(final Long memberId, final Function<Long, BooleanExpression> 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,
Expand All @@ -66,7 +50,7 @@ private List<TradeHistoryResponse> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public List<TradeHistoryResponse> findHistories(final Long memberId, final boole
it.getId(),
"buyer",
"seller",
1L,
"title",
it.getProductOriginPrice(),
it.getProductDiscountPrice(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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 타입으로 ',' 이용해서 묶음")

)
));
}
Expand Down
Loading