diff --git a/src/main/java/khu/bigdata/infou/business/LectureConverter.java b/src/main/java/khu/bigdata/infou/business/LectureConverter.java index f014ff4..e1cf558 100644 --- a/src/main/java/khu/bigdata/infou/business/LectureConverter.java +++ b/src/main/java/khu/bigdata/infou/business/LectureConverter.java @@ -7,6 +7,7 @@ import khu.bigdata.infou.web.dto.LectureResponseDTO; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class LectureConverter { @@ -37,7 +38,7 @@ public static LectureResponseDTO.CategoryRecommendLectureDto toCategoryRecommend // 키워드별 추천 강좌 조회 - public static LectureResponseDTO.KeywordRecommendLectureInfo toKeywordRecommendLectureInfo(LectureUdemy lectureUdemy) { + public static LectureResponseDTO.KeywordRecommendLectureInfo toKeywordRecommendLectureInfo(LectureUdemy lectureUdemy, LectureTag lectureTag) { return LectureResponseDTO.KeywordRecommendLectureInfo.builder() .lectureId(lectureUdemy.getLectureId()) @@ -46,15 +47,28 @@ public static LectureResponseDTO.KeywordRecommendLectureInfo toKeywordRecommendL .avgRating(lectureUdemy.getAvgRating()) .thumbnail(lectureUdemy.getThumbnail()) .instructorName(lectureUdemy.getInstructorName()) - .topword1(lectureUdemy.getTopic()) - .topword2(lectureUdemy.getSubcategory()) + .topword1(lectureTag.getTopword1()) + .topword2(lectureTag.getTopword2()) + .topword3(lectureTag.getTopword3()) + .topword4(lectureTag.getTopword4()) + .topword5(lectureTag.getTopword5()) .build(); } - public static LectureResponseDTO.KeywordRecommendLectureDto toKeywordRecommendLectureDto(List lectureUdemyList) { + public static LectureResponseDTO.KeywordRecommendLectureDto toKeywordRecommendLectureDto( + List lectureUdemyList, List lectureTagList) { + + Map lectureTagMap = lectureTagList.stream() + .collect(Collectors.toMap(LectureTag::getLectureUdemyId, tag -> tag)); List lectureInfos = lectureUdemyList.stream() - .map(LectureConverter::toKeywordRecommendLectureInfo) + .map(lectureUdemy -> { + LectureTag lectureTag = lectureTagMap.get(lectureUdemy.getLectureId()); + if (lectureTag == null) { + throw new IllegalArgumentException("Matching LectureTag not found for lectureId: " + lectureUdemy.getLectureId()); + } + return toKeywordRecommendLectureInfo(lectureUdemy, lectureTag); + }) .collect(Collectors.toList()); return LectureResponseDTO.KeywordRecommendLectureDto.builder() diff --git a/src/main/java/khu/bigdata/infou/implement/LectureService.java b/src/main/java/khu/bigdata/infou/implement/LectureService.java index b57ee0a..5cfc532 100644 --- a/src/main/java/khu/bigdata/infou/implement/LectureService.java +++ b/src/main/java/khu/bigdata/infou/implement/LectureService.java @@ -79,8 +79,17 @@ public LectureResponseDTO.KeywordRecommendLectureDto findRecommendedLectureByKey throw new IllegalArgumentException("Keyword must not be null or empty"); } - // 키워드를 포함하는 강의 목록을 조회(topic 데이터에 있는지 확인) - List lectureUdemyList = lectureUdemyRepository.findAllByTopic(keyword); + // 키워드를 포함하는 LectureTag 목록 조회 + List lectureTags = lectureTagRepository.findByTopword1ContainingOrTopword2ContainingOrTopword3ContainingOrTopword4ContainingOrTopword5Containing( + keyword, keyword, keyword, keyword, keyword); + + // LectureTag 목록에서 lectureId 추출 + List lectureIds = lectureTags.stream() + .map(LectureTag::getLectureUdemyId) + .collect(Collectors.toList()); + + // lectureId 목록을 사용하여 LectureUdemy 목록 조회 + List lectureUdemyList = lectureUdemyRepository.findByLectureIdIn(lectureIds); // 조회된 강의 목록을 AvgRating과 NumReviews의 값을 곱한 값으로 내림차순 정렬 List sortedList = lectureUdemyList.stream() @@ -88,8 +97,8 @@ public LectureResponseDTO.KeywordRecommendLectureDto findRecommendedLectureByKey .limit(1000) // 1000개로 제한 .collect(Collectors.toList()); - // 조회된 강의 목록을 DTO로 변환하여 반환 - return LectureConverter.toKeywordRecommendLectureDto(sortedList); + // 조회된 강의 목록과 대응되는 LectureTag를 매핑하여 DTO로 변환 + return LectureConverter.toKeywordRecommendLectureDto(sortedList, lectureTags); } /** diff --git a/src/main/java/khu/bigdata/infou/repository/LectureTagRepository.java b/src/main/java/khu/bigdata/infou/repository/LectureTagRepository.java index 6415379..fda475e 100644 --- a/src/main/java/khu/bigdata/infou/repository/LectureTagRepository.java +++ b/src/main/java/khu/bigdata/infou/repository/LectureTagRepository.java @@ -3,9 +3,13 @@ import khu.bigdata.infou.domain.LectureTag; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; import java.util.Optional; public interface LectureTagRepository extends JpaRepository { Optional findByLectureUdemyId(Integer lectureId); + + List findByTopword1ContainingOrTopword2ContainingOrTopword3ContainingOrTopword4ContainingOrTopword5Containing( + String keyword1, String keyword2, String keyword3, String keyword4, String keyword5); } diff --git a/src/main/java/khu/bigdata/infou/repository/LectureUdemyRepository.java b/src/main/java/khu/bigdata/infou/repository/LectureUdemyRepository.java index ef9ab89..51345ef 100644 --- a/src/main/java/khu/bigdata/infou/repository/LectureUdemyRepository.java +++ b/src/main/java/khu/bigdata/infou/repository/LectureUdemyRepository.java @@ -11,7 +11,7 @@ public interface LectureUdemyRepository extends JpaRepository findAllBySubcategory(String subcategory); - List findAllByTopic(String topic); + List findByLectureIdIn(List lectureIdList); Optional findByLectureId(Integer lectureId); diff --git a/src/main/java/khu/bigdata/infou/web/dto/LectureResponseDTO.java b/src/main/java/khu/bigdata/infou/web/dto/LectureResponseDTO.java index 814efef..c46ab42 100644 --- a/src/main/java/khu/bigdata/infou/web/dto/LectureResponseDTO.java +++ b/src/main/java/khu/bigdata/infou/web/dto/LectureResponseDTO.java @@ -56,7 +56,6 @@ public static class KeywordRecommendLectureInfo { private float price; private String instructorName; - // todo: topword1 ~ 5까지 추가 -> lecture_tag 테이블에서 가져오기 private String topword1; private String topword2; private String topword3;