Skip to content

Commit

Permalink
Merge pull request #118 from KAKAO-TOUR-API-CONTEST/devyj
Browse files Browse the repository at this point in the history
fix : 1차원 받아서 넣는걸로 변경
  • Loading branch information
yyujin1231 authored Sep 29, 2024
2 parents 5371a20 + 8639694 commit 7c7d907
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ResponseEntity<String> saveRecommendations(@RequestHeader(value = "Author
if (tokenProvider.validToken(accessToken)) {
Long userId = tokenProvider.getUserId(accessToken);
try {
recommendationService.saveRecommendations(userId, recommendRequest.getRecommendations());
recommendationService.saveRecommendations(userId, recommendRequest.getRestaurants(), recommendRequest.getLeisures(), recommendRequest.getHotels());
return ResponseEntity.ok("선택ID저장");
} catch (IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("잘못된 데이터: " + e.getMessage());
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/com/example/ai_jeju/dto/RecommendRequest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.example.ai_jeju.dto;

import java.util.List;
import lombok.Getter;
import lombok.Setter;

public class RecommendRequest {
import java.util.List;

private List<List<Long>> recommendations;

// Getter and Setter
public List<List<Long>> getRecommendations() {
return recommendations;
}
@Getter
@Setter
public class RecommendRequest {

public void setRecommendations(List<List<Long>> recommendations) {
this.recommendations = recommendations;
}
private List<Long> restaurants;
private List<Long> leisures;
private List<Long> hotels;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,34 @@ public class RecommendationService {
@Autowired
private RecommendationRepository recommendationRepository;

@Transactional // 트랜잭션 처리 추가
public void saveRecommendations(Long userId, List<List<Long>> recommendIdsList) {
@Transactional
public void saveRecommendations(Long userId, List<Long> restaurantIds, List<Long> leisureIds, List<Long> hotelIds) {
List<Recommendation> recommendations = new ArrayList<>();
for (List<Long> recommendIds : recommendIdsList) {
for (Long recommendId : recommendIds) {
recommendations.add(Recommendation.builder()
.userId(userId)
.recommendId(recommendId)
.build());
}

// 음식점 리스트 저장
for (Long restaurantId : restaurantIds) {
recommendations.add(Recommendation.builder()
.userId(userId)
.recommendId(restaurantId)
.build());
}

// 레저 리스트 저장
for (Long leisureId : leisureIds) {
recommendations.add(Recommendation.builder()
.userId(userId)
.recommendId(leisureId)
.build());
}
recommendationRepository.saveAll(recommendations); // 성능 개선을 위해 한번에 저장

// 호텔 리스트 저장
for (Long hotelId : hotelIds) {
recommendations.add(Recommendation.builder()
.userId(userId)
.recommendId(hotelId)
.build());
}

recommendationRepository.saveAll(recommendations); // 성능 개선을 위해 한 번에 저장
}
}
}

0 comments on commit 7c7d907

Please sign in to comment.