diff --git a/src/main/java/org/alongtheblue/alongtheblue_server/global/data/cafe/CafeRepository.java b/src/main/java/org/alongtheblue/alongtheblue_server/global/data/cafe/CafeRepository.java index 7176623..5b5f5f0 100644 --- a/src/main/java/org/alongtheblue/alongtheblue_server/global/data/cafe/CafeRepository.java +++ b/src/main/java/org/alongtheblue/alongtheblue_server/global/data/cafe/CafeRepository.java @@ -1,12 +1,12 @@ package org.alongtheblue.alongtheblue_server.global.data.cafe; import org.alongtheblue.alongtheblue_server.global.data.global.SimpleInformation; +import org.alongtheblue.alongtheblue_server.global.data.search.SearchInformation; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; -import java.util.List; import java.util.Optional; public interface CafeRepository extends JpaRepository { @@ -14,7 +14,8 @@ public interface CafeRepository extends JpaRepository { @Query("SELECT c FROM Cafe c JOIN c.images i GROUP BY c HAVING COUNT(i) > 0") Page findAllSimple(Pageable pageable); - List findByTitleContaining(String keyword); + @Query("SELECT c FROM Cafe c JOIN c.images i WHERE c.title LIKE %:keyword% GROUP BY c HAVING COUNT(i) > 0") + Page findByTitleContaining(String keyword, Pageable pageable); Optional findByContentId(String contentId); } diff --git a/src/main/java/org/alongtheblue/alongtheblue_server/global/data/cafe/CafeService.java b/src/main/java/org/alongtheblue/alongtheblue_server/global/data/cafe/CafeService.java index 371740e..117c86c 100644 --- a/src/main/java/org/alongtheblue/alongtheblue_server/global/data/cafe/CafeService.java +++ b/src/main/java/org/alongtheblue/alongtheblue_server/global/data/cafe/CafeService.java @@ -9,6 +9,7 @@ import org.alongtheblue.alongtheblue_server.global.data.global.SimpleInformation; import org.alongtheblue.alongtheblue_server.global.data.global.dto.response.DetailResponseDto; import org.alongtheblue.alongtheblue_server.global.data.global.dto.response.HomeResponseDto; +import org.alongtheblue.alongtheblue_server.global.data.search.SearchInformation; import org.alongtheblue.alongtheblue_server.global.data.weather.WeatherResponseDto; import org.alongtheblue.alongtheblue_server.global.data.weather.WeatherService; import org.alongtheblue.alongtheblue_server.global.gpt.OpenAIService; @@ -440,23 +441,35 @@ public ApiResponse> getCafesHome() { return ApiResponse.ok("카페 정보를 성공적으로 조회했습니다.", dtos); } - public ApiResponse> getCafesByKeyword(String keyword) { - List optionalCafes = cafeRepository.findByTitleContaining(keyword); - List partCafeResponseDtoList = new ArrayList<>(); - for(Cafe cafe: optionalCafes) { - String[] arr = cafe.getAddress().substring(8).split(" "); - PartCafeResponseDto partCafeResponseDto = new PartCafeResponseDto( - arr[0] + " " + arr[1], - cafe.getTitle(), - cafe.getContentId(), - cafe.getImages().isEmpty() ? null : cafe.getImages().get(0).getOriginimgurl(), - cafe.getXMap(), - cafe.getYMap(), - "cafe" - ); - partCafeResponseDtoList.add(partCafeResponseDto); - } - return ApiResponse.ok("카페 정보를 성공적으로 검색했습니다.", partCafeResponseDtoList); + public ApiResponse> getCafesByKeyword(String keyword, int page, int size) { + Pageable pageable = PageRequest.of(page, size); + + // 1. Restaurant 기준으로 페이징 처리된 데이터를 조회 + Page cafePage = cafeRepository.findByTitleContaining(keyword, pageable); + + // CustomPage 객체로 변환 (기존 페이지네이션 정보와 category를 함께 담음) + CustomPage customPage = new CustomPage<>( + cafePage.getContent(), pageable, cafePage.getTotalElements(), Category.CAFE.getValue()); + + // ApiResponse로 반환 + return ApiResponse.ok("카페 목록을 성공적으로 조회했습니다.", customPage); + +// List optionalCafes = cafeRepository.findByTitleContaining(keyword); +// List partCafeResponseDtoList = new ArrayList<>(); +// for(Cafe cafe: optionalCafes) { +// String[] arr = cafe.getAddress().substring(8).split(" "); +// PartCafeResponseDto partCafeResponseDto = new PartCafeResponseDto( +// arr[0] + " " + arr[1], +// cafe.getTitle(), +// cafe.getContentId(), +// cafe.getImages().isEmpty() ? null : cafe.getImages().get(0).getOriginimgurl(), +// cafe.getXMap(), +// cafe.getYMap(), +// "cafe" +// ); +// partCafeResponseDtoList.add(partCafeResponseDto); +// } +// return ApiResponse.ok("카페 정보를 성공적으로 검색했습니다.", partCafeResponseDtoList); } public ApiResponse> getHomeCafeList() { diff --git a/src/main/java/org/alongtheblue/alongtheblue_server/global/data/search/SearchController.java b/src/main/java/org/alongtheblue/alongtheblue_server/global/data/search/SearchController.java index 777370c..73ab195 100644 --- a/src/main/java/org/alongtheblue/alongtheblue_server/global/data/search/SearchController.java +++ b/src/main/java/org/alongtheblue/alongtheblue_server/global/data/search/SearchController.java @@ -70,8 +70,10 @@ public ApiResponse> getCafesHome() { } @GetMapping("/cafe") - public ApiResponse> searchCafesByKeyword(@RequestParam String keyword) { - return cafeService.getCafesByKeyword(keyword); + public ApiResponse> searchCafesByKeyword(@RequestParam String keyword, + @RequestParam(defaultValue = "0") int page, + @RequestParam(defaultValue = "10") int size) { + return cafeService.getCafesByKeyword(keyword, page, size); } @GetMapping("/tourData/list")