-
Notifications
You must be signed in to change notification settings - Fork 1
feat : 트렌딩 게시글 조회 api 추가(#128) #129
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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/ftm/server/adapter/in/web/post/controller/GetTrendingPostsController.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,29 @@ | ||
| package com.ftm.server.adapter.in.web.post.controller; | ||
|
|
||
| import com.ftm.server.adapter.in.web.post.dto.response.GetTrendingPostsResponse; | ||
| import com.ftm.server.application.port.in.post.LoadTrendingPostsUseCase; | ||
| import com.ftm.server.common.response.ApiResponse; | ||
| import com.ftm.server.common.response.enums.SuccessResponseCode; | ||
| import java.util.List; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class GetTrendingPostsController { | ||
|
|
||
| private final LoadTrendingPostsUseCase loadTrendingPostsUseCase; | ||
|
|
||
| @GetMapping("/api/posts/trend") | ||
| public ResponseEntity<ApiResponse> getTrendingPosts() { | ||
| List<GetTrendingPostsResponse> result = | ||
| loadTrendingPostsUseCase.execute().stream() | ||
| .map(GetTrendingPostsResponse::from) | ||
| .toList(); | ||
| return ResponseEntity.status(HttpStatus.OK) | ||
| .body(ApiResponse.success(SuccessResponseCode.OK, result)); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/ftm/server/adapter/in/web/post/dto/response/GetTrendingPostsResponse.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,29 @@ | ||
| package com.ftm.server.adapter.in.web.post.dto.response; | ||
|
|
||
| import com.ftm.server.application.vo.post.TrendingPostVo; | ||
| import com.ftm.server.common.consts.PropertiesHolder; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class GetTrendingPostsResponse { | ||
| private final Long postId; | ||
| private final Integer ranking; | ||
| private final String title; | ||
| private final Integer viewCount; | ||
| private final Integer likeCount; | ||
| private final Long scrapCount; | ||
| private final String imageUrl; | ||
|
|
||
| public static GetTrendingPostsResponse from(TrendingPostVo vo) { | ||
| String imageUrl = | ||
| vo.getImageUrl() == null ? PropertiesHolder.POST_DEFAULT_IMAGE : vo.getImageUrl(); | ||
| return new GetTrendingPostsResponse( | ||
| vo.getPostId(), | ||
| vo.getRank(), | ||
| vo.getTitle(), | ||
| vo.getViewCount(), | ||
| vo.getLikeCount(), | ||
| vo.getScrapCount(), | ||
| PropertiesHolder.CDN_PATH + "/" + imageUrl); | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
src/main/java/com/ftm/server/adapter/out/cache/LoadTrendingPostsWithCacheAdapter.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,94 @@ | ||
| package com.ftm.server.adapter.out.cache; | ||
|
|
||
| import static com.ftm.server.common.consts.StaticConsts.TRENDING_POSTS_CACHE_KEY_ALL; | ||
| import static com.ftm.server.common.consts.StaticConsts.TRENDING_POSTS_CACHE_NAME; | ||
|
|
||
| import com.ftm.server.application.port.out.cache.LoadTrendingPostsWithCachePort; | ||
| import com.ftm.server.application.port.out.persistence.post.LoadPostImagePort; | ||
| import com.ftm.server.application.port.out.persistence.post.LoadPostWithBookmarkCountPort; | ||
| import com.ftm.server.application.query.FindByIdsQuery; | ||
| import com.ftm.server.application.query.FindPostsByCreatedDateQuery; | ||
| import com.ftm.server.application.vo.post.PostWithBookmarkCountVo; | ||
| import com.ftm.server.application.vo.post.TrendingPostVo; | ||
| import com.ftm.server.common.annotation.Adapter; | ||
| import java.time.LocalDate; | ||
| import java.util.Comparator; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.IntStream; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.cache.annotation.CacheConfig; | ||
| import org.springframework.cache.annotation.Cacheable; | ||
|
|
||
| @Adapter | ||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| @CacheConfig(cacheManager = "trendingPostsCacheManager") | ||
| public class LoadTrendingPostsWithCacheAdapter implements LoadTrendingPostsWithCachePort { | ||
|
|
||
| private final LoadPostWithBookmarkCountPort loadPostPort; | ||
| private final LoadPostImagePort loadPostImagePort; | ||
|
|
||
| private static final int N = 15; | ||
|
|
||
| @Override | ||
| @Cacheable(value = TRENDING_POSTS_CACHE_NAME, key = TRENDING_POSTS_CACHE_KEY_ALL) | ||
| public List<TrendingPostVo> loadTrendingPosts() { | ||
|
|
||
| log.info("오잉 캐시 실행 안됨!!"); | ||
| // 현재보다 1주일 이전에 작성된 게시물만 조회(북마크 조회수 포함) (예전 게시물은 포함x) | ||
| List<PostWithBookmarkCountVo> rawPosts = | ||
| loadPostPort.loadAllPostsWithBookmarkCount( | ||
| FindPostsByCreatedDateQuery.of(LocalDate.now())); | ||
|
|
||
| if (rawPosts.isEmpty()) return List.of(); | ||
|
|
||
| // 1. 최대값 계산 (정규화를 위해) | ||
| long maxView = 1, maxLike = 1, maxScrap = 1; | ||
| for (PostWithBookmarkCountVo post : rawPosts) { | ||
| maxView = Math.max(maxView, post.getViewCount()); | ||
| maxLike = Math.max(maxLike, post.getLikeCount()); | ||
| maxScrap = Math.max(maxScrap, post.getScrapCount()); | ||
| } | ||
|
|
||
| // 2. 점수 계산 후 정렬 | ||
| long finalMaxView = maxView; | ||
| long finalMaxLike = maxLike; | ||
| long finalMaxScrap = maxScrap; | ||
| List<PostWithBookmarkCountVo> topN = | ||
| rawPosts.stream() | ||
| .sorted( | ||
| Comparator.comparingDouble( | ||
| p -> | ||
| -((double) p.getViewCount() / finalMaxView | ||
| + (double) p.getLikeCount() | ||
| / finalMaxLike | ||
| + (double) p.getScrapCount() | ||
| / finalMaxScrap) | ||
| / 3.0)) | ||
| .limit(N) | ||
| .toList(); | ||
|
|
||
| // 3. 각 게시물 이미지 조회 (없으면 null) | ||
| List<Long> postIds = topN.stream().map(PostWithBookmarkCountVo::getPostId).toList(); | ||
|
|
||
| Map<Long, String> imageMap = new HashMap<>(); | ||
| postIds.forEach(p -> imageMap.put(p, null)); | ||
| loadPostImagePort | ||
| .loadRepresentativeImagesByPostIds(FindByIdsQuery.from(postIds)) | ||
| .forEach( | ||
| postImage -> imageMap.put(postImage.getPostId(), postImage.getObjectKey())); | ||
|
|
||
| // 4. 결과 조합 (순위 부여) | ||
| return IntStream.range(0, topN.size()) | ||
| .mapToObj( | ||
| i -> { | ||
| PostWithBookmarkCountVo post = topN.get(i); | ||
| String imageKey = imageMap.get(post.getPostId()); | ||
| return TrendingPostVo.from(post, i + 1, imageKey); // rank = i+1 | ||
| }) | ||
| .toList(); | ||
| } | ||
| } | ||
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
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
10 changes: 10 additions & 0 deletions
10
...a/com/ftm/server/adapter/out/persistence/repository/PostWithBookmarkCustomRepository.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,10 @@ | ||
| package com.ftm.server.adapter.out.persistence.repository; | ||
|
|
||
| import com.ftm.server.application.query.FindPostsByCreatedDateQuery; | ||
| import com.ftm.server.application.vo.post.PostWithBookmarkCountVo; | ||
| import java.util.List; | ||
|
|
||
| public interface PostWithBookmarkCustomRepository { | ||
|
|
||
| List<PostWithBookmarkCountVo> findAllPostsWithBookmarkCount(FindPostsByCreatedDateQuery query); | ||
| } |
44 changes: 44 additions & 0 deletions
44
...m/ftm/server/adapter/out/persistence/repository/PostWithBookmarkCustomRepositoryImpl.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,44 @@ | ||
| package com.ftm.server.adapter.out.persistence.repository; | ||
|
|
||
| import static com.ftm.server.adapter.out.persistence.model.QBookmarkJpaEntity.bookmarkJpaEntity; | ||
| import static com.ftm.server.adapter.out.persistence.model.QPostJpaEntity.postJpaEntity; | ||
|
|
||
| import com.ftm.server.application.query.FindPostsByCreatedDateQuery; | ||
| import com.ftm.server.application.vo.post.PostWithBookmarkCountVo; | ||
| import com.querydsl.core.types.Projections; | ||
| import com.querydsl.jpa.impl.JPAQueryFactory; | ||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| @RequiredArgsConstructor | ||
| public class PostWithBookmarkCustomRepositoryImpl implements PostWithBookmarkCustomRepository { | ||
|
|
||
| private final JPAQueryFactory queryFactory; | ||
|
|
||
| @Override | ||
| public List<PostWithBookmarkCountVo> findAllPostsWithBookmarkCount( | ||
| FindPostsByCreatedDateQuery query) { | ||
|
|
||
| LocalDateTime twoWeeksAgo = | ||
| query.getDate().minusWeeks(1).atStartOfDay(); // 현재 기준 1주일 전 게시물만 조회 | ||
|
|
||
| return queryFactory | ||
| .select( | ||
| Projections.constructor( | ||
| PostWithBookmarkCountVo.class, | ||
| postJpaEntity.id, | ||
| postJpaEntity.title, | ||
| postJpaEntity.viewCount, | ||
| postJpaEntity.likeCount, | ||
| bookmarkJpaEntity.id.count())) | ||
| .from(postJpaEntity) | ||
| .leftJoin(bookmarkJpaEntity) | ||
| .on(bookmarkJpaEntity.post.eq(postJpaEntity)) | ||
| .where(postJpaEntity.createdAt.goe(twoWeeksAgo)) | ||
| .groupBy(postJpaEntity.id) | ||
| .fetch(); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/ftm/server/application/port/in/post/LoadTrendingPostsUseCase.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,10 @@ | ||
| package com.ftm.server.application.port.in.post; | ||
|
|
||
| import com.ftm.server.application.vo.post.TrendingPostVo; | ||
| import com.ftm.server.common.annotation.UseCase; | ||
| import java.util.List; | ||
|
|
||
| @UseCase | ||
| public interface LoadTrendingPostsUseCase { | ||
| List<TrendingPostVo> execute(); | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/ftm/server/application/port/out/cache/LoadTrendingPostsWithCachePort.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,9 @@ | ||
| package com.ftm.server.application.port.out.cache; | ||
|
|
||
| import com.ftm.server.application.vo.post.TrendingPostVo; | ||
| import java.util.List; | ||
|
|
||
| public interface LoadTrendingPostsWithCachePort { | ||
|
|
||
| List<TrendingPostVo> loadTrendingPosts(); | ||
| } |
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
9 changes: 9 additions & 0 deletions
9
...a/com/ftm/server/application/port/out/persistence/post/LoadPostWithBookmarkCountPort.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,9 @@ | ||
| package com.ftm.server.application.port.out.persistence.post; | ||
|
|
||
| import com.ftm.server.application.query.FindPostsByCreatedDateQuery; | ||
| import com.ftm.server.application.vo.post.PostWithBookmarkCountVo; | ||
| import java.util.List; | ||
|
|
||
| public interface LoadPostWithBookmarkCountPort { | ||
| List<PostWithBookmarkCountVo> loadAllPostsWithBookmarkCount(FindPostsByCreatedDateQuery query); | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/ftm/server/application/query/FindPostsByCreatedDateQuery.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,13 @@ | ||
| package com.ftm.server.application.query; | ||
|
|
||
| import java.time.LocalDate; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class FindPostsByCreatedDateQuery { | ||
| private final LocalDate date; | ||
|
|
||
| public static FindPostsByCreatedDateQuery of(LocalDate date) { | ||
| return new FindPostsByCreatedDateQuery(date); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/ftm/server/application/service/post/LoadTrendingPostsService.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,22 @@ | ||
| package com.ftm.server.application.service.post; | ||
|
|
||
| import com.ftm.server.application.port.in.post.LoadTrendingPostsUseCase; | ||
| import com.ftm.server.application.port.out.cache.LoadTrendingPostsWithCachePort; | ||
| import com.ftm.server.application.vo.post.TrendingPostVo; | ||
| import java.util.*; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class LoadTrendingPostsService implements LoadTrendingPostsUseCase { | ||
|
|
||
| private final LoadTrendingPostsWithCachePort loadTrendingPostsWithCachePort; | ||
|
|
||
| @Override | ||
| @Transactional(readOnly = true) | ||
| public List<TrendingPostVo> execute() { | ||
| return loadTrendingPostsWithCachePort.loadTrendingPosts(); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/ftm/server/application/vo/post/PostWithBookmarkCountVo.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.ftm.server.application.vo.post; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| public class PostWithBookmarkCountVo { | ||
| private final Long postId; | ||
| private final String title; | ||
| private final Integer viewCount; | ||
| private final Integer likeCount; | ||
| private final Long scrapCount; | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/ftm/server/application/vo/post/TrendingPostVo.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,26 @@ | ||
| package com.ftm.server.application.vo.post; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class TrendingPostVo { | ||
|
|
||
| private final Long postId; | ||
| private final Integer rank; | ||
| private final String title; | ||
| private final Integer viewCount; | ||
| private final Integer likeCount; | ||
| private final Long scrapCount; | ||
| private final String imageUrl; | ||
|
|
||
| public static TrendingPostVo from(PostWithBookmarkCountVo vo, Integer rank, String imageUrl) { | ||
| return new TrendingPostVo( | ||
| vo.getPostId(), | ||
| rank, | ||
| vo.getTitle(), | ||
| vo.getViewCount(), | ||
| vo.getLikeCount(), | ||
| vo.getScrapCount(), | ||
| imageUrl); | ||
| } | ||
| } |
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.
😁
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.
앗.. 못 지웠네요…ㅎㅎ