-
Notifications
You must be signed in to change notification settings - Fork 0
feat(#345): 내가 찜한 브랜드 및 캠페인 조회 추가 #352
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/com/example/RealMatch/user/application/service/UserFavoriteService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package com.example.RealMatch.user.application.service; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import com.example.RealMatch.brand.domain.entity.BrandDescribeTag; | ||
| import com.example.RealMatch.brand.domain.repository.BrandDescribeTagRepository; | ||
| import com.example.RealMatch.match.domain.entity.enums.BrandSortType; | ||
| import com.example.RealMatch.match.domain.entity.enums.CampaignSortType; | ||
| import com.example.RealMatch.user.domain.repository.UserFavoriteBrandQueryRepository; | ||
| import com.example.RealMatch.user.domain.repository.UserFavoriteCampaignQueryRepository; | ||
| import com.example.RealMatch.user.presentation.dto.response.FavoriteBrandDto; | ||
| import com.example.RealMatch.user.presentation.dto.response.FavoriteBrandListResponseDto; | ||
| import com.example.RealMatch.user.presentation.dto.response.FavoriteCampaignDto; | ||
| import com.example.RealMatch.user.presentation.dto.response.FavoriteCampaignListResponseDto; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional(readOnly = true) | ||
| public class UserFavoriteService { | ||
|
|
||
| private final UserFavoriteCampaignQueryRepository userFavoriteCampaignQueryRepository; | ||
| private final UserFavoriteBrandQueryRepository userFavoriteBrandQueryRepository; | ||
| private final BrandDescribeTagRepository brandDescribeTagRepository; | ||
|
|
||
| /* ===== 찜한 브랜드 ===== */ | ||
| public FavoriteBrandListResponseDto getMyFavoriteBrands( | ||
| Long userId, | ||
| BrandSortType sortType | ||
| ) { | ||
| List<FavoriteBrandDto> brands = | ||
| userFavoriteBrandQueryRepository.findFavoriteBrands(userId, sortType); | ||
|
|
||
| if (brands.isEmpty()) { | ||
| return FavoriteBrandListResponseDto.empty(); | ||
| } | ||
|
|
||
| List<Long> brandIds = brands.stream() | ||
| .map(FavoriteBrandDto::getBrandId) | ||
| .toList(); | ||
|
|
||
| List<BrandDescribeTag> tags = | ||
| brandDescribeTagRepository.findAllByBrandIdIn(brandIds); | ||
|
|
||
| Map<Long, List<String>> tagMap = tags.stream() | ||
| .collect(Collectors.groupingBy( | ||
| tag -> tag.getBrand().getId(), | ||
| Collectors.mapping( | ||
| BrandDescribeTag::getBrandDescribeTag, | ||
| Collectors.toList() | ||
| ) | ||
| )); | ||
|
|
||
| brands.forEach(dto -> | ||
| dto.setTags(tagMap.getOrDefault(dto.getBrandId(), List.of())) | ||
| ); | ||
|
|
||
| return FavoriteBrandListResponseDto.builder() | ||
| .count(brands.size()) | ||
| .brands(brands) | ||
| .build(); | ||
| } | ||
|
|
||
| /* ===== 찜한 캠페인 ===== */ | ||
| public FavoriteCampaignListResponseDto getMyFavoriteCampaigns( | ||
| Long userId, | ||
| CampaignSortType sortType | ||
| ) { | ||
| List<FavoriteCampaignDto> campaigns = | ||
| userFavoriteCampaignQueryRepository.findFavoriteCampaigns(userId, sortType); | ||
|
|
||
| if (campaigns.isEmpty()) { | ||
| return FavoriteCampaignListResponseDto.empty(); | ||
| } | ||
|
|
||
| return FavoriteCampaignListResponseDto.builder() | ||
| .count(campaigns.size()) | ||
| .campaigns(campaigns) | ||
| .build(); | ||
| } | ||
| } | ||
5 changes: 5 additions & 0 deletions
5
src/main/java/com/example/RealMatch/user/domain/entity/enums/FavoriteType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package com.example.RealMatch.user.domain.entity.enums; | ||
|
|
||
| public enum FavoriteType { | ||
| BRAND, CAMPAIGN | ||
| } |
14 changes: 14 additions & 0 deletions
14
...n/java/com/example/RealMatch/user/domain/repository/UserFavoriteBrandQueryRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.example.RealMatch.user.domain.repository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import com.example.RealMatch.match.domain.entity.enums.BrandSortType; | ||
| import com.example.RealMatch.user.presentation.dto.response.FavoriteBrandDto; | ||
|
|
||
| public interface UserFavoriteBrandQueryRepository { | ||
|
|
||
| List<FavoriteBrandDto> findFavoriteBrands( | ||
| Long userId, | ||
| BrandSortType sortType | ||
| ); | ||
| } |
114 changes: 114 additions & 0 deletions
114
...in/java/com/example/RealMatch/user/domain/repository/UserFavoriteBrandRepositoryImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| package com.example.RealMatch.user.domain.repository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import com.example.RealMatch.brand.domain.entity.QBrand; | ||
| import com.example.RealMatch.brand.domain.entity.QBrandLike; | ||
| import com.example.RealMatch.match.domain.entity.QMatchBrandHistory; | ||
| import com.example.RealMatch.match.domain.entity.enums.BrandSortType; | ||
| import com.example.RealMatch.user.presentation.dto.response.FavoriteBrandDto; | ||
| import com.querydsl.core.types.Expression; | ||
| import com.querydsl.core.types.OrderSpecifier; | ||
| import com.querydsl.core.types.Projections; | ||
| import com.querydsl.core.types.dsl.Expressions; | ||
| import com.querydsl.jpa.JPAExpressions; | ||
| import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Repository | ||
| @RequiredArgsConstructor | ||
| public class UserFavoriteBrandRepositoryImpl | ||
| implements UserFavoriteBrandQueryRepository { | ||
|
|
||
| private final JPAQueryFactory queryFactory; | ||
|
|
||
| // ✅ Checkstyle 대응: static final → 대문자 상수 | ||
| private static final QBrandLike BRAND_LIKE = QBrandLike.brandLike; | ||
| private static final QBrand BRAND = QBrand.brand; | ||
| private static final QMatchBrandHistory MATCH_BRAND_HISTORY = | ||
| QMatchBrandHistory.matchBrandHistory; | ||
|
|
||
| @Override | ||
| public List<FavoriteBrandDto> findFavoriteBrands( | ||
| Long userId, | ||
| BrandSortType sortType | ||
| ) { | ||
| List<OrderSpecifier<?>> orderSpecifiers = | ||
| buildBrandOrderSpecifiers(sortType); | ||
|
|
||
| return queryFactory | ||
| .select(Projections.constructor( | ||
| FavoriteBrandDto.class, | ||
| BRAND.id, | ||
| BRAND.brandName, | ||
| BRAND.logoUrl, | ||
| brandMatchingRatioExpression(), | ||
| brandLikeCountExpression(), | ||
| Expressions.nullExpression(List.class) // tags는 나중에 채움 | ||
| )) | ||
| .from(BRAND_LIKE) | ||
| .join(BRAND_LIKE.brand, BRAND) | ||
| .leftJoin(MATCH_BRAND_HISTORY).on( | ||
| MATCH_BRAND_HISTORY.brand.id.eq(BRAND.id), | ||
| MATCH_BRAND_HISTORY.user.id.eq(userId), | ||
| MATCH_BRAND_HISTORY.isDeprecated.eq(false) | ||
| ) | ||
| .where(BRAND_LIKE.user.id.eq(userId)) | ||
| .orderBy(orderSpecifiers.toArray(new OrderSpecifier[0])) | ||
| .fetch(); | ||
| } | ||
|
|
||
| private List<OrderSpecifier<?>> buildBrandOrderSpecifiers( | ||
| BrandSortType sortType | ||
| ) { | ||
| if (sortType == null) { | ||
| sortType = BrandSortType.MATCH_SCORE; | ||
| } | ||
|
|
||
| return switch (sortType) { | ||
| case MATCH_SCORE -> List.of( | ||
| MATCH_BRAND_HISTORY.matchingRatio.desc().nullsLast(), | ||
| BRAND.id.asc() | ||
| ); | ||
|
|
||
| case POPULARITY -> List.of( | ||
| popularityDesc(), | ||
| MATCH_BRAND_HISTORY.matchingRatio.desc().nullsLast(), | ||
| BRAND.id.asc() | ||
| ); | ||
|
|
||
| case NEWEST -> List.of( | ||
| BRAND.createdAt.desc(), | ||
| BRAND.id.asc() | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| private OrderSpecifier<?> popularityDesc() { | ||
| return new OrderSpecifier<>( | ||
| com.querydsl.core.types.Order.DESC, | ||
| brandLikeCountExpression(), | ||
| OrderSpecifier.NullHandling.NullsLast | ||
| ); | ||
| } | ||
|
|
||
| private Expression<Long> brandLikeCountExpression() { | ||
| QBrandLike bl = QBrandLike.brandLike; | ||
|
|
||
| return JPAExpressions | ||
| .select(bl.id.count()) | ||
| .from(bl) | ||
| .where(bl.brand.id.eq(BRAND.id)); | ||
| } | ||
|
|
||
| private Expression<Long> brandMatchingRatioExpression() { | ||
| return Expressions.numberTemplate( | ||
| Long.class, | ||
| "COALESCE({0}, 0)", | ||
| MATCH_BRAND_HISTORY.matchingRatio | ||
| ); | ||
| } | ||
|
Comment on lines
+107
to
+113
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
14 changes: 14 additions & 0 deletions
14
...ava/com/example/RealMatch/user/domain/repository/UserFavoriteCampaignQueryRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.example.RealMatch.user.domain.repository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import com.example.RealMatch.match.domain.entity.enums.CampaignSortType; | ||
| import com.example.RealMatch.user.presentation.dto.response.FavoriteCampaignDto; | ||
|
|
||
| public interface UserFavoriteCampaignQueryRepository { | ||
|
|
||
| List<FavoriteCampaignDto> findFavoriteCampaigns( | ||
| Long userId, | ||
| CampaignSortType sortType | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FavoriteBrandDto를 불변으로 변경하는 것과 연관하여,forEach를 사용해 DTO 리스트를 직접 수정하는 대신,stream().map()을 사용하여 태그 정보가 포함된 새로운 DTO 객체를 생성하는 것이 좋습니다. 이는 코드의 불변성을 유지하고 가독성을 높이는 데 도움이 됩니다.References