Skip to content

Commit

Permalink
Merge pull request #48 from AlongTheBlue/develop
Browse files Browse the repository at this point in the history
[Feat] cafe 검색 pagination 구현
  • Loading branch information
MoonInbae authored Oct 13, 2024
2 parents 85c8aa4 + 56cea3e commit 189f7e8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
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<Cafe, Long> {

@Query("SELECT c FROM Cafe c JOIN c.images i GROUP BY c HAVING COUNT(i) > 0")
Page<SimpleInformation> findAllSimple(Pageable pageable);

List<Cafe> 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<SearchInformation> findByTitleContaining(String keyword, Pageable pageable);

Optional<Cafe> findByContentId(String contentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -440,23 +441,35 @@ public ApiResponse<List<PartCafeResponseDto>> getCafesHome() {
return ApiResponse.ok("카페 정보를 성공적으로 조회했습니다.", dtos);
}

public ApiResponse<List<PartCafeResponseDto>> getCafesByKeyword(String keyword) {
List<Cafe> optionalCafes = cafeRepository.findByTitleContaining(keyword);
List<PartCafeResponseDto> 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<CustomPage<SearchInformation>> getCafesByKeyword(String keyword, int page, int size) {
Pageable pageable = PageRequest.of(page, size);

// 1. Restaurant 기준으로 페이징 처리된 데이터를 조회
Page<SearchInformation> cafePage = cafeRepository.findByTitleContaining(keyword, pageable);

// CustomPage 객체로 변환 (기존 페이지네이션 정보와 category를 함께 담음)
CustomPage<SearchInformation> customPage = new CustomPage<>(
cafePage.getContent(), pageable, cafePage.getTotalElements(), Category.CAFE.getValue());

// ApiResponse로 반환
return ApiResponse.ok("카페 목록을 성공적으로 조회했습니다.", customPage);

// List<Cafe> optionalCafes = cafeRepository.findByTitleContaining(keyword);
// List<PartCafeResponseDto> 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<List<HomeResponseDto>> getHomeCafeList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ public ApiResponse<List<PartCafeResponseDto>> getCafesHome() {
}

@GetMapping("/cafe")
public ApiResponse<List<PartCafeResponseDto>> searchCafesByKeyword(@RequestParam String keyword) {
return cafeService.getCafesByKeyword(keyword);
public ApiResponse<CustomPage<SearchInformation>> searchCafesByKeyword(@RequestParam String keyword,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
return cafeService.getCafesByKeyword(keyword, page, size);
}

@GetMapping("/tourData/list")
Expand Down

0 comments on commit 189f7e8

Please sign in to comment.