Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] API 수정 #19

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public static LectureResponseDTO.LectureDetailDto toLectureDetailDto(LectureUdem
public static LectureResponseDTO.OtherStudentsListInfo toOtherStudentInfo(PlatformStudent student) {

return LectureResponseDTO.OtherStudentsListInfo.builder()
.inflearnUserId(Long.valueOf(student.getInflearnUserId()))
.udemyUserId(Long.valueOf(student.getUdemyUserId()))
.inflearnUserId(student.getInflearnUserId() != null ? student.getInflearnUserId() : null)
.udemyUserId(student.getUdemyUserId() != null ? student.getUdemyUserId() : null)
.name(student.getName())
.topword1(student.getTopword1())
.topword2(student.getTopword2())
Expand Down
58 changes: 34 additions & 24 deletions src/main/java/khu/bigdata/infou/implement/LectureService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import khu.bigdata.infou.business.LectureConverter;
import khu.bigdata.infou.domain.LectureDetail;
import khu.bigdata.infou.domain.LectureInflearn;
import khu.bigdata.infou.domain.LectureUdemy;
import khu.bigdata.infou.domain.PlatformStudent;
import khu.bigdata.infou.repository.LectureDetailRepository;
Expand All @@ -11,11 +12,8 @@
import khu.bigdata.infou.web.dto.LectureResponseDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -37,11 +35,37 @@ public class LectureService {
public LectureResponseDTO.CategoryRecommendLectureDto findRecommendedLectureByCategory(String category) {

if (category == null) {
// 카테고리 프로그래밍 언어로 고정해서 가져와라
// default category(udemy)
category = "Programming Languages";
}

//inflearn 강좌 조회 추가

// default category(inflearn)
String categoryInflearn = "프로그래밍 언어";

String firstCategory = null;
String secondCategory = null;

categoryInflearn = switch (category) {
case "Programming Languages" -> "프로그래밍 언어";
case "Web Development" -> "웹 개발";
case "Game Development" -> "게임 개발";
case "Mobile Development" -> "모바일 개발";
case "Data Science" -> "데이터 사이언스";
default -> categoryInflearn;
};

String[] parts = categoryInflearn.split(" ");
if (parts.length == 2) {
firstCategory = parts[0];
secondCategory = parts[1];
}

List<LectureUdemy> lectureUdemyList = lectureUdemyRepository.findAllBySubcategory(category);

List<LectureInflearn> lectureInflearnList = lectureInflearnRepository.findAllByFirstCategoryAndSecondCategory(firstCategory, secondCategory);

// 일단 가져오고 상위 값 추출
List<LectureUdemy> sortedList = lectureUdemyList.stream()
.sorted(Comparator.comparingDouble((LectureUdemy lecture) -> lecture.getAvgRating() * lecture.getNumReviews()).reversed())
Expand Down Expand Up @@ -96,26 +120,12 @@ public LectureResponseDTO.LectureDetailDto findLectureDetail(Integer lectureId)
*/
public LectureResponseDTO.OtherStudentsDto findOtherStudents() {

Pageable pageable = PageRequest.of(0, 1000);

// 쿼리 실행
List<Object[]> topStudents = platformStudentRepository.findTopStudents(pageable);

// 조회된 데이터를 List로 변환
List<PlatformStudent> studentList = new ArrayList<>();
for (Object[] result : topStudents) {
Integer userId = (Integer) result[0];
List<PlatformStudent> students;
if (userId != null) {
students = platformStudentRepository.findByInflearnUserId(userId);
if (students.isEmpty()) {
students = platformStudentRepository.findByUdemyUserId(userId);
}
} else {
students = new ArrayList<>();
}
studentList.addAll(students);
}
List<PlatformStudent> platformStudents = platformStudentRepository.findAllWithUdemyUserId();

// 가져온 데이터를 List로 변환
List<PlatformStudent> studentList = platformStudents.stream()
.limit(1000) // 상위 1000개로 제한
.collect(Collectors.toList());

return LectureConverter.toOtherStudentsDto(studentList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import khu.bigdata.infou.domain.LectureInflearn;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface LectureInflearnRepository extends JpaRepository<LectureInflearn, Long> {

Optional<LectureInflearn> findByLectureId(Integer lectureId);

List<LectureInflearn> findAllByFirstCategoryAndSecondCategory(String firstCategory, String secondCategory);
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package khu.bigdata.infou.repository;

import khu.bigdata.infou.domain.PlatformStudent;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PlatformStudentRepository extends JpaRepository<PlatformStudent, Long> {

@Query("SELECT p.inflearnUserId AS userId, COUNT(p) AS cnt FROM PlatformStudent p WHERE p.inflearnUserId IS NOT NULL GROUP BY p.inflearnUserId " +
"UNION " +
"SELECT p.udemyUserId AS userId, COUNT(p) AS cnt FROM PlatformStudent p WHERE p.udemyUserId IS NOT NULL GROUP BY p.udemyUserId " +
"ORDER BY cnt DESC")
List<Object[]> findTopStudents(Pageable pageable);

List<PlatformStudent> findByInflearnUserId(Integer userId);

List<PlatformStudent> findByUdemyUserId(Integer userId);
@Query("SELECT p FROM PlatformStudent p WHERE p.udemyUserId IS NOT NULL")
List<PlatformStudent> findAllWithUdemyUserId();
}

Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public static class LectureDetailDto {
@AllArgsConstructor
public static class OtherStudentsListInfo {

private Long inflearnUserId;
private Long udemyUserId;
private Integer inflearnUserId;
private Integer udemyUserId;
private String name;

private String topword1;
Expand Down
Loading