Skip to content

Commit

Permalink
feat: 장소 검색결과 조회 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
qzzloz committed Aug 10, 2024
1 parent 512cddc commit 83488ca
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/example/locavel/converter/PlaceConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.example.locavel.domain.PlaceImg;
import com.example.locavel.domain.Places;
import com.example.locavel.domain.Reviews;
import com.example.locavel.domain.ReviewImg;
import com.example.locavel.domain.enums.Category;
import com.example.locavel.domain.enums.Region;
import com.example.locavel.web.dto.PlaceDTO.PlaceRequestDTO;
Expand Down Expand Up @@ -136,4 +137,25 @@ public static List<PlaceResponseDTO.FilterPlaceDTO> toRecommendPlace(List<Places
))
.collect(Collectors.toList());
}

public static PlaceResponseDTO.SearchResultPlaceDTO toSearchResultPlaceDTO(Places place) {
List<String> reviewImgList = place.getReviewList().stream()
.flatMap(review -> review.getReviewImgList().stream())
.map(ReviewImg::getImgUrl)
.collect(Collectors.toList());

return PlaceResponseDTO.SearchResultPlaceDTO.builder()
.placeId(place.getId())
.name(place.getName())
.rating(place.getRating())
.reviewCount(place.getReviewList().size())
.reviewImgList(reviewImgList)
.build();
}

public static List<PlaceResponseDTO.SearchResultPlaceDTO> toSearchResultPlaceListDTO(List<Places> placesList) {
return placesList.stream()
.map(PlaceConverter::toSearchResultPlaceDTO)
.collect(Collectors.toList());
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/example/locavel/domain/Places.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import jakarta.persistence.*;
import lombok.*;

import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@Setter
Expand Down Expand Up @@ -43,4 +46,7 @@ public class Places extends BaseEntity {
private Float rating;

private String telephoneNumber;

@OneToMany(mappedBy = "place")
private List<Reviews> reviewList = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ List<Places> findPlacesInRange(
List<Places> findNearbyPlaces(@Param("latitude")double latitude,
@Param("longitude")double longitude,
double radius);

@Query("SELECT p FROM Places p " +
"WHERE p.name LIKE %:keyword% " +
"OR p.description LIKE %:keyword% " +
"OR p.address LIKE %:keyword% ")
List<Places> searchByKeyword(@Param("keyword") String keyword);


}
4 changes: 4 additions & 0 deletions src/main/java/com/example/locavel/service/PlaceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,8 @@ public List<Places> getFilterPlaces(String category) {
public List<Places> recommendNearbyPlaces(double latitude, double longitude, double radius) {
return placeRepository.findNearbyPlaces(latitude, longitude, radius);
}

public List<Places> searchPlaces(String keyword) {
return placeRepository.searchByKeyword(keyword);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ public ApiResponse<PlaceResponseDTO.FilterPlaceListDTO> getFilterPlaceList(@Requ
return ApiResponse.onSuccess(PlaceConverter.toFilterPlaceListDTO(places, reviewsLists, reviewImgLists));
}

@GetMapping("/api/places/search-results")
public ApiResponse<List<PlaceResponseDTO.FilterPlaceDTO>> searchPlace(@RequestParam double latitude, // 사용자의 위도
@GetMapping("/api/places/recommend-results")
@Operation(summary = "장소 검색 조회(내 주변 추천 장소) API", description = "검색어 입력 전 내 주변 추천 장소 목록을 조회합니다.")
public ApiResponse<List<PlaceResponseDTO.FilterPlaceDTO>> recommendPlace(@RequestParam double latitude, // 사용자의 위도
@RequestParam double longitude) { // 사용자의 경도
double radius = 200;
List<Places> places = placeService.recommendNearbyPlaces(latitude, longitude, radius);
Expand All @@ -101,4 +102,12 @@ public ApiResponse<List<PlaceResponseDTO.FilterPlaceDTO>> searchPlace(@RequestPa
return ApiResponse.onSuccess(PlaceConverter.toRecommendPlace(places, reviewsLists, reviewImgLists));
}

@GetMapping("/api/places/search-results")
@Operation(summary = "장소 검색 결과 조회 API", description = "검색 결과 장소 목록을 조회합니다.")
public ApiResponse<List<PlaceResponseDTO.SearchResultPlaceDTO>> searchPlace(@RequestParam String keyword) {
List<Places> places = placeService.searchPlaces(keyword);
return ApiResponse.onSuccess(PlaceConverter.toSearchResultPlaceListDTO(places));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,17 @@ public static class FilterPlaceListDTO{
String category;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class SearchResultPlaceDTO{
Long placeId;
String name;
Float rating;
int reviewCount;
List<String> reviewImgList;
}


}

0 comments on commit 83488ca

Please sign in to comment.