Skip to content
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
6 changes: 5 additions & 1 deletion backend/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 23 additions & 35 deletions backend/src/main/java/_team/onmyway/controller/RouteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.function.Function;

@RestController
@RequestMapping("/route")
Expand All @@ -28,47 +29,34 @@ public class RouteController {
private final ObjectMapper objectMapper;

@PostMapping("/findOut")
public ResponseEntity<?> getFindOutRoute(@RequestBody List<PositionDTO> positions) {
if (positions.isEmpty()) return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
PositionDTO start = positions.get(0);

RouteResponseDTO routing = routeService.findOutRoute(positions).block();
Mono<AllCategoryRecommendationsDTO> recommendations = recommendationService.recommendByRoute(routing, start.getLat(), start.getLon());

ObjectNode response = objectMapper.createObjectNode();
response.set("route", objectMapper.valueToTree(routing));
response.set("recommendations", objectMapper.valueToTree(recommendations));

return new ResponseEntity<>(response, HttpStatus.OK);
public Mono<ResponseEntity<?>> getFindOutRoute(@RequestBody List<PositionDTO> positions) {
return processRouteAndRecommend(positions, routeService::findOutRoute);
}

@PostMapping("/right")
public ResponseEntity<?> getRightRoute(@RequestBody List<PositionDTO> positions) {
if (positions.isEmpty()) return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
PositionDTO start = positions.get(0);

RouteResponseDTO routing = routeService.rightRoute(positions).block();
Mono<AllCategoryRecommendationsDTO> recommendations = recommendationService.recommendByRoute(routing, start.getLat(), start.getLon());

ObjectNode response = objectMapper.createObjectNode();
response.set("route", objectMapper.valueToTree(routing));
response.set("recommendations", objectMapper.valueToTree(recommendations));

return new ResponseEntity<>(response, HttpStatus.OK);
public Mono<ResponseEntity<?>> getRightRoute(@RequestBody List<PositionDTO> positions) {
return processRouteAndRecommend(positions, routeService::rightRoute);
}

@PostMapping("/slow")
public ResponseEntity<?> getRoute(@RequestBody List<PositionDTO> positions) {
if (positions.isEmpty()) return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
PositionDTO start = positions.get(0);

RouteResponseDTO routing = routeService.slowRoute(positions).block();
Mono<AllCategoryRecommendationsDTO> recommendations = recommendationService.recommendByRoute(routing, start.getLat(), start.getLon());

ObjectNode response = objectMapper.createObjectNode();
response.set("route", objectMapper.valueToTree(routing));
response.set("recommendations", objectMapper.valueToTree(recommendations));
public Mono<ResponseEntity<?>> getRoute(@RequestBody List<PositionDTO> positions) {
return processRouteAndRecommend(positions, routeService::slowRoute);
}

return new ResponseEntity<>(response, HttpStatus.OK);
// 비동기 로직 한 번에 묶기
private Mono<ResponseEntity<?>> processRouteAndRecommend(
List<PositionDTO> positions,
Function<List<PositionDTO>, Mono<RouteResponseDTO>> processer) {
if (positions.isEmpty()) return Mono.just(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
PositionDTO start = positions.get(0);
return processer.apply(positions)
.flatMap(routing -> recommendationService.recommendByRoute(routing, start.getLat(), start.getLon())
.map(recommendations -> {
ObjectNode response = objectMapper.createObjectNode();
response.set("route", objectMapper.valueToTree(routing));
response.set("recommendations", objectMapper.valueToTree(recommendations));

return new ResponseEntity<>(response, HttpStatus.OK);
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface PhotosRepository extends JpaRepository<Photos, Long> {
boolean existsByPlaceId(Long placeId);

Optional<Photos> findFirstByPlaceId(Long placeId);
public List<Photos> findByPlace(Place place);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import _team.onmyway.entity.Place;
import _team.onmyway.entity.ServiceCategory;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

Expand Down Expand Up @@ -40,12 +41,14 @@ List<Place> findRandomByCategoryInRadius(
int limit
);

@EntityGraph(attributePaths = {"workingTimes", "serviceCategory"})
// JPQL로 해결
@Query(value = """
SELECT * FROM place p
WHERE p.service_category_id IN :categoryIds
SELECT p FROM Place p
WHERE p.serviceCategory.id IN :categoryIds
AND p.lat BETWEEN :minLat AND :maxLat
AND p.lng BETWEEN :minLng AND :maxLng
""", nativeQuery = true)
""")
List<Place> findByBoundingBox(
List<Long> categoryIds,
double minLat,
Expand Down
6 changes: 3 additions & 3 deletions backend/src/main/java/_team/onmyway/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public ImageService(PhotosRepository photosRepository) {
}

public Mono<String> getImageURL(Place p) {
List<Photos> dbPhotos = p.getPhotos();
if (dbPhotos.size() > 0) {
return Mono.just(dbPhotos.get(0).getPhotoURL());
boolean hasPhoto = photosRepository.existsById(p.getId());
if (hasPhoto) {
return Mono.just(photosRepository.findFirstByPlaceId(p.getId()).get().getPhotoURL());
} else {
return webClient.get()
.uri(uri -> uri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.jdbc.Work;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -105,10 +106,18 @@ public Mono<AllCategoryRecommendationsDTO> recommendByRoute(RouteResponseDTO rou
.flatMap(p ->
imageService.getImageURL(p)
.map(imageURL -> {
WorkingTime workingTime = p.getWorkingTimes().get(day%7);
return toPlaceRecommendationDTO(p, userLat, userLng,
workingTime.isClosed(), workingTime.getOpenTime(),
workingTime.getCloseTime(), imageURL);
List<WorkingTime> workingTimes = p.getWorkingTimes();
int index = day&7;

if (workingTimes != null & !workingTimes.isEmpty() && index < workingTimes.size()) {
WorkingTime workingTime = workingTimes.get(index);
return toPlaceRecommendationDTO(p, userLat, userLng,
workingTime.isClosed(), workingTime.getOpenTime(),
workingTime.getCloseTime(), imageURL);
} else {
return toPlaceRecommendationDTO(p, userLat, userLng, true,
null, null, imageURL);
}
})
)
.collectList()
Expand Down Expand Up @@ -249,17 +258,29 @@ private Mono<CategoryRecommendationDTO> recommendSingleCategory(double lat, doub
return Flux.fromIterable(places)
.flatMap(place -> {
List<WorkingTime> placeWorkingTime = place.getWorkingTimes();
WorkingTime workingTime = placeWorkingTime.get(day%7);

// 2. imageService.getImageURL(place)가 Mono<String>을 반환한다고 가정
return imageService.getImageURL(place)
.map(imageURL -> toPlaceRecommendationDTO(
place, lat, lng,
workingTime.isClosed(),
workingTime.getOpenTime(),
workingTime.getCloseTime(),
imageURL
));
if (placeWorkingTime.isEmpty()) {
return imageService.getImageURL(place)
.map(imageURL -> toPlaceRecommendationDTO(
place, lat, lng,
true,
null,
null,
imageURL
));
}
else {
WorkingTime workingTime = placeWorkingTime.get(day%7);

// 2. imageService.getImageURL(place)가 Mono<String>을 반환한다고 가정
return imageService.getImageURL(place)
.map(imageURL -> toPlaceRecommendationDTO(
place, lat, lng,
workingTime.isClosed(),
workingTime.getOpenTime(),
workingTime.getCloseTime(),
imageURL
));
}
})
.collectList() // 3. 비동기로 생성된 DTO들을 다시 List로 모음
.map(placeInfos -> {
Expand Down
Loading