Skip to content
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 @@ -62,12 +62,9 @@ public FeedGetResponse getFeedById(User user, Long feedId) {
UserBasicInfo feedUserBasicInfo = getUserBasicInfo(feed.getUser());
Novel novel = getLinkedNovelOrNull(feed.getNovelId());
Boolean isLiked = isUserLikedFeed(user, feed);
List<String> relevantCategories = feed.getFeedCategories().stream()
.map(feedCategory -> feedCategory.getCategory().getCategoryName().getLabel())
.collect(Collectors.toList());
Boolean isMyFeed = isUserFeedOwner(feed.getUser(), user);

return FeedGetResponse.of(feed, feedUserBasicInfo, novel, isLiked, relevantCategories, isMyFeed);
return FeedGetResponse.of(feed, feedUserBasicInfo, novel, isLiked, isMyFeed);
}

private UserBasicInfo getUserBasicInfo(User user) {
Expand All @@ -93,19 +90,19 @@ private Boolean isUserFeedOwner(User createdUser, User user) {
}

@Transactional(readOnly = true)
public FeedsGetResponse getFeeds(User user, String category, Long lastFeedId, int size,
public FeedsGetResponse getFeeds(User user, Long lastFeedId, int size,
FeedGetOption feedGetOption) {
Long userIdOrNull = Optional.ofNullable(user).map(User::getUserId).orElse(null);

List<Genre> genres = getPreferenceGenres(user);

Slice<Feed> feeds = findFeedsByCategoryLabel(getChosenCategoryOrDefault(category), lastFeedId, userIdOrNull,
Slice<Feed> feeds = findFeedsByCategoryLabel(lastFeedId, userIdOrNull,
PageRequest.of(DEFAULT_PAGE_NUMBER, size), feedGetOption, genres);

List<FeedInfo> feedGetResponses = feeds.getContent().stream().filter(feed -> feed.isVisibleTo(userIdOrNull))
.map(feed -> createFeedInfo(feed, user)).toList();

return FeedsGetResponse.of(getChosenCategoryOrDefault(category), feeds.hasNext(), feedGetResponses);
return FeedsGetResponse.of(feeds.hasNext(), feedGetResponses);
}

private List<Genre> getPreferenceGenres(User user) {
Expand All @@ -119,26 +116,22 @@ private static String getChosenCategoryOrDefault(String category) {
return Optional.ofNullable(category).orElse(DEFAULT_CATEGORY);
}

private Slice<Feed> findFeedsByCategoryLabel(String category, Long lastFeedId, Long userId, PageRequest pageRequest,
private Slice<Feed> findFeedsByCategoryLabel(Long lastFeedId, Long userId, PageRequest pageRequest,
FeedGetOption feedGetOption, List<Genre> genres) {
return feedServiceImpl.findFeedsByCategoryLabel(category, lastFeedId, userId, pageRequest, feedGetOption,
return feedServiceImpl.findFeedsByCategoryLabel(lastFeedId, userId, pageRequest, feedGetOption,
genres);
}

private FeedInfo createFeedInfo(Feed feed, User user) {
UserBasicInfo userBasicInfo = getUserBasicInfo(feed.getUser());
Novel novel = getLinkedNovelOrNull(feed.getNovelId());
Boolean isLiked = user != null && isUserLikedFeed(user, feed);
List<String> relevantCategories = feed.getFeedCategories().stream()
.map(feedCategory -> feedCategory.getCategory().getCategoryName().getLabel())
.collect(Collectors.toList());
Boolean isMyFeed = user != null && isUserFeedOwner(feed.getUser(), user);
Integer imageCount = feedServiceImpl.countByFeedId(feed.getFeedId());
Optional<FeedImage> thumbnailImage = feedServiceImpl.findThumbnailFeedImageByFeedId(feed.getFeedId());
String thumbnailUrl = thumbnailImage.map(FeedImage::getUrl).orElse(null);

return FeedInfo.of(feed, userBasicInfo, novel, isLiked, relevantCategories, isMyFeed, thumbnailUrl, imageCount,
user);
return FeedInfo.of(feed, userBasicInfo, novel, isLiked, isMyFeed, thumbnailUrl, imageCount, user);
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package org.websoso.WSSServer.dto.feed;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.util.List;

public record FeedCreateRequest(
@NotNull(message = "카테고리는 null일 수 없습니다.")
@NotEmpty(message = "카테고리는 1개 이상 선택해야 합니다.")
List<String> relevantCategories,

@NotBlank(message = "피드 내용은 비어 있거나, 공백일 수 없습니다.")
@Size(max = 2000, message = "피드 내용은 2000자를 초과할 수 없습니다.")
String feedContent,
Expand All @@ -23,4 +17,4 @@ public record FeedCreateRequest(
@NotNull(message = "공개 여부는 null일 수 없습니다.")
Boolean isPublic
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public record FeedGetResponse(
String title,
Integer novelRatingCount,
Float novelRating,
List<String> relevantCategories,
Boolean isSpoiler,
Boolean isModified,
Boolean isMyFeed,
Expand All @@ -35,7 +34,7 @@ public record FeedGetResponse(
String novelDescription
) {
public static FeedGetResponse of(Feed feed, UserBasicInfo feedUserBasicInfo, Novel novel, Boolean isLiked,
List<String> relevantCategories, Boolean isMyFeed) {
Boolean isMyFeed) {
String title = null;
Integer novelRatingCount = null;
Float novelRating = null;
Expand Down Expand Up @@ -81,7 +80,6 @@ public static FeedGetResponse of(Feed feed, UserBasicInfo feedUserBasicInfo, Nov
title,
novelRatingCount,
novelRating,
relevantCategories,
feed.getIsSpoiler(),
!feed.getCreatedDate().equals(feed.getModifiedDate()),
isMyFeed,
Expand Down Expand Up @@ -114,4 +112,4 @@ private static Float getFeedWriterNovelRating(Novel novel, Long feedWriterId) {
.map(userNovel -> userNovel.isOnlyInterested() ? null : userNovel.getUserNovelRating())
.orElse(null);
}
}
}
7 changes: 2 additions & 5 deletions src/main/java/org/websoso/WSSServer/dto/feed/FeedInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public record FeedInfo(
String title,
Integer novelRatingCount,
Float novelRating,
List<String> relevantCategories,
Boolean isSpoiler,
Boolean isModified,
Boolean isMyFeed,
Expand All @@ -34,8 +33,7 @@ public record FeedInfo(
Float feedWriterNovelRating
) {
public static FeedInfo of(Feed feed, UserBasicInfo userBasicInfo, Novel novel, Boolean isLiked,
List<String> relevantCategories, Boolean isMyFeed, String thumbnailUrl,
Integer imageCount, User user) {
Boolean isMyFeed, String thumbnailUrl, Integer imageCount, User user) {
String title = null;
Integer novelRatingCount = null;
Float novelRating = null;
Expand Down Expand Up @@ -70,7 +68,6 @@ public static FeedInfo of(Feed feed, UserBasicInfo userBasicInfo, Novel novel, B
title,
novelRatingCount,
novelRating,
relevantCategories,
feed.getIsSpoiler(),
!feed.getCreatedDate().equals(feed.getModifiedDate()),
isMyFeed,
Expand Down Expand Up @@ -124,4 +121,4 @@ private static Float getFeedWriterNovelRating(Novel novel, Long feedWriterId) {
.orElse(null);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package org.websoso.WSSServer.dto.feed;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.util.List;

public record FeedUpdateRequest(
@NotNull(message = "카테고리는 null일 수 없습니다.")
@NotEmpty(message = "카테고리는 1개 이상 선택해야 합니다.")
List<String> relevantCategories,

@NotBlank(message = "피드 내용은 비어 있거나, 공백일 수 없습니다.")
@Size(max = 2000, message = "피드 내용은 2000자를 초과할 수 없습니다.")
String feedContent,
Expand All @@ -23,4 +17,4 @@ public record FeedUpdateRequest(
@NotNull(message = "공개 여부는 null일 수 없습니다.")
Boolean isPublic
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import java.util.List;

public record FeedsGetResponse(
String category,
Boolean isLoadable,
List<FeedInfo> feeds
) {
static public FeedsGetResponse of(String category, Boolean isLoadable, List<FeedInfo> feeds) {
static public FeedsGetResponse of( Boolean isLoadable, List<FeedInfo> feeds) {
return new FeedsGetResponse(
category,
isLoadable,
feeds
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.websoso.WSSServer.dto.feed;

import java.util.List;
import org.websoso.WSSServer.feed.domain.Category;
import org.websoso.WSSServer.feed.domain.Feed;
import org.websoso.WSSServer.feed.domain.FeedCategory;
import org.websoso.WSSServer.feed.domain.Like;
import org.websoso.WSSServer.novel.domain.Novel;
import org.websoso.WSSServer.library.domain.UserNovel;
Expand All @@ -23,7 +21,6 @@ public record UserFeedGetResponse(
String title,
Float novelRating,
Long novelRatingCount,
List<String> relevantCategories,
Boolean isPublic,
String genre,
Float userNovelRating,
Expand All @@ -39,7 +36,6 @@ public static UserFeedGetResponse of(Feed feed, Novel novel, Long visitorId, Str
Float novelRating = getNovelRating(novel, novelRatingCount);
List<Long> likeUsers = getLikeUsers(feed);
boolean isLiked = likeUsers.contains(visitorId);
List<String> relevantCategories = getFeedCategories(feed);
String genreName = getNovelGenreName(novel);
Float userNovelRating = getUserNovelRating(novel, visitorId);
Float feedWriterNovelRating = getFeedWriterNovelRating(novel, feed.getUser().getUserId());
Expand All @@ -60,7 +56,6 @@ public static UserFeedGetResponse of(Feed feed, Novel novel, Long visitorId, Str
null : novel.getTitle(),
novelRating,
novelRatingCount,
relevantCategories,
feed.getIsPublic(),
genreName,
userNovelRating,
Expand All @@ -70,15 +65,6 @@ public static UserFeedGetResponse of(Feed feed, Novel novel, Long visitorId, Str
);
}

private static List<String> getFeedCategories(Feed feed) {
return feed.getFeedCategories()
.stream()
.map(FeedCategory::getCategory)
.map(Category::getCategoryName)
.map(Enum::name)
.toList();
}

private static List<Long> getLikeUsers(Feed feed) {
return feed.getLikes()
.stream()
Expand Down Expand Up @@ -150,4 +136,4 @@ private static Float getFeedWriterNovelRating(Novel novel, Long feedWriterId) {
.map(UserNovel::getUserNovelRating)
.orElse(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ public ResponseEntity<FeedGetResponse> getFeed(@AuthenticationPrincipal User use

@GetMapping
public ResponseEntity<FeedsGetResponse> getFeeds(@AuthenticationPrincipal User user,
@RequestParam(value = "category", required = false) String category,
@RequestParam("lastFeedId") Long lastFeedId,
@RequestParam("size") int size,
@RequestParam(value = "feedsOption", required = false) FeedGetOption feedGetOption) {
return ResponseEntity
.status(OK)
.body(feedFindApplication.getFeeds(user, category, lastFeedId, size, feedGetOption));
.body(feedFindApplication.getFeeds(user, lastFeedId, size, feedGetOption));
}

@PutMapping("/{feedId}")
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/org/websoso/WSSServer/feed/domain/Feed.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ public class Feed {
@JoinColumn(name = "user_id", nullable = false)
private User user;

@OneToMany(mappedBy = "feed", cascade = ALL, fetch = FetchType.LAZY)
private List<FeedCategory> feedCategories = new ArrayList<>();

@OneToMany(mappedBy = "feed", cascade = ALL, fetch = FetchType.LAZY)
private List<Like> likes = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ public interface FeedCustomRepository {

List<Feed> findFeedsByNoOffsetPagination(User owner, Long lastFeedId, int size, Boolean isVisible,
Boolean isUnVisible, SortCriteria sortCriteria, List<Genre> genres,
Long visitorId);
Long visitorId, boolean includeEtc);

Slice<Feed> findRecommendedFeeds(Long lastFeedId, Long userId, PageRequest pageRequest, List<Genre> genres);

Slice<Feed> findFeedsByGenres(List<Genre> genres, boolean includeEtc, Long lastFeedId, Long userId,
PageRequest pageRequest);

Long countVisibleFeeds(User owner, Long lastFeedId, Boolean isVisible,
Boolean isUnVisible, List<Genre> genres,
Long visitorId);
Long visitorId, boolean includeEtc);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public List<Feed> findPopularFeedsByNovelIds(List<Long> novelIds) {
@Override
public List<Feed> findFeedsByNoOffsetPagination(User owner, Long lastFeedId, int size, Boolean isVisible,
Boolean isUnVisible, SortCriteria sortCriteria,
List<Genre> genres, Long visitorId) {
List<Genre> genres, Long visitorId, boolean includeEtc) {
return jpaQueryFactory
.selectFrom(feed)
.distinct()
Expand All @@ -68,7 +68,7 @@ public List<Feed> findFeedsByNoOffsetPagination(User owner, Long lastFeedId, int
ltFeedId(lastFeedId),
checkVisible(visitorId),
checkPublic(isVisible, isUnVisible),
checkGenres(genres)
checkGenres(genres, includeEtc)
)
.orderBy(
checkSortCriteria(sortCriteria),
Expand All @@ -93,7 +93,7 @@ public Optional<FeedImage> findThumbnailFeedImageByFeedId(long feedId) {
@Override
public Long countVisibleFeeds(User owner, Long lastFeedId, Boolean isVisible,
Boolean isUnVisible, List<Genre> genres,
Long visitorId) {
Long visitorId, boolean includeEtc) {
return jpaQueryFactory
.select(feed.feedId.countDistinct())
.from(feed)
Expand All @@ -104,7 +104,7 @@ public Long countVisibleFeeds(User owner, Long lastFeedId, Boolean isVisible,
feed.user.eq(owner),
checkVisible(visitorId),
checkPublic(isVisible, isUnVisible),
checkGenres(genres)
checkGenres(genres, includeEtc)
)
.fetchOne();
}
Expand Down Expand Up @@ -149,7 +149,7 @@ public Slice<Feed> findRecommendedFeeds(Long lastFeedId, Long userId, PageReques
.where(
ltFeedId(lastFeedId),
checkPopularFeed(),
checkGenres(genres),
checkGenres(genres, false),
checkBlocking(userId),
checkHidden()
)
Expand All @@ -174,11 +174,39 @@ private BooleanExpression checkPopularFeed() {
.goe(POPULAR_FEED_LIKE_COUNT);
}

private BooleanExpression checkGenres(List<Genre> genres) {
private BooleanExpression checkGenres(List<Genre> genres, boolean includeEtc) {
BooleanExpression etcCondition = includeEtc ? feed.novelId.isNull() : null;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p5; 회사에서 하신 말씀이 이거였군요 ㅋㅎㅋㅎㅋㅎㅋ 진짜 햇갈리긴 하네요..

if (genres != null && !genres.isEmpty()) {
return genre.in(genres).or(feed.novelId.isNull());
BooleanExpression genreCondition = genre.in(genres);
return etcCondition != null ? genreCondition.or(etcCondition) : genreCondition;
}
return null;
return etcCondition;
}

@Override
public Slice<Feed> findFeedsByGenres(List<Genre> genres, boolean includeEtc, Long lastFeedId, Long userId,
PageRequest pageRequest) {
List<Feed> feeds = jpaQueryFactory
.selectFrom(feed)
.distinct()
.leftJoin(novel).on(feed.novelId.eq(novel.novelId))
.leftJoin(novelGenre).on(novel.eq(novelGenre.novel))
.leftJoin(genre).on(novelGenre.genre.eq(genre))
.where(
ltFeedId(lastFeedId),
checkGenres(genres, includeEtc),
checkBlocking(userId),
checkHidden()
)
.orderBy(feed.feedId.desc())
.limit(pageRequest.getPageSize() + 1)
.fetch();

boolean hasNext = feeds.size() > pageRequest.getPageSize();
if (hasNext) {
feeds.remove(feeds.size() - 1);
}
return new SliceImpl<>(feeds, pageRequest, hasNext);
}

private BooleanExpression checkBlocking(Long userId) {
Expand Down
Loading
Loading