Skip to content

Commit

Permalink
[fix] 잘못된 메서드 네이밍 수정 및 쿼리 리팩토링
Browse files Browse the repository at this point in the history
- findByRegionIdAndTitleOrContentsContaining 메서드 네이밍 오류 수정
- JPQL을 사용하여 쿼리 명확하게 재작성
  • Loading branch information
khee2 committed Jun 4, 2024
1 parent 90ccc38 commit 45585db
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.SafeNet.Backend.domain.post.entity.Category;
import com.SafeNet.Backend.domain.post.entity.Post;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -18,7 +20,7 @@ public interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findByRegion_IdOrderByBuyDateDesc(Long memberRegionId);

List<Post> findByRegion_Id(Long regionId);
List<Post> findByRegion_IdAndTitleContainingOrContentsContaining(Long regionId, String title, String contents);

@Query("SELECT p FROM Post p WHERE p.region.id = :regionId AND (p.title LIKE %:keyword% OR p.contents LIKE %:keyword%)")
List<Post> findByRegionIdAndTitleOrContentsContaining(@Param("regionId") Long regionId, @Param("keyword") String keyword);
}

Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public List<PostResponseDto> searchPostsByKeyword(String keyword, String email)
throw new PostException("검색어가 너무 깁니다. " + MAX_LENGTH + " 글자 이하로 입력해주세요.", HttpStatus.BAD_REQUEST);
}
Long memberRegionId = getMemberRegionId(email);
List<Post> posts = postRepository.findByRegion_IdAndTitleContainingOrContentsContaining(memberRegionId, keyword, keyword);
// 특정 지역의 정보이면서 제목을 포함한 경우 & 특정 지역의 정보이면서 내용을 포함한 경우
List<Post> posts = postRepository.findByRegionIdAndTitleOrContentsContaining(memberRegionId, keyword);
return getPostResponseDtos(email, posts);
}

Expand Down

0 comments on commit 45585db

Please sign in to comment.