From 99c5f4c47eafde41ede044d5b0a42696b30d8eb6 Mon Sep 17 00:00:00 2001 From: seokhee Date: Fri, 20 Dec 2024 14:10:31 +0900 Subject: [PATCH] =?UTF-8?q?[#534]=20fix:=20=EC=9D=B4=EC=83=89=EC=B2=B4?= =?UTF-8?q?=ED=97=98=20=EC=B0=9C=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/FavoriteController.java | 5 +- .../repository/FavoriteRepositoryCustom.java | 4 ++ .../repository/FavoriteRepositoryImpl.java | 38 +++++++++++++++ .../favorite/service/FavoriteService.java | 47 +++++++++++++++++++ 4 files changed, 92 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/jeju/nanaland/domain/favorite/controller/FavoriteController.java b/src/main/java/com/jeju/nanaland/domain/favorite/controller/FavoriteController.java index 4c7b9962d..0843d7550 100644 --- a/src/main/java/com/jeju/nanaland/domain/favorite/controller/FavoriteController.java +++ b/src/main/java/com/jeju/nanaland/domain/favorite/controller/FavoriteController.java @@ -1,6 +1,5 @@ package com.jeju.nanaland.domain.favorite.controller; -import static com.jeju.nanaland.domain.common.data.Category.EXPERIENCE; import static com.jeju.nanaland.domain.common.data.Category.FESTIVAL; import static com.jeju.nanaland.domain.common.data.Category.MARKET; import static com.jeju.nanaland.domain.common.data.Category.NANA; @@ -8,6 +7,7 @@ import static com.jeju.nanaland.domain.common.data.Category.RESTAURANT; import static com.jeju.nanaland.global.exception.SuccessCode.POST_LIKE_TOGGLE_SUCCESS; +import com.jeju.nanaland.domain.experience.entity.enums.ExperienceType; import com.jeju.nanaland.domain.favorite.dto.FavoriteRequest.LikeToggleDto; import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse; import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse.StatusDto; @@ -66,11 +66,12 @@ public BaseResponse getAllFavorites( @GetMapping("/experience/list") public BaseResponse getExperienceFavorites( @AuthMember MemberInfoDto memberInfoDto, + @RequestParam ExperienceType experienceType, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "12") int size) { FavoriteResponse.PreviewPageDto favoritePreviewPageDto = - favoriteService.getAllCategoryFavorites(memberInfoDto, EXPERIENCE, page, size); + favoriteService.getExperienceFavorites(memberInfoDto, experienceType, page, size); return BaseResponse.success(SuccessCode.GET_FAVORITE_LIST_SUCCESS, favoritePreviewPageDto); } diff --git a/src/main/java/com/jeju/nanaland/domain/favorite/repository/FavoriteRepositoryCustom.java b/src/main/java/com/jeju/nanaland/domain/favorite/repository/FavoriteRepositoryCustom.java index 942824349..686376ae6 100644 --- a/src/main/java/com/jeju/nanaland/domain/favorite/repository/FavoriteRepositoryCustom.java +++ b/src/main/java/com/jeju/nanaland/domain/favorite/repository/FavoriteRepositoryCustom.java @@ -1,6 +1,7 @@ package com.jeju.nanaland.domain.favorite.repository; import com.jeju.nanaland.domain.common.data.Category; +import com.jeju.nanaland.domain.experience.entity.enums.ExperienceType; import com.jeju.nanaland.domain.favorite.entity.Favorite; import com.jeju.nanaland.domain.member.entity.Member; import java.util.List; @@ -14,5 +15,8 @@ public interface FavoriteRepositoryCustom { Page findAllFavoritesOrderByModifiedAtDesc(Member member, Category category, Pageable pageable); + Page findAllExperienceFavoritesOrderByModifiedAtDesc(Member member, + ExperienceType experienceType, Pageable pageable); + List findAllFavoriteToSendNotification(); } diff --git a/src/main/java/com/jeju/nanaland/domain/favorite/repository/FavoriteRepositoryImpl.java b/src/main/java/com/jeju/nanaland/domain/favorite/repository/FavoriteRepositoryImpl.java index 1db048f9f..c782a8c70 100644 --- a/src/main/java/com/jeju/nanaland/domain/favorite/repository/FavoriteRepositoryImpl.java +++ b/src/main/java/com/jeju/nanaland/domain/favorite/repository/FavoriteRepositoryImpl.java @@ -1,8 +1,10 @@ package com.jeju.nanaland.domain.favorite.repository; +import static com.jeju.nanaland.domain.experience.entity.QExperience.experience; import static com.jeju.nanaland.domain.favorite.entity.QFavorite.favorite; import com.jeju.nanaland.domain.common.data.Category; +import com.jeju.nanaland.domain.experience.entity.enums.ExperienceType; import com.jeju.nanaland.domain.favorite.entity.Favorite; import com.jeju.nanaland.domain.member.entity.Member; import com.querydsl.core.types.dsl.BooleanExpression; @@ -73,6 +75,42 @@ public Page findAllFavoritesOrderByModifiedAtDesc(Member member, Categ return PageableExecutionUtils.getPage(resultDto, pageable, countQuery::fetchOne); } + @Override + public Page findAllExperienceFavoritesOrderByModifiedAtDesc(Member member, + ExperienceType experienceType, Pageable pageable) { + Category category = Category.EXPERIENCE; + + List resultDto = queryFactory + .select(favorite) + .from(favorite) + .innerJoin(experience) + .on(favorite.post.id.eq(experience.id) + .and(experience.experienceType.eq(experienceType))) + .where( + favorite.member.eq(member), + favorite.category.eq(category), + favorite.status.eq("ACTIVE") + ) + .orderBy(favorite.modifiedAt.desc()) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .fetch(); + + JPAQuery countQuery = queryFactory + .select(favorite.count()) + .from(favorite) + .innerJoin(experience) + .on(favorite.post.id.eq(experience.id) + .and(experience.experienceType.eq(experienceType))) + .where( + favorite.member.eq(member), + favorite.category.eq(category), + favorite.status.eq("ACTIVE") + ); + + return PageableExecutionUtils.getPage(resultDto, pageable, countQuery::fetchOne); + } + @Override public List findAllFavoriteToSendNotification() { LocalDateTime now = LocalDateTime.now(); diff --git a/src/main/java/com/jeju/nanaland/domain/favorite/service/FavoriteService.java b/src/main/java/com/jeju/nanaland/domain/favorite/service/FavoriteService.java index 36870ee9b..e6c8b2e7d 100644 --- a/src/main/java/com/jeju/nanaland/domain/favorite/service/FavoriteService.java +++ b/src/main/java/com/jeju/nanaland/domain/favorite/service/FavoriteService.java @@ -4,6 +4,7 @@ import com.jeju.nanaland.domain.common.data.Language; import com.jeju.nanaland.domain.common.dto.PostPreviewDto; import com.jeju.nanaland.domain.common.service.PostService; +import com.jeju.nanaland.domain.experience.entity.enums.ExperienceType; import com.jeju.nanaland.domain.favorite.dto.FavoriteRequest; import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse; import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse.PreviewDto; @@ -120,6 +121,52 @@ public FavoriteResponse.PreviewPageDto getAllCategoryFavorites(MemberInfoDto mem .build(); } + /** + * 이색체험 찜리스트 조회 + * + * @param memberInfoDto 회원 정보 + * @param page 페이지 + * @param size 페이지 크기 + * @return FavoriteResponse.PreviewPageDto + */ + @Transactional(readOnly = true) + public FavoriteResponse.PreviewPageDto getExperienceFavorites(MemberInfoDto memberInfoDto, + ExperienceType experienceType, int page, int size) { + + Member member = memberInfoDto.getMember(); + Category category = Category.EXPERIENCE; + Language language = memberInfoDto.getLanguage(); + Pageable pageable = PageRequest.of(page, size); + + // favorite 테이블에서 해당 유저의 이색체험(액티비티, 문화예술) 찜리스트 페이지 조회 + Page favoritePage = + favoriteRepository.findAllExperienceFavoritesOrderByModifiedAtDesc(member, experienceType + , pageable); + + // 각 게시물 조회 쿼리 비동기 요청 + List> favoritePreviewFutures = favoritePage.get() + .map(favorite -> CompletableFuture.supplyAsync(() -> { + Long postId = favorite.getPost().getId(); + PostPreviewDto postPreviewDto = postService.getPostPreviewDto(postId, category, language); + return new FavoriteResponse.PreviewDto(postPreviewDto); + })) + .toList(); + + // 모든 비동기 작업이 완료될 때까지 기다린 후 각 결과를 수집 + List favoritePreviewDtos = CompletableFuture.allOf( + favoritePreviewFutures.toArray(new CompletableFuture[0]) + ).thenApply(v -> + favoritePreviewFutures.stream() + .map(CompletableFuture::join) // 순서대로 결과 가져오기 + .collect(Collectors.toList()) + ).join(); // 최종 결과 리스트 + + return FavoriteResponse.PreviewPageDto.builder() + .totalElements(favoritePage.getTotalElements()) + .data(favoritePreviewDtos) + .build(); + } + /** * 찜 등록, 취소 토글 *