Skip to content

Commit

Permalink
feat: 이름으로 검색할 때 글자 수가 하나이면 동등 검색하는 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
seokjin8678 committed Apr 4, 2024
1 parent 90757ce commit c46078f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ public Page<AdminArtistV1Response> findAll(SearchCondition searchCondition) {
private BooleanExpression containSearchFilter(String searchFilter, String searchKeyword) {
return switch (searchFilter) {
case "id" -> eqId(searchKeyword);
case "name" -> containsName(searchKeyword);
case "name" -> {
// avoid NPE
if (searchKeyword != null && searchKeyword.length() == 1) {
yield eqName(searchKeyword);
}
yield containsName(searchKeyword);
}
default -> null;
};
}
Expand All @@ -63,6 +69,13 @@ private BooleanExpression containsName(String name) {
return null;
}

private BooleanExpression eqName(String name) {
if (StringUtils.hasText(name)) {
return artist.name.eq(name);
}
return null;
}

public Optional<AdminArtistV1Response> findById(Long artistId) {
return fetchOne(queryFactory -> queryFactory.select(
new QAdminArtistV1Response(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,20 @@ class AdminArtistV1QueryServiceIntegrationTest extends ApplicationIntegrationTes
@Nested
class findAll {

Artist ;
Artist 베토벤;
Artist 아이유;
Artist 에픽하이;
Artist 소녀시대;

@BeforeEach
void setUp() {
= artistRepository.save(ArtistFixture.builder()
.name("벤")
.build());
베토벤 = artistRepository.save(ArtistFixture.builder()
.name("베토벤")
.build());
아이유 = artistRepository.save(ArtistFixture.builder()
.name("아이유")
.build());
Expand All @@ -77,7 +85,7 @@ void setUp() {

assertThat(response.getContent())
.map(AdminArtistV1Response::name)
.containsExactly(소녀시대.getName(), 아이유.getName(), 에픽하이.getName());
.containsExactly(베토벤.getName(), .getName(), 소녀시대.getName(), 아이유.getName(), 에픽하이.getName());
}

@Test
Expand Down Expand Up @@ -108,6 +116,20 @@ void setUp() {
.containsExactly(에픽하이.getName());
}

@Test
void 이름으로_검색할때_한_글자이면_동등_검색이_되어야_한다() {
// given
Pageable pageable = Pageable.ofSize(10);
SearchCondition searchCondition = new SearchCondition("name", "벤", pageable);

// when
var response = adminArtistV1QueryService.findAll(searchCondition);

assertThat(response.getContent())
.map(AdminArtistV1Response::name)
.containsExactly(.getName());
}

@Test
void 검색_필터가_비어있으면_필터링이_적용되지_않는다() {
// given
Expand All @@ -118,7 +140,7 @@ void setUp() {
var response = adminArtistV1QueryService.findAll(searchCondition);

assertThat(response.getContent())
.hasSize(3);
.hasSize(5);
}

@Test
Expand All @@ -131,7 +153,7 @@ void setUp() {
var response = adminArtistV1QueryService.findAll(searchCondition);

assertThat(response.getContent())
.hasSize(3);
.hasSize(5);
}

@Test
Expand All @@ -146,8 +168,8 @@ void setUp() {
// then
assertSoftly(softly -> {
softly.assertThat(response.getSize()).isEqualTo(2);
softly.assertThat(response.getTotalPages()).isEqualTo(2);
softly.assertThat(response.getTotalElements()).isEqualTo(3);
softly.assertThat(response.getTotalPages()).isEqualTo(3);
softly.assertThat(response.getTotalElements()).isEqualTo(5);
});
}
}
Expand Down

0 comments on commit c46078f

Please sign in to comment.