Skip to content

Commit

Permalink
Merge pull request #140 from dionisos198/refactor/#136-review-nplusone
Browse files Browse the repository at this point in the history
Refactor/#136 review nplusone
  • Loading branch information
dionisos198 authored Feb 11, 2024
2 parents 57432f7 + 23bbeb9 commit b456407
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,18 +317,16 @@ private ProgramProviderListResponseDto changeOTTtoKoreanAndMakeProviderResponseD

@Override
public UserSpecificRatingResponseDto showUserSpecificRating(User user, Long programId) {
Program program = programRepository.findById(programId)
.orElseThrow(() -> new NotFoundException(ErrorCode.PROGRAM_NOT_FOUND));

Genre usersFirstGenre = userGenreRepository.find1stGenreByUserIdFetchJoin(user.getId())
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_FIRST_GENRE_NOT_FOUND))
.getGenre();

int userSpecificGenreCount = reviewRepository.countByGenreName(usersFirstGenre.getName(),
program);
programId);

Double sumRating = reviewRepository.sumReviewRatingByGenreName(usersFirstGenre.getName(),
program);
programId);

double userSpecificReviewRatingSum = (sumRating != null) ? sumRating : 0.0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,37 @@ public interface ReviewRepository extends JpaRepository<Review, Long> {

boolean existsByProgramAndUser(Program program, User user);

Optional<Review> findByProgramAndUser(Program program, User user);

List<Review> findTop4ByProgramOrderByLikeCountsDesc(Program program);
Optional<Review> findByProgramIdAndUserId(Long programId, Long userId);

// Slice ๋กœ ์ „์ฒด ๋ฆฌ๋ทฐ ๊ฐ€์ ธ์˜ค๊ธฐ
Slice<Review> findByProgram(Program program, Pageable pageable);

@Query("select r from Review r join fetch r.user where r.program.id=:programId order by r.likeCounts desc")
List<Review> findTop4ByProgramOrderByLikeCountsDescWithFetchUser(
@Param("programId") Long programId, Pageable pageable);


//Slice ๋กœ ์ „์ฒด ๋ฆฌ๋ทฐ ๊ฐ€์ ธ์˜ค๊ธฐ
@Query("select r from Review r join fetch r.user where r.program.id=:programId")
Slice<Review> findByProgramWithFetchUser(@Param("programId") Long programId, Pageable pageable);


//์ข‹์•„์š” ๋งŽ์ด ๋ฐ›์€ ์ˆœ์œผ๋กœ user first genre 3๊ฐœ, ๋‚˜๋ž‘ ์ทจํ–ฅ์ด ๋น„์Šทํ•œ user์˜ ์žฅ๋ฅด
@Query("select r from Review r where r.program=:program and r.genre=:genre order by r.likeCounts desc")
List<Review> findUserSpecific4ByProgramAndGenreOrderByLikeNumberDesc(
@Param("program") Program program, @Param("genre") String genre, Pageable pageable);
@Query("select r from Review r join fetch r.user where r.program.id=:programId and r.genre=:genre order by r.likeCounts desc")
List<Review> findUserSpecific4ByProgramAndGenreOrderByLikeNumberDescWithFetchUser(
@Param("programId") Long programId, @Param("genre") String genre, Pageable pageable);


//์ข‹์•„์š” ๋งŽ์ด ๋ฐ›์€ ์ˆœ์œผ๋กœ ์œ ์ € first genre ๊ฐ™์€ ์• ๋“ค, ๋ฒ”์œ„ ์ „์ฒด
@Query("select r from Review r where r.program=:program and r.genre=:genre")
Slice<Review> findUserSpecificByProgramAndGenre(@Param("program") Program program,
@Query("select r from Review r join fetch r.user where r.program.id=:programId and r.genre=:genre")
Slice<Review> findUserSpecificByProgramAndGenreWithFetchUser(@Param("programId") Long programId,
@Param("genre") String genre, Pageable pageable);

@Query("select count(r) from Review r where r.genre=:genre and r.program=:program")
int countByGenreName(@Param("genre") String genre, @Param("program") Program program);
@Query("select count(r) from Review r where r.genre=:genre and r.program.id=:programId")
int countByGenreName(@Param("genre") String genre, @Param("programId") Long programId);

@Query("select sum(r.rating) from Review r where r.genre=:genre and r.program=:program")
@Query("select sum(r.rating) from Review r where r.genre=:genre and r.program.id=:programId")
Double sumReviewRatingByGenreName(@Param("genre") String genreName,
@Param("program") Program program);
@Param("programId") Long programId);

List<Review> findByUserId(Long userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ public class ReviewShowProgramDetailServiceImpl implements ReviewShowProgramDeta
//๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ๋ฆฌ๋ทฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
@Override
public ReviewProgramResponseDto showMyReview(User user, Long programId) {
Program program = programRepository.findById(programId)
.orElseThrow(() -> new NotFoundException(ErrorCode.PROGRAM_NOT_FOUND));

return reviewRepository.findByProgramAndUser(program, user).map(this::makeReviewDto)
return reviewRepository.findByProgramIdAndUserId(programId, user.getId())
.map(this::makeReviewDto)
.orElse(null);

}
Expand All @@ -53,8 +52,8 @@ public FourReviewResponseWithCounts show4Review(Long programId) {
Program program = programRepository.findById(programId)
.orElseThrow(() -> new NotFoundException(ErrorCode.PROGRAM_NOT_FOUND));

List<ReviewProgramResponseDto> reviewProgramResponseDtoList = reviewRepository.findTop4ByProgramOrderByLikeCountsDesc(
program).stream().map(this::makeReviewDto).collect(
List<ReviewProgramResponseDto> reviewProgramResponseDtoList = reviewRepository.findTop4ByProgramOrderByLikeCountsDescWithFetchUser(
programId, PageRequest.of(0, 4)).stream().map(this::makeReviewDto).collect(
Collectors.toList());

int leftReviewCounts = program.getReviewCount() - 4 > 0 ? program.getReviewCount() - 4 : 0;
Expand All @@ -67,11 +66,9 @@ public FourReviewResponseWithCounts show4Review(Long programId) {

@Override
public ReviewListWithSliceInfoDto showReviewList(Long programId, Pageable pageable) {
Program program = programRepository.findById(programId)
.orElseThrow(() -> new NotFoundException(ErrorCode.PROGRAM_NOT_FOUND));

Slice<ReviewProgramResponseDto> reviewProgramResponseDtoSlice = reviewRepository.findByProgram(
program, pageable).map(r -> makeReviewDto(r));
Slice<ReviewProgramResponseDto> reviewProgramResponseDtoSlice = reviewRepository.findByProgramWithFetchUser(
programId, pageable).map(r -> makeReviewDto(r));

return new ReviewListWithSliceInfoDto(reviewProgramResponseDtoSlice.getContent(),
reviewProgramResponseDtoSlice.hasNext());
Expand All @@ -83,19 +80,16 @@ public ReviewListWithSliceInfoDto showReviewList(Long programId, Pageable pageab

public FourReviewResponseWithCounts show4UserSpecificReviewList(User user, Long programId) {

Program program = programRepository.findById(programId)
.orElseThrow(() -> new NotFoundException(ErrorCode.PROGRAM_NOT_FOUND));

Genre usersFirstGenre = userGenreRepository.findByUserAndIsFirst(user, true)
Genre usersFirstGenre = userGenreRepository.find1stGenreByUserIdFetchJoin(user.getId())
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_FIRST_GENRE_NOT_FOUND))
.getGenre();

List<ReviewProgramResponseDto> reviewProgramResponseDtoList = reviewRepository.findUserSpecific4ByProgramAndGenreOrderByLikeNumberDesc(
program, usersFirstGenre.getName(), PageRequest.of(0, 4)).stream()
List<ReviewProgramResponseDto> reviewProgramResponseDtoList = reviewRepository.findUserSpecific4ByProgramAndGenreOrderByLikeNumberDescWithFetchUser(
programId, usersFirstGenre.getName(), PageRequest.of(0, 4)).stream()
.map(r -> makeReviewDto(r)).collect(Collectors.toList());

int userSpecificGenreCount = reviewRepository.countByGenreName(usersFirstGenre.getName(),
program);
programId);

int leftCount = userSpecificGenreCount - 4 > 0 ? userSpecificGenreCount - 4 : 0;

Expand All @@ -110,15 +104,12 @@ public FourReviewResponseWithCounts show4UserSpecificReviewList(User user, Long
public ReviewListWithSliceInfoDto showUserSpecificReviewList(User user, Long programId,
Pageable pageable) {

Program program = programRepository.findById(programId)
.orElseThrow(() -> new NotFoundException(ErrorCode.PROGRAM_NOT_FOUND));

Genre usersFirstGenre = userGenreRepository.findByUserAndIsFirst(user, true)
Genre usersFirstGenre = userGenreRepository.find1stGenreByUserIdFetchJoin(user.getId())
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_FIRST_GENRE_NOT_FOUND))
.getGenre();

Slice<ReviewProgramResponseDto> reviewProgramResponseDtoSlice = reviewRepository.findUserSpecificByProgramAndGenre(
program,
Slice<ReviewProgramResponseDto> reviewProgramResponseDtoSlice = reviewRepository.findUserSpecificByProgramAndGenreWithFetchUser(
programId,
usersFirstGenre.getName(), pageable).map(r -> makeReviewDto(r));

return new ReviewListWithSliceInfoDto(reviewProgramResponseDtoSlice.getContent(),
Expand All @@ -130,10 +121,9 @@ public ReviewListWithSliceInfoDto showUserSpecificReviewList(User user, Long pro

private ReviewProgramResponseDto makeReviewDto(Review review) {

List<String> reviewTagNames = reviewReviewTagRepository.findByReviewWithReviewTagFetch(
review.getId())
.stream().map(reviewReviewTag -> reviewReviewTag.getReviewTag().getName())
.collect(Collectors.toList());
List<String> reviewTagNames = review.getReviewReviewTags().stream()
.map(reviewReviewTag -> reviewReviewTag.getReviewTag().getName()).collect(
Collectors.toList());

ReviewProgramResponseDto reviewProgramResponseDto = ReviewProgramResponseDto.builder()
.reviewTagNames(reviewTagNames)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ void saveReview() {

reviewCUDService.saveReview(user, reviewSaveDto1);

Review findReview = reviewRepository.findByProgramAndUser(program, user).get();
Review findReview = reviewRepository.findByProgramIdAndUserId(program.getId(), user.getId())
.get();

Assertions.assertThat(findReview.getContent()).isEqualTo("์•„ ์ง„์งœ ๊ฟ€์žผ์ด์˜€์–ด์š”ใ… ");

Expand All @@ -90,7 +91,8 @@ void saveReview() {

reviewCUDService.saveReview(user2, reviewSaveDto2);

Review findReview2 = reviewRepository.findByProgramAndUser(program, user2).get();
Review findReview2 = reviewRepository.findByProgramIdAndUserId(program.getId(),
user2.getId()).get();

Assertions.assertThat(program.getReviewCount()).isEqualTo(2);
Assertions.assertThat(program.getAverageRating()).isEqualTo(4);
Expand All @@ -106,7 +108,8 @@ void saveReview() {
Assertions.assertThat(program.getReviewCount()).isEqualTo(3);
Assertions.assertThat(program.getAverageRating()).isEqualTo(4);

Review findReview3 = reviewRepository.findByProgramAndUser(program, user3).get();
Review findReview3 = reviewRepository.findByProgramIdAndUserId(program.getId(),
user3.getId()).get();

User user4 = makeUser4();
ReviewSaveDto reviewSaveDto4 = new ReviewSaveDto("๋ฒ”์ฃ„๋ฌผ์ด ์•„๋‹ˆ์˜€์–ด? ์ข€ ๋…ธ์žผ", program.getId(), 1.5,
Expand Down

0 comments on commit b456407

Please sign in to comment.