Skip to content
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

[FIX] 유저 피드 조회 시 발생하는 NPE 해결 #189

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ public static UserFeedGetResponse of(Feed feed, Novel novel, Long visitorId) {
isLiked,
feed.getLikes().size(),
feed.getComments().size(),
novel.getNovelId(),
novel.getTitle(),
novel == null ?
null : novel.getNovelId(),
novel == null ?
null : novel.getTitle(),
novelRating,
novelRatingCount,
relevantCategories
Expand All @@ -69,21 +71,31 @@ private static List<Long> getLikeUsers(Feed feed) {
}

private static Long getNovelRatingCount(Novel novel) {
if (novel == null) {
return null;
}
return novel.getUserNovels()
.stream()
.filter(userNovel -> userNovel.getUserNovelRating() != 0.0f)
.count();
}

private static Float getNovelRatingSum(Novel novel) {
if (novel == null) {
return null;
}

return (float) novel.getUserNovels()
.stream()
.filter(userNovel -> userNovel.getUserNovelRating() != 0.0f)
.mapToDouble(UserNovel::getUserNovelRating)
.sum();
}

private static float getNovelRating(Novel novel, Long novelRatingCount) {
private static Float getNovelRating(Novel novel, Long novelRatingCount) {
if (novel == null) {
return null;
}
return novelRatingCount > 0
? getNovelRatingSum(novel) / novelRatingCount
: 0.0f;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/websoso/WSSServer/service/FeedService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -333,11 +334,12 @@ public UserFeedsGetResponse getUserFeeds(User visitor, Long ownerId, Long lastFe
: visitor.getUserId();

if (owner.getIsProfilePublic() || isOwner(visitor, ownerId)) {
List<Feed> feedsByNoOffsetPagination = feedRepository.findFeedsByNoOffsetPagination(owner, lastFeedId,
size);
List<Feed> feedsByNoOffsetPagination =
feedRepository.findFeedsByNoOffsetPagination(owner, lastFeedId, size);

List<Long> novelIds = feedsByNoOffsetPagination.stream()
.map(Feed::getNovelId)
.filter(Objects::nonNull)
.collect(Collectors.toList());
Map<Long, Novel> novelMap = novelRepository.findAllById(novelIds)
.stream()
Expand Down
Loading