Skip to content

Commit

Permalink
Merge pull request #45 from AlongTheBlue/develop
Browse files Browse the repository at this point in the history
[Feat] restaurant 검색 pagination 구현
  • Loading branch information
MoonInbae authored Oct 13, 2024
2 parents 8fabcf9 + 845792c commit 3fc7729
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.alongtheblue.alongtheblue_server.global.data.restaurant;

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;
Expand All @@ -13,7 +14,8 @@ public interface RestaurantRepository extends JpaRepository<Restaurant, Long> {
@Query("SELECT r FROM Restaurant r JOIN r.images i GROUP BY r HAVING COUNT(i) > 0")
Page<SimpleInformation> findAllSimple(Pageable pageable);

List<Restaurant> findByTitleContaining(String title);
@Query("SELECT r FROM Restaurant r JOIN r.images i WHERE r.title LIKE %:title% GROUP BY r HAVING COUNT(i) > 0")
Page<SearchInformation> findByTitleContaining(String title, Pageable pageable);

Page<Restaurant> findAll(Pageable pageable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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.restaurant.dto.response.PartRestaurantResponseDto;
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.error.ErrorCode;
Expand Down Expand Up @@ -463,23 +464,36 @@ public ApiResponse<Restaurant> getRestaurant(Long id) {
// else return null;
}

public ApiResponse<List<PartRestaurantResponseDto>> getRestaurantsByKeyword(String keyword) {
List<Restaurant> optionalRestaurants = restaurantRepository.findByTitleContaining(keyword);
List<PartRestaurantResponseDto> partRestaurantResponseDtoList = new ArrayList<>();
for(Restaurant restaurant: optionalRestaurants) {
String[] arr = restaurant.getAddress().substring(8).split(" ");
PartRestaurantResponseDto restaurantResponseDto = new PartRestaurantResponseDto(
arr[0] + " " + arr[1],
restaurant.getTitle(),
restaurant.getContentId(),
restaurant.getImages().isEmpty() ? null : restaurant.getImages().get(0).getOriginimgurl(),
restaurant.getXMap(),
restaurant.getYMap(),
"restaurant"
);
partRestaurantResponseDtoList.add(restaurantResponseDto);
}
return ApiResponse.ok("음식점 정보를 성공적으로 검색했습니다.", partRestaurantResponseDtoList);
public ApiResponse<CustomPage<SearchInformation>> getRestaurantsByKeyword(String keyword, int page, int size) {
Pageable pageable = PageRequest.of(page, size);

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

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

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


// List<SearchInformation> optionalRestaurants = restaurantRepository.findByTitleContaining(keyword);
// List<PartRestaurantResponseDto> partRestaurantResponseDtoList = new ArrayList<>();
// for(Restaurant restaurant: optionalRestaurants) {
// String[] arr = restaurant.getAddress().substring(8).split(" ");
// PartRestaurantResponseDto restaurantResponseDto = new PartRestaurantResponseDto(
// arr[0] + " " + arr[1],
// restaurant.getTitle(),
// restaurant.getContentId(),
// restaurant.getImages().isEmpty() ? null : restaurant.getImages().get(0).getOriginimgurl(),
// restaurant.getXMap(),
// restaurant.getYMap(),
// "restaurant"
// );
// partRestaurantResponseDtoList.add(restaurantResponseDto);
// }
// return ApiResponse.ok("음식점 정보를 성공적으로 검색했습니다.", partRestaurantResponseDtoList);
}

public ApiResponse<List<HomeResponseDto>> getHomeRestaurant() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.alongtheblue.alongtheblue_server.global.data.blue.BlueResponseDto;
import org.alongtheblue.alongtheblue_server.global.data.cafe.CafeService;
import org.alongtheblue.alongtheblue_server.global.data.cafe.dto.PartCafeResponseDto;
import org.alongtheblue.alongtheblue_server.global.data.global.CustomPage;
import org.alongtheblue.alongtheblue_server.global.data.restaurant.RestaurantService;
import org.alongtheblue.alongtheblue_server.global.data.restaurant.dto.response.PartRestaurantResponseDto;
import org.alongtheblue.alongtheblue_server.global.data.tourData.TourDataService;
Expand Down Expand Up @@ -47,14 +48,16 @@ public ApiResponse<List<PartRestaurantResponseDto>> searchRestaurants() {
}

@GetMapping("/restaurant")
public ApiResponse<List<PartRestaurantResponseDto>> searchRestaurantsByKeyword(@RequestParam String keyword) {
return restaurantService.getRestaurantsByKeyword(keyword);
public ApiResponse<CustomPage<SearchInformation>> searchRestaurantsByKeyword(@RequestParam String keyword,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
return restaurantService.getRestaurantsByKeyword(keyword, page, size);
}

@GetMapping("/all")
public ApiResponse<List<SearchResponseDto>> searchAllCategoryByKeyword(@RequestParam String keyword){
return searchService.searchAllCategoryByKeyword(keyword);
}
// @GetMapping("/all")
// public ApiResponse<List<SearchResponseDto>> searchAllCategoryByKeyword(@RequestParam String keyword){
// return searchService.searchAllCategoryByKeyword(keyword);
// }

@GetMapping("/all/list")
public ApiResponse<List<SearchResponseDto>> getAllCategory() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.alongtheblue.alongtheblue_server.global.data.search;

import java.util.List;

public interface SearchInformation<T> {
String getContentId();
String getTitle();
String getAddress();
String getXMap();
String getYMap();
List<T> getImages();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ public class SearchService {
private final TourDataRepository tourDataRepository;
private final AccommodationRepository accommodationRepository;

public ApiResponse<List<SearchResponseDto>> searchAllCategoryByKeyword(String keyword){
List<SearchResponseDto> searchResponseDtoList = new ArrayList<>();
List<Blue> blues = blueRepository.findByNameContaining(keyword);
List<Restaurant> restaurants = restaurantRepository.findByTitleContaining(keyword);
List<Cafe> cafes = cafeRepository.findByTitleContaining(keyword);
List<TourData> tourDataList = tourDataRepository.findByTitleContaining(keyword);
List<Accommodation> accommodations = accommodationRepository.findByTitleContaining(keyword);

addSearchResponseDto(searchResponseDtoList, blues, restaurants, cafes, tourDataList, accommodations);
return ApiResponse.ok("모든 카테고리에서 검색을 성공하였습니다.", searchResponseDtoList);
}
// public ApiResponse<List<SearchResponseDto>> searchAllCategoryByKeyword(String keyword){
// List<SearchResponseDto> searchResponseDtoList = new ArrayList<>();
// List<Blue> blues = blueRepository.findByNameContaining(keyword);
// List<Restaurant> restaurants = restaurantRepository.findByTitleContaining(keyword);
// List<Cafe> cafes = cafeRepository.findByTitleContaining(keyword);
// List<TourData> tourDataList = tourDataRepository.findByTitleContaining(keyword);
// List<Accommodation> accommodations = accommodationRepository.findByTitleContaining(keyword);
//
// addSearchResponseDto(searchResponseDtoList, blues, restaurants, cafes, tourDataList, accommodations);
// return ApiResponse.ok("모든 카테고리에서 검색을 성공하였습니다.", searchResponseDtoList);
// }

public ApiResponse<List<SearchResponseDto>> searchAllCategory() {
List<SearchResponseDto> searchResponseDtoList = new ArrayList<>();
Expand Down

0 comments on commit 3fc7729

Please sign in to comment.