Skip to content

Commit

Permalink
✨ Feat: 키워드별 추천 강좌 조회 시, topword 기준으로 검색 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yxhwxn committed Jun 13, 2024
1 parent 7ead425 commit 83e2c7b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
24 changes: 19 additions & 5 deletions src/main/java/khu/bigdata/infou/business/LectureConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand All @@ -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<LectureUdemy> lectureUdemyList) {
public static LectureResponseDTO.KeywordRecommendLectureDto toKeywordRecommendLectureDto(
List<LectureUdemy> lectureUdemyList, List<LectureTag> lectureTagList) {

Map<Integer, LectureTag> lectureTagMap = lectureTagList.stream()
.collect(Collectors.toMap(LectureTag::getLectureUdemyId, tag -> tag));

List<LectureResponseDTO.KeywordRecommendLectureInfo> 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()
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/khu/bigdata/infou/implement/LectureService.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,26 @@ public LectureResponseDTO.KeywordRecommendLectureDto findRecommendedLectureByKey
throw new IllegalArgumentException("Keyword must not be null or empty");
}

// 키워드를 포함하는 강의 목록을 조회(topic 데이터에 있는지 확인)
List<LectureUdemy> lectureUdemyList = lectureUdemyRepository.findAllByTopic(keyword);
// 키워드를 포함하는 LectureTag 목록 조회
List<LectureTag> lectureTags = lectureTagRepository.findByTopword1ContainingOrTopword2ContainingOrTopword3ContainingOrTopword4ContainingOrTopword5Containing(
keyword, keyword, keyword, keyword, keyword);

// LectureTag 목록에서 lectureId 추출
List<Integer> lectureIds = lectureTags.stream()
.map(LectureTag::getLectureUdemyId)
.collect(Collectors.toList());

// lectureId 목록을 사용하여 LectureUdemy 목록 조회
List<LectureUdemy> lectureUdemyList = lectureUdemyRepository.findByLectureIdIn(lectureIds);

// 조회된 강의 목록을 AvgRating과 NumReviews의 값을 곱한 값으로 내림차순 정렬
List<LectureUdemy> sortedList = lectureUdemyList.stream()
.sorted(Comparator.comparingDouble((LectureUdemy lecture) -> lecture.getAvgRating() * lecture.getNumReviews()).reversed())
.limit(1000) // 1000개로 제한
.collect(Collectors.toList());

// 조회된 강의 목록을 DTO로 변환하여 반환
return LectureConverter.toKeywordRecommendLectureDto(sortedList);
// 조회된 강의 목록과 대응되는 LectureTag를 매핑하여 DTO로 변환
return LectureConverter.toKeywordRecommendLectureDto(sortedList, lectureTags);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LectureTag, Long> {

Optional<LectureTag> findByLectureUdemyId(Integer lectureId);

List<LectureTag> findByTopword1ContainingOrTopword2ContainingOrTopword3ContainingOrTopword4ContainingOrTopword5Containing(
String keyword1, String keyword2, String keyword3, String keyword4, String keyword5);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface LectureUdemyRepository extends JpaRepository<LectureUdemy, Long

List<LectureUdemy> findAllBySubcategory(String subcategory);

List<LectureUdemy> findAllByTopic(String topic);
List<LectureUdemy> findByLectureIdIn(List<Integer> lectureIdList);

Optional<LectureUdemy> findByLectureId(Integer lectureId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 83e2c7b

Please sign in to comment.