diff --git a/src/main/java/hyu/erica/capstone/repository/PreferAttractionRepository.java b/src/main/java/hyu/erica/capstone/repository/PreferAttractionRepository.java index 1a00e8b..09569ae 100644 --- a/src/main/java/hyu/erica/capstone/repository/PreferAttractionRepository.java +++ b/src/main/java/hyu/erica/capstone/repository/PreferAttractionRepository.java @@ -11,4 +11,5 @@ public interface PreferAttractionRepository extends JpaRepository findAllByTripPlanId(Long tripPlanId); List findByTripPlanIdAndIsPreferTrue(Long tripPlanId); boolean existsByAttraction_ContentIdAndUserId(Long attractionId, Long userId); + boolean existsByTripPlanIdAndAttractionContentId(Long tripPlanId, Long attractionId); } diff --git a/src/main/java/hyu/erica/capstone/repository/PreferRestaurantRepository.java b/src/main/java/hyu/erica/capstone/repository/PreferRestaurantRepository.java index dd441ec..f11e3a1 100644 --- a/src/main/java/hyu/erica/capstone/repository/PreferRestaurantRepository.java +++ b/src/main/java/hyu/erica/capstone/repository/PreferRestaurantRepository.java @@ -10,4 +10,5 @@ public interface PreferRestaurantRepository extends JpaRepository findAllByTripPlanId(Long tripPlanId); List findByTripPlanIdAndIsPreferTrue(Long tripPlanId); boolean existsByRestaurantIdAndUserId(Long restaurantId, Long userId); + boolean existsByTripPlanIdAndRestaurantId(Long tripPlanId, Long restaurantId); } diff --git a/src/main/java/hyu/erica/capstone/service/tripPlan/TripPlanCommandService.java b/src/main/java/hyu/erica/capstone/service/tripPlan/TripPlanCommandService.java index f91e409..6e9dfa5 100644 --- a/src/main/java/hyu/erica/capstone/service/tripPlan/TripPlanCommandService.java +++ b/src/main/java/hyu/erica/capstone/service/tripPlan/TripPlanCommandService.java @@ -11,4 +11,8 @@ public interface TripPlanCommandService { Long confirmRestaurantRecommendation(Long tripPlanId, SaveRestaurantRequestDTO request); void updatePlan(UpdateAllScheduleOrderRequest request); + + void editAttractionRecommendation(Long tripPlansId, SaveAttractionRequestDTO request); + + void editRestaurantRecommendation(Long tripPlansId, SaveRestaurantRequestDTO request); } diff --git a/src/main/java/hyu/erica/capstone/service/tripPlan/impl/TripPlanCommandServiceImpl.java b/src/main/java/hyu/erica/capstone/service/tripPlan/impl/TripPlanCommandServiceImpl.java index bf233aa..fa3a7b6 100644 --- a/src/main/java/hyu/erica/capstone/service/tripPlan/impl/TripPlanCommandServiceImpl.java +++ b/src/main/java/hyu/erica/capstone/service/tripPlan/impl/TripPlanCommandServiceImpl.java @@ -24,11 +24,9 @@ import hyu.erica.capstone.web.dto.tripPlan.request.UpdateAllScheduleOrderRequest.ScheduleOrderItemDTO; import java.util.ArrayList; import java.util.Collections; -import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Random; import java.util.Set; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; @@ -117,6 +115,62 @@ public void updatePlan(UpdateAllScheduleOrderRequest request) { } } + @Override + public void editAttractionRecommendation(Long tripPlansId, SaveAttractionRequestDTO request) { + if (!tripPlanRepository.existsById(tripPlansId)) { + throw new GeneralException(ErrorStatus._TRIP_PLAN_NOT_FOUND); + } + + List preferAttractions = preferAttractionRepository.findAllByTripPlanId(tripPlansId); + + for (PreferAttraction preferAttraction : preferAttractions) { + if (!request.attractionIds().contains(preferAttraction.getAttraction().getContentId())) { + preferAttraction.setPrefer(false); + } + } + + // 입력 받은 것 중, 기존 DB에 없는 것들은 새로 추가. + for (Long attractionId : request.attractionIds()) { + if (!preferAttractionRepository.existsByTripPlanIdAndAttractionContentId(tripPlansId, attractionId)) { + PreferAttraction preferAttraction = PreferAttraction.builder() + .tripPlan(tripPlanRepository.getReferenceById(tripPlansId)) + .attraction(attractionRepository.getReferenceById(attractionId)) + .isPrefer(true) + .build(); + preferAttractionRepository.save(preferAttraction); + } + } + } + + + @Override + public void editRestaurantRecommendation(Long tripPlansId, SaveRestaurantRequestDTO request) { + if (!tripPlanRepository.existsById(tripPlansId)) { + throw new GeneralException(ErrorStatus._TRIP_PLAN_NOT_FOUND); + } + + List preferRestaurants = preferRestaurantRepository.findAllByTripPlanId(tripPlansId); + + for (PreferRestaurant preferRestaurant : preferRestaurants) { + if (!request.restaurantIds().contains(preferRestaurant.getRestaurant().getId())) { + preferRestaurant.setPrefer(false); + } + } + + // 입력 받은 것 중, 기존 DB에 없는 것들은 새로 추가. + for (Long restaurantId : request.restaurantIds()) { + if (!preferRestaurantRepository.existsByTripPlanIdAndRestaurantId(tripPlansId, restaurantId)) { + PreferRestaurant preferRestaurant = PreferRestaurant.builder() + .tripPlan(tripPlanRepository.getReferenceById(tripPlansId)) + .restaurant(restaurantRepository.getReferenceById(restaurantId)) + .isPrefer(true) + .build(); + preferRestaurantRepository.save(preferRestaurant); + } + } + + } + private void createTripPlanFinal(Long tripPlanId) { TripPlan tripPlan = tripPlanRepository.findById(tripPlanId) .orElseThrow(() -> new GeneralException(ErrorStatus._TRIP_PLAN_NOT_FOUND)); diff --git a/src/main/java/hyu/erica/capstone/web/controller/TripPlanController.java b/src/main/java/hyu/erica/capstone/web/controller/TripPlanController.java index 7873932..72c8d95 100644 --- a/src/main/java/hyu/erica/capstone/web/controller/TripPlanController.java +++ b/src/main/java/hyu/erica/capstone/web/controller/TripPlanController.java @@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -132,6 +133,40 @@ public ApiResponse viewRestaurantDetail( return ApiResponse.onSuccess(SuccessStatus._OK, tripPlanQueryService.getRecommendRestaurantDetail(restaurantId)); } + // 선택지 편집 + @Tag(name = "선택지 확인", description = "선택지 확인 API") + @Operation(summary = "추천 여행지 편집", description = """ + ### 추천 여행지 편집 API + + ### Path Variables + - tripPlansId: 여행 계획 ID + """) + @PutMapping("/{tripPlansId}/attractions/edit") + public ApiResponse editPlaces( + @PathVariable Long tripPlansId, + @RequestBody SaveAttractionRequestDTO request) { + // 선택지 편집 + tripPlanCommandService.editAttractionRecommendation(tripPlansId, request); + return ApiResponse.onSuccess(SuccessStatus._OK); + } + + // 음식점 편집 + @Tag(name = "선택지 확인", description = "선택지 확인 API") + @Operation(summary = "추천 음식점 편집", description = """ + ### 추천 음식점 편집 API + + ### Path Variables + - tripPlansId: 여행 계획 ID + """) + @PutMapping("/{tripPlansId}/restaurants/edit") + public ApiResponse editRestaurants( + @PathVariable Long tripPlansId, + @RequestBody SaveRestaurantRequestDTO request) { + // 음식점 편집 + tripPlanCommandService.editRestaurantRecommendation(tripPlansId, request); + return ApiResponse.onSuccess(SuccessStatus._OK); + } + // 음식점 키워드 검색 @Tag(name = "선택지 확인", description = "선택지 확인 API")