Skip to content

Commit

Permalink
[BE] 회원 정보를 조회할 때 id에 대한 역순으로 정렬이 안되는 오류 수정 (#368)
Browse files Browse the repository at this point in the history
* fix: 회원정보 조회 시 기본 정렬 기준이 적용되도록 수정

* refactor: 접근제어자 수정
  • Loading branch information
hamcheeseburger authored Aug 3, 2022
1 parent bce7bf5 commit 57b0c68
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

import static com.woowacourse.f12.domain.member.QMember.member;

import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;

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

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport;

public class MemberRepositoryCustomImpl extends QuerydslRepositorySupport implements MemberRepositoryCustom {
Expand All @@ -24,33 +31,56 @@ public MemberRepositoryCustomImpl(final JPAQueryFactory jpaQueryFactory) {
public Slice<Member> findBySearchConditions(final String keyword, final CareerLevel careerLevel,
final JobType jobType,
final Pageable pageable) {
final JPAQuery<Member> jpaQuery = jpaQueryFactory.selectFrom(member)
final JPAQuery<Member> jpaQuery = jpaQueryFactory.select(member)
.from(member)
.where(
containsKeyword(keyword),
eqCareerLevel(careerLevel),
eqJobType(jobType)
)
.offset(pageable.getOffset())
.limit(pageable.getPageSize() + 1);
final List<Member> members = jpaQuery.fetch();
return toSlice(pageable, members);
.limit(pageable.getPageSize() + 1)
.orderBy(makeOrderSpecifiers(pageable));

return toSlice(pageable, jpaQuery.fetch());
}

private OrderSpecifier [] makeOrderSpecifiers(final Pageable pageable) {
return pageable.getSort()
.stream()
.map(this::toOrderSpecifier)
.collect(Collectors.toList()).toArray(OrderSpecifier[]::new);
}

private OrderSpecifier toOrderSpecifier(final Sort.Order sortOrder) {
final Order orderMethod = toOrder(sortOrder);
final PathBuilder<Member> pathBuilder = new PathBuilder<>(member.getType(), member.getMetadata());
return new OrderSpecifier(orderMethod, pathBuilder.get(sortOrder.getProperty()));
}

private Order toOrder(final Sort.Order sortOrder) {
if(sortOrder.isAscending()) {
return Order.ASC;
}
return Order.DESC;
}


private BooleanExpression containsKeyword(final String keyword) {
if (Objects.isNull(keyword) || keyword.isBlank()) {
return null;
}
return member.gitHubId.contains(keyword);
}

public BooleanExpression eqCareerLevel(final CareerLevel careerLevel) {
private BooleanExpression eqCareerLevel(final CareerLevel careerLevel) {
if (Objects.isNull(careerLevel)) {
return null;
}
return member.careerLevel.eq(careerLevel);
}

public BooleanExpression eqJobType(final JobType jobType) {
private BooleanExpression eqJobType(final JobType jobType) {
if (Objects.isNull(jobType)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
package com.woowacourse.f12.acceptance;

import com.woowacourse.f12.domain.inventoryproduct.InventoryProduct;
import com.woowacourse.f12.domain.member.Member;
import com.woowacourse.f12.domain.product.Product;
import com.woowacourse.f12.domain.product.ProductRepository;
import com.woowacourse.f12.dto.request.member.MemberRequest;
import com.woowacourse.f12.dto.response.auth.LoginMemberResponse;
import com.woowacourse.f12.dto.response.auth.LoginResponse;
import com.woowacourse.f12.dto.response.member.MemberPageResponse;
import com.woowacourse.f12.dto.response.member.MemberResponse;
import com.woowacourse.f12.dto.response.member.MemberWithProfileProductResponse;
import com.woowacourse.f12.dto.response.product.ProductResponse;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import java.util.List;

import static com.woowacourse.f12.acceptance.support.LoginUtil.로그인을_한다;
import static com.woowacourse.f12.acceptance.support.RestAssuredRequestUtil.GET_요청을_보낸다;
import static com.woowacourse.f12.acceptance.support.RestAssuredRequestUtil.로그인된_상태로_GET_요청을_보낸다;
import static com.woowacourse.f12.acceptance.support.RestAssuredRequestUtil.로그인된_상태로_PATCH_요청을_보낸다;
import static com.woowacourse.f12.acceptance.support.RestAssuredRequestUtil.*;
import static com.woowacourse.f12.domain.member.CareerLevel.JUNIOR;
import static com.woowacourse.f12.domain.member.JobType.BACKEND;
import static com.woowacourse.f12.presentation.member.CareerLevelConstant.JUNIOR_CONSTANT;
Expand All @@ -21,26 +38,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import com.woowacourse.f12.domain.inventoryproduct.InventoryProduct;
import com.woowacourse.f12.domain.member.Member;
import com.woowacourse.f12.domain.product.Product;
import com.woowacourse.f12.domain.product.ProductRepository;
import com.woowacourse.f12.dto.request.member.MemberRequest;
import com.woowacourse.f12.dto.response.auth.LoginMemberResponse;
import com.woowacourse.f12.dto.response.auth.LoginResponse;
import com.woowacourse.f12.dto.response.member.MemberPageResponse;
import com.woowacourse.f12.dto.response.member.MemberResponse;
import com.woowacourse.f12.dto.response.member.MemberWithProfileProductResponse;
import com.woowacourse.f12.dto.response.product.ProductResponse;
import com.woowacourse.f12.dto.response.product.ProductResponse;
import com.woowacourse.f12.support.InventoryProductFixtures;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

public class MemberAcceptanceTest extends AcceptanceTest {

@Autowired
Expand Down Expand Up @@ -241,6 +238,35 @@ public class MemberAcceptanceTest extends AcceptanceTest {
);
}

@Test
void 회원정보_목록은_id_역순으로_조회된다() {
// given
MemberRequest memberRequest = new MemberRequest(SENIOR_CONSTANT, BACKEND_CONSTANT);

LoginResponse firstLoginResponse = 로그인을_한다(MINCHO_GITHUB.getCode());
로그인된_상태로_PATCH_요청을_보낸다("/api/v1/members/me", firstLoginResponse.getToken(), memberRequest);

LoginResponse secondLoginResponse = 로그인을_한다(CORINNE_GITHUB.getCode());
로그인된_상태로_PATCH_요청을_보낸다("/api/v1/members/me", secondLoginResponse.getToken(), memberRequest);

// when
ExtractableResponse<Response> response = GET_요청을_보낸다("/api/v1/members?page=0&size=2");

// then
MemberPageResponse memberPageResponse = response.as(MemberPageResponse.class);

assertAll(
() -> assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()),
() -> assertThat(memberPageResponse.isHasNext()).isFalse(),
() -> assertThat(memberPageResponse.getItems()
.stream()
.map(MemberWithProfileProductResponse::getId))
.usingRecursiveFieldByFieldElementComparatorOnFields("id")
.hasSize(2)
.containsExactly(secondLoginResponse.getMember().getId(), firstLoginResponse.getMember().getId())
);
}

private Product 제품을_저장한다(Product product) {
return productRepository.save(product);
}
Expand Down

0 comments on commit 57b0c68

Please sign in to comment.