-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFacadeFeedService.java
More file actions
97 lines (84 loc) · 4.53 KB
/
FacadeFeedService.java
File metadata and controls
97 lines (84 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package ddingdong.ddingdongBE.domain.feed.service;
import ddingdong.ddingdongBE.domain.feed.entity.Feed;
import ddingdong.ddingdongBE.domain.feed.repository.FeedCommentRepository;
import ddingdong.ddingdongBE.domain.feed.repository.dto.FeedCountDto;
import ddingdong.ddingdongBE.domain.feed.service.dto.query.ClubFeedPageQuery;
import ddingdong.ddingdongBE.domain.feed.service.dto.query.ClubProfileQuery;
import ddingdong.ddingdongBE.domain.feed.service.dto.query.FeedCommentQuery;
import ddingdong.ddingdongBE.domain.feed.service.dto.query.FeedFileInfoQuery;
import ddingdong.ddingdongBE.domain.feed.service.dto.query.FeedListQuery;
import ddingdong.ddingdongBE.domain.feed.service.dto.query.FeedQuery;
import ddingdong.ddingdongBE.domain.feed.service.dto.query.FeedPageQuery;
import ddingdong.ddingdongBE.domain.feed.service.dto.query.PagingQuery;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class FacadeFeedService {
private final FeedService feedService;
private final FeedFileService feedFileService;
private final FeedCommentService feedCommentService;
private final FeedCommentRepository feedCommentRepository;
public ClubFeedPageQuery getFeedPageByClub(Long clubId, int size, Long currentCursorId) {
Slice<Feed> feedPage = feedService.getFeedPageByClubId(clubId, size, currentCursorId);
if (feedPage == null) {
return ClubFeedPageQuery.createEmpty();
}
List<Feed> completeFeeds = feedPage.getContent();
List<FeedListQuery> feedListQueries = buildFeedListQueriesWithCounts(completeFeeds);
PagingQuery pagingQuery = PagingQuery.of(currentCursorId, completeFeeds, feedPage.hasNext());
return ClubFeedPageQuery.of(feedListQueries, pagingQuery);
}
public FeedPageQuery getAllFeedPage(int size, Long currentCursorId) {
Slice<Feed> feedPage = feedService.getAllFeedPage(size, currentCursorId);
if (feedPage == null) {
return FeedPageQuery.createEmpty();
}
List<Feed> completeFeeds = feedPage.getContent();
List<FeedListQuery> feedListQueries = buildFeedListQueriesWithCounts(completeFeeds);
PagingQuery pagingQuery = PagingQuery.of(currentCursorId, completeFeeds, feedPage.hasNext());
return FeedPageQuery.of(feedListQueries, pagingQuery);
}
@Transactional
public FeedQuery getById(Long feedId) {
feedService.incrementViewCount(feedId);
Feed feed = feedService.getById(feedId);
ClubProfileQuery clubProfileQuery = feedFileService.extractClubInfo(feed.getClub());
FeedFileInfoQuery feedFileInfoQuery = feedFileService.extractFeedFileInfo(feed);
long likeCount = feed.getLikeCount();
long commentCount = feedCommentService.countByFeedId(feedId);
List<FeedCommentQuery> comments = feedCommentService.getAllByFeedId(feedId);
return FeedQuery.of(feed, clubProfileQuery, feedFileInfoQuery, likeCount, commentCount, comments);
}
private List<FeedListQuery> buildFeedListQueriesWithCounts(List<Feed> feeds) {
List<FeedListQuery> feedListQueries = feeds.stream()
.map(feedFileService::extractFeedThumbnailInfo)
.toList();
List<Long> feedIds = feeds.stream().map(Feed::getId).toList();
if (feedIds.isEmpty()) {
return feedListQueries;
}
Map<Long, Long> likeCountMap = feeds.stream()
.collect(Collectors.toMap(Feed::getId, Feed::getLikeCount));
Map<Long, Long> commentCountMap = feedCommentRepository.countsByFeedIds(feedIds).stream()
.collect(Collectors.toMap(FeedCountDto::getFeedId, FeedCountDto::getCnt));
return feedListQueries.stream()
.map(q -> FeedListQuery.builder()
.id(q.id())
.thumbnailCdnUrl(q.thumbnailCdnUrl())
.thumbnailOriginUrl(q.thumbnailOriginUrl())
.feedType(q.feedType())
.thumbnailFileName(q.thumbnailFileName())
.viewCount(q.viewCount())
.likeCount(likeCountMap.getOrDefault(q.id(), 0L))
.commentCount(commentCountMap.getOrDefault(q.id(), 0L))
.build())
.toList();
}
}