Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 여행지 근처추천여행지 기능 구현 #108

Merged
merged 6 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDto;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceMapInfoResponseDto;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceNearbyResponseDto;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceResponseDto;
import com.haejwo.tripcometrue.domain.place.service.PlaceService;
import com.haejwo.tripcometrue.global.util.ResponseDTO;
Expand Down Expand Up @@ -67,6 +68,20 @@ public ResponseEntity<ResponseDTO<List<PlaceMapInfoResponseDto>>> placeMapInfoLi
.body(responseBody);
}

@GetMapping("/{placeId}/nearby")
public ResponseEntity<ResponseDTO<List<PlaceNearbyResponseDto>>> placeNearbyList(
@PathVariable Long placeId
) {

List<PlaceNearbyResponseDto> responseDtos = placeService.findNearbyPlaceList(placeId);

ResponseDTO<List<PlaceNearbyResponseDto>> responseBody = ResponseDTO.okWithData(responseDtos);

return ResponseEntity.status(responseBody
.getCode())
.body(responseBody);
}

@GetMapping
public ResponseEntity<ResponseDTO<Page<PlaceResponseDto>>> placeList(
Pageable pageable,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.haejwo.tripcometrue.domain.place.dto.response;

import lombok.Builder;

public record PlaceNearbyResponseDto(
Long placeId,
String placeName,
String imageUrl,
Double latitude,
Double longitude,
Integer storedCount,
Integer reviewCount,
Integer commentCount
) {

@Builder
public PlaceNearbyResponseDto(Long placeId, String placeName, String imageUrl, Double latitude,
Double longitude, Integer storedCount, Integer reviewCount, Integer commentCount) {
this.placeId = placeId;
this.placeName = placeName;
this.imageUrl = imageUrl;
this.latitude = latitude;
this.longitude = longitude;
this.storedCount = storedCount;
this.reviewCount = reviewCount;
this.commentCount = commentCount;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.haejwo.tripcometrue.domain.place.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.haejwo.tripcometrue.domain.place.entity.Place;
import java.time.LocalTime;
import lombok.Builder;
Expand All @@ -9,35 +10,36 @@ public record PlaceResponseDto(
String name,
String address,
String description,
String phoneNumber,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm") LocalTime weekdayOpenTime,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm") LocalTime weekdayCloseTime,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm") LocalTime weekendOpenTime,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm") LocalTime weekendCloseTime,
Double latitude,
Double longitude,
Integer storedCount,
Long cityId
) {

@Builder
public PlaceResponseDto(
Long id,
String name,
String address,
String description,
LocalTime weekdayOpenTime,
LocalTime weekdayCloseTime,
LocalTime weekendOpenTime,
LocalTime weekendCloseTime,
Integer storedCount,
Long cityId
) {
public PlaceResponseDto(Long id, String name, String address, String description,
String phoneNumber,
@JsonFormat(shape = Shape.STRING, pattern = "HH:mm") LocalTime weekdayOpenTime,
@JsonFormat(shape = Shape.STRING, pattern = "HH:mm") LocalTime weekdayCloseTime,
@JsonFormat(shape = Shape.STRING, pattern = "HH:mm") LocalTime weekendOpenTime,
@JsonFormat(shape = Shape.STRING, pattern = "HH:mm") LocalTime weekendCloseTime,
Double latitude, Double longitude, Integer storedCount, Long cityId) {
this.id = id;
this.name = name;
this.address = address;
this.description = description;
this.phoneNumber = phoneNumber;
this.weekdayOpenTime = weekdayOpenTime;
this.weekdayCloseTime = weekdayCloseTime;
this.weekendOpenTime = weekendOpenTime;
this.weekendCloseTime = weekendCloseTime;
this.latitude = latitude;
this.longitude = longitude;
this.storedCount = storedCount;
this.cityId = cityId;
}
Expand All @@ -48,10 +50,13 @@ public static PlaceResponseDto fromEntity(Place entity) {
.name(entity.getName())
.address(entity.getAddress())
.description(entity.getDescription())
.phoneNumber(entity.getPhoneNumber())
.weekdayOpenTime(entity.getWeekdayOpenTime())
.weekdayCloseTime(entity.getWeekdayCloseTime())
.weekendOpenTime(entity.getWeekendOpenTime())
.weekendCloseTime(entity.getWeekendCloseTime())
.latitude(entity.getLatitude())
.longitude(entity.getLongitude())
.storedCount(entity.getStoredCount())
.cityId(entity.getCity().getId())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Place extends BaseTimeEntity {
@Column(nullable = false)
private String address;
private String description;
private String phoneNumber;
private LocalTime weekdayOpenTime;
private LocalTime weekdayCloseTime;
private LocalTime weekendOpenTime;
Expand All @@ -49,15 +50,15 @@ public void prePersist() {
}

@Builder
public Place(Long id, String name, String address, String description,
LocalTime weekdayOpenTime,
LocalTime weekdayCloseTime, LocalTime weekendOpenTime, LocalTime weekendCloseTime,
Double latitude, Double longitude, Integer storedCount, Integer reviewCount,
Integer commentCount, City city) {
public Place(Long id, String name, String address, String description, String phoneNumber,
LocalTime weekdayOpenTime, LocalTime weekdayCloseTime, LocalTime weekendOpenTime,
LocalTime weekendCloseTime, Double latitude, Double longitude, Integer storedCount,
Integer reviewCount, Integer commentCount, City city) {
this.id = id;
this.name = name;
this.address = address;
this.description = description;
this.phoneNumber = phoneNumber;
this.weekdayOpenTime = weekdayOpenTime;
this.weekdayCloseTime = weekdayCloseTime;
this.weekendOpenTime = weekendOpenTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.haejwo.tripcometrue.domain.city.entity.City;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceMapInfoResponseDto;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceNearbyResponseDto;
import com.haejwo.tripcometrue.domain.place.entity.Place;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -19,4 +20,6 @@ Page<Place> findPlaceWithFilter(Pageable pageable,
List<Place> findPlacesByCityAndOrderByStoredCountLimitSize(City city, int size);

List<PlaceMapInfoResponseDto> findPlaceMapInfoListByPlaceId(Long placeId);

List<PlaceNearbyResponseDto> findNearbyPlaces(Long placeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.haejwo.tripcometrue.domain.city.entity.City;
import com.haejwo.tripcometrue.domain.city.entity.QCity;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceMapInfoResponseDto;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceNearbyResponseDto;
import com.haejwo.tripcometrue.domain.place.entity.Place;
import com.haejwo.tripcometrue.domain.place.entity.QPlace;
import com.haejwo.tripcometrue.domain.triprecord.entity.QTripRecordSchedule;
Expand Down Expand Up @@ -149,6 +150,56 @@ public List<PlaceMapInfoResponseDto> findPlaceMapInfoListByPlaceId(Long placeId)

}

@Override
public List<PlaceNearbyResponseDto> findNearbyPlaces(Long placeId) {

QPlace qPlace = QPlace.place;
QTripRecordSchedule qTripRecordSchedule = QTripRecordSchedule.tripRecordSchedule;
QTripRecordScheduleImage qTripRecordScheduleImage = QTripRecordScheduleImage.tripRecordScheduleImage;

// 주어진 placeId에 해당하는 장소의 위도, 경도를 찾는다.
Place centerPlace = queryFactory
.selectFrom(qPlace)
.where(qPlace.id.eq(placeId))
.fetchOne();

// 주변 장소를 뽑아냅니다. (1도 범위 내면 보통 11km 안쪽임)
List<Place> nearbyPlaces = queryFactory
.selectFrom(qPlace)
.where(qPlace.latitude.between(centerPlace.getLatitude() - 1, centerPlace.getLatitude() + 1)
.and(qPlace.longitude.between(centerPlace.getLongitude() - 1, centerPlace.getLongitude() + 1)))
.orderBy(qPlace.storedCount.desc())
.where(qPlace.id.ne(placeId))
.limit(5)
.fetch();

// 각 장소에 해당하는 스케줄 이미지를 찾아 PlaceNearbyResponseDto 객체를 생성한다. (어차피 컨텐츠가 5개 고정이여서 이게 더 간단함)
List<PlaceNearbyResponseDto> result = nearbyPlaces.stream()
.map(place -> {
String imageUrl = queryFactory
.select(qTripRecordScheduleImage.imageUrl)
.from(qTripRecordScheduleImage)
.join(qTripRecordScheduleImage.tripRecordSchedule, qTripRecordSchedule)
.where(qTripRecordSchedule.place.id.eq(place.getId()))
.orderBy(qTripRecordScheduleImage.id.asc())
.fetchFirst();

return new PlaceNearbyResponseDto(
place.getId(),
place.getName(),
imageUrl,
place.getLatitude(),
place.getLongitude(),
place.getStoredCount(),
place.getReviewCount(),
place.getCommentCount()
);
})
.collect(Collectors.toList());

return result;
}

private OrderSpecifier<?>[] getSort(Pageable pageable) {
QPlace place = QPlace.place;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.haejwo.tripcometrue.domain.city.repository.CityRepository;
import com.haejwo.tripcometrue.domain.place.dto.request.PlaceRequestDto;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceMapInfoResponseDto;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceNearbyResponseDto;
import com.haejwo.tripcometrue.domain.place.dto.response.PlaceResponseDto;
import com.haejwo.tripcometrue.domain.place.entity.Place;
import com.haejwo.tripcometrue.domain.place.exception.PlaceNotFoundException;
Expand Down Expand Up @@ -65,6 +66,14 @@ public List<PlaceMapInfoResponseDto> findPlaceMapInfoList(Long placeId) {
return responseDtos;
}

public List<PlaceNearbyResponseDto> findNearbyPlaceList(Long placeId) {

List<PlaceNearbyResponseDto> responseDtos = placeRepository.findNearbyPlaces(placeId);

return responseDtos;

}

@Transactional
public PlaceResponseDto modifyPlace(Long placeId, PlaceRequestDto requestDto) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ public record TripRecordListResponseDto(
Integer totalDays,
Integer commentCount,
Integer storeCount,
Double averageRating,
String imageUrl,
TripRecordMemberResponseDto member
) {

@Builder
public TripRecordListResponseDto(Long tripRecordId, String title, String countries, Integer totalDays,
Integer commentCount, Integer storeCount, String imageUrl,
TripRecordMemberResponseDto member) {
public TripRecordListResponseDto(Long tripRecordId, String title, String countries,
Integer totalDays, Integer commentCount, Integer storeCount, Double averageRating,
String imageUrl, TripRecordMemberResponseDto member) {
this.tripRecordId = tripRecordId;
this.title = title;
this.countries = countries;
this.totalDays = totalDays;
this.commentCount = commentCount;
this.storeCount = storeCount;
this.averageRating = averageRating;
this.imageUrl = imageUrl;
this.member = member;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public List<TripRecordListResponseDto> finTripRecordWithFilter(
qTripRecord.totalDays,
qTripRecord.commentCount,
qTripRecord.storeCount,
qTripRecord.averageRating,
JPAExpressions
.select(qTripRecordImage.imageUrl.min())
.from(qTripRecordImage)
Expand Down
Loading