Skip to content

Commit

Permalink
refactor: GymRepository에 querydsl 적용 (#275) (#283)
Browse files Browse the repository at this point in the history
* refactor: GymRepository에 querydsl 적용 (#274)

* refactor: List를 반환하도록 변경 (#274)

* test: test 코드 추가 (#274)

* style: 변수명 변경 (#274)

* style: 변수명 변경 (#274)

* test: test 로직 변경 (#277)

* fix: 정규 표현식 변경 (#274)
  • Loading branch information
Sangwook02 authored Dec 4, 2023
1 parent d443b67 commit 23b0f4a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

import com.newfit.reservation.domains.gym.domain.Gym;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;

public interface GymRepository extends JpaRepository<Gym, Long> {
@Query(value = "select * from gym where name ~* :keywordString", nativeQuery = true)
List<Gym> findAllByNameContaining(@Param("keywordString") String keywordString);
public interface GymRepository extends JpaRepository<Gym, Long>, GymRepositoryCustom {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.newfit.reservation.domains.gym.repository;

import com.newfit.reservation.domains.gym.domain.Gym;
import java.util.List;

public interface GymRepositoryCustom {

List<Gym> findAllByNameContaining(List<String> keywordString);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.newfit.reservation.domains.gym.repository;

import com.newfit.reservation.domains.gym.domain.Gym;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import java.util.List;

import static com.newfit.reservation.domains.gym.domain.QGym.*;

@RequiredArgsConstructor
public class GymRepositoryImpl implements GymRepositoryCustom {
private final JPAQueryFactory queryFactory;

@Override
public List<Gym> findAllByNameContaining(List<String> keywords) {
return queryFactory
.selectFrom(gym)
.where(containsKeyword(keywords))
.fetch();
}

private BooleanBuilder containsKeyword(List<String> keywords) {
return keywords.stream()
.map(keyword -> gym.name.toLowerCase().contains(keyword.toLowerCase()))
.reduce(new BooleanBuilder(), BooleanBuilder::or, BooleanBuilder::or);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.newfit.reservation.domains.gym.repository.GymRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
Expand All @@ -14,19 +16,18 @@ public class GymService {
private final GymRepository gymRepository;

public GymListResponse searchGyms(String gymName) {
String keywordString = processQueryParam(gymName);
List<Gym> findGyms = gymRepository.findAllByNameContaining(keywordString);
List<String> keywords = processQueryParam(gymName);
List<Gym> findGyms = gymRepository.findAllByNameContaining(keywords);
List<GymResponse> gyms = findGyms.stream()
.map(GymResponse::new).toList();

return GymListResponse.createResponse(gyms);
}

private String processQueryParam(String gymName) {
if (gymName == null) {
return "()";
private List<String> processQueryParam(String gymName) {
if (gymName == null || gymName.trim().equals("헬스장")) {
return new ArrayList<>();
}
String processedGymName = gymName.replace("헬스장", "").trim().replaceAll("\s+", "|");
return "(" + processedGymName + ")";
return Arrays.stream(gymName.replace("헬스장", "").trim().split("\s+")).toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.newfit.reservation.domains.gym.repository;

import com.newfit.reservation.common.config.QuerydslConfig;
import com.newfit.reservation.domains.gym.domain.Gym;
import com.newfit.reservation.domains.gym.dto.request.admin.CreateGymRequest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Assertions.*;

@DataJpaTest
@Import(QuerydslConfig.class)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class GymRepositoryTest {

Expand All @@ -35,9 +39,31 @@ void caseInsensitiveSearchTest() {
gymRepository.save(gym);

// when
List<Gym> gyms = gymRepository.findAllByNameContaining("(sad|gym)");
List<Gym> gyms = gymRepository.findAllByNameContaining(Arrays.asList("sAd", "gym"));

// then
assertThat(gyms.size()).isEqualTo(1);
}

@Test
void 헬스장_키워드로_검색() {
// given
CreateGymRequest request = new CreateGymRequest();
request.setName("Sad but happy Gym");
request.setTel("010-1235-4565");
request.setAddress("마포구 와우산로");
request.setOpenAt(LocalTime.MIN);
request.setCloseAt(LocalTime.MAX);
request.setAllDay(false);

Gym gym = Gym.from(request);
gymRepository.save(gym);

// when
List<Gym> findBySad = gymRepository.findAllByNameContaining(List.of("Sad"));
List<Gym> findByHappy = gymRepository.findAllByNameContaining(List.of("Happy"));

// then
assertThat(findBySad.size()).isEqualTo(findByHappy.size());
}
}

0 comments on commit 23b0f4a

Please sign in to comment.