Skip to content

Commit

Permalink
Merge pull request #9 from NARAE-FLIWITH/update/tourDetail
Browse files Browse the repository at this point in the history
관광지 상세 조회에 리뷰 목록 추가, 디코딩 에러 return
  • Loading branch information
jjunjji authored May 19, 2024
2 parents bfdcb7e + 7e986c2 commit 116a41b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ public class TourController {
private final TourService tourService;
@GetMapping("/tour")
public ResponseEntity<BaseRes<List<TourType>>> getTourByType(@RequestParam String latitude, @RequestParam String longitude, @RequestParam String contentTypeId){
return ResponseEntity.ok(BaseRes.create(HttpStatus.OK.value(), "관광지 리스트를 조회하는데 성공했습니다.", tourService.getTourByType(latitude, longitude, contentTypeId)));
return ResponseEntity.ok(BaseRes.create(HttpStatus.OK.value(), "관광지 목록 조회에 성공했습니다.", tourService.getTourByType(latitude, longitude, contentTypeId)));
}

@GetMapping("/tour/{contentTypeId}/{contentId}")
public ResponseEntity<BaseRes<TourDetailRes>> getTour(@PathVariable(value = "contentTypeId")String contentTypeId, @PathVariable(value = "contentId") String contentId){
return ResponseEntity.ok(BaseRes.create(HttpStatus.OK.value(), "관광지 리스트를 조회하는데 성공했습니다.", tourService.getTour(contentTypeId, contentId)));
return ResponseEntity.ok(BaseRes.create(HttpStatus.OK.value(), "관광지 상세 조회에 성공했습니다.", tourService.getTour(contentTypeId, contentId)));

}

@GetMapping("/admin/save")
public ResponseEntity<BaseRes<Void>> saveTour(@RequestParam String contentTypeId){
tourService.saveAllSpots(contentTypeId);
return ResponseEntity.ok(BaseRes.create(HttpStatus.OK.value(), "관광지 타입 리스트를 저장하는데 성공했습니다."));
return ResponseEntity.ok(BaseRes.create(HttpStatus.OK.value(), "관광지 타입 목록을 저장하는데 성공했습니다."));
}

}
2 changes: 2 additions & 0 deletions src/main/java/com/narae/fliwith/dto/TourRes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.narae.fliwith.dto;

import com.narae.fliwith.dto.ReviewRes.ReviewItem;
import com.narae.fliwith.dto.openAPI.DetailCommonRes;
import com.narae.fliwith.dto.openAPI.DetailIntroRes;
import com.narae.fliwith.dto.openAPI.DetailWithTourRes;
Expand Down Expand Up @@ -37,6 +38,7 @@ public static class TourDetailRes{
DetailWithTourRes.Item detailWithTour;
DetailIntroRes.Item detailIntro;
DetailCommonRes.Item detailCommon;
List<ReviewItem> reviews;

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.narae.fliwith.repository;

import com.narae.fliwith.domain.Review;
import com.narae.fliwith.domain.Spot;
import com.narae.fliwith.domain.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -14,5 +15,7 @@ public interface ReviewRepository extends JpaRepository<Review, Long> {

Page<Review> findAllByUserOrderByCreatedAtDesc(User user, Pageable pageable);

Page<Review> findAllBySpotOrderByCreatedAtDesc(Spot spot, Pageable pageable);


}
29 changes: 23 additions & 6 deletions src/main/java/com/narae/fliwith/service/openAPI/TourService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.narae.fliwith.service.openAPI;

import com.narae.fliwith.domain.Location;
import com.narae.fliwith.domain.Review;
import com.narae.fliwith.domain.Spot;
import com.narae.fliwith.dto.TourRes;
import com.narae.fliwith.dto.ReviewRes.ReviewItem;
import com.narae.fliwith.dto.TourRes.TourDetailRes;
import com.narae.fliwith.dto.TourRes.TourType;
import com.narae.fliwith.dto.openAPI.*;
import com.narae.fliwith.dto.openAPI.DetailWithTourRes.Item;
import com.narae.fliwith.exception.spot.SpotFindFailException;
import com.narae.fliwith.repository.LocationRepository;
import com.narae.fliwith.repository.ReviewRepository;
import com.narae.fliwith.repository.SpotRepository;
import jakarta.transaction.Transactional;
import java.util.ArrayList;
Expand All @@ -17,6 +19,9 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.codec.DecodingException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
Expand All @@ -32,8 +37,9 @@ public class TourService {

private final SpotRepository spotRepository;
private final LocationRepository locationRepository;
private final ReviewRepository reviewRepository;

public Mono<Item> getDetailWithTour(String contentId) {
public Mono<DetailWithTourRes.Item> getDetailWithTour(String contentId) {
return webClient.get()
.uri(uriBuilder ->
uriBuilder.path("/detailWithTour1")
Expand All @@ -47,7 +53,8 @@ public Mono<Item> getDetailWithTour(String contentId) {
.retrieve()
.bodyToMono(new ParameterizedTypeReference<DetailWithTourRes.Root>() {
})
.map(root -> root.getResponse().getBody().getItems().getItem().get(0));
.map(root -> root.getResponse().getBody().getItems().getItem().get(0))
.onErrorReturn(DecodingException.class, new DetailWithTourRes.Item());

}

Expand All @@ -69,7 +76,8 @@ public Mono<DetailCommonRes.Item> getDetailCommon(String contentId) {
.retrieve()
.bodyToMono(new ParameterizedTypeReference<DetailCommonRes.Root>() {
})
.map(root -> root.getResponse().getBody().getItems().getItem().get(0));
.map(root -> root.getResponse().getBody().getItems().getItem().get(0))
.onErrorReturn(DecodingException.class, new DetailCommonRes.Item());
}

public Mono<DetailIntroRes.Item> getDetailIntro(String contentId, String contentTypeId) {
Expand All @@ -87,7 +95,8 @@ public Mono<DetailIntroRes.Item> getDetailIntro(String contentId, String content
.retrieve()
.bodyToMono(new ParameterizedTypeReference<DetailIntroRes.Root>() {
})
.map(root -> root.getResponse().getBody().getItems().getItem().get(0));
.map(root -> root.getResponse().getBody().getItems().getItem().get(0))
.onErrorReturn(DecodingException.class, new DetailIntroRes.Item());
}

public List<TourType> getTourByType(String latitude, String longitude, String contentTypeId) {
Expand Down Expand Up @@ -121,10 +130,18 @@ public TourDetailRes getTour(String contentTypeId, String contentId) {
DetailIntroRes.Item detailIntro = getDetailIntro(contentId, contentTypeId).block();
DetailCommonRes.Item detailCommon = getDetailCommon(contentId).block();

Pageable pageable = PageRequest.of(0, 10); // 0은 페이지 번호, 10은 페이지 크기
Spot spot = spotRepository.findById(Integer.parseInt(contentId)).orElseThrow(SpotFindFailException::new);

Page<Review> reviewsPage = reviewRepository.findAllBySpotOrderByCreatedAtDesc(spot, pageable);
List<Review> reviews = reviewsPage.getContent();

return TourDetailRes.builder()
.detailWithTour(detailWithTour)
.detailIntro(detailIntro)
.detailCommon(detailCommon)
.reviews(reviews.stream().map(review -> new ReviewItem(review.getId(), review.getImages().get(0).getUrl(), review.getUser().getNickname(), review.getUser().getDisability(), (long) review.getLikes().size())).collect(
Collectors.toList()))
.build();
}

Expand Down

0 comments on commit 116a41b

Please sign in to comment.