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 @@ -2,13 +2,9 @@

import static org.websoso.WSSServer.exception.error.CustomAvatarError.AVATAR_NOT_FOUND;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
Expand Down Expand Up @@ -135,30 +131,51 @@ private FeedInfo createFeedInfo(Feed feed, User user) {
}

@Transactional(readOnly = true)
public PopularFeedsGetResponse getPopularFeeds(User user) {
Long currentUserId = Optional.ofNullable(user).map(User::getUserId).orElse(null);

List<PopularFeed> popularFeeds = Optional.ofNullable(user).map(u -> findPopularFeedsWithUser(u.getUserId()))
.orElseGet(this::findPopularFeedsWithoutUser);
public PopularFeedsGetResponse getPopularFeeds(User user, int size) {
List<PopularFeed> popularFeeds = Optional.ofNullable(user)
.map(u -> findPopularFeedsWithUser(u.getUserId(), size))
.orElseGet(() -> findPopularFeedsWithoutUser(size));

List<Long> novelIds = popularFeeds.stream()
.map(f -> f.getFeed().getNovelId())
.filter(Objects::nonNull)
.distinct()
.toList();

Map<Long, Novel> novelMap = novelServiceImpl.getNovelsWithGenresByIds(novelIds).stream()
.collect(Collectors.toMap(Novel::getNovelId, novel -> novel));

List<PopularFeedGetResponse> popularFeedGetResponses = mapToPopularFeedGetResponseList(popularFeeds,
currentUserId);
List<PopularFeedGetResponse> popularFeedGetResponses = mapToPopularFeedGetResponseList(popularFeeds, novelMap);

return new PopularFeedsGetResponse(popularFeedGetResponses);
return PopularFeedsGetResponse.of(popularFeedGetResponses);
}

private List<PopularFeed> findPopularFeedsWithUser(Long userId) {
return feedServiceImpl.findPopularFeedsWithUser(userId);
private List<PopularFeed> findPopularFeedsWithUser(Long userId, int size) {
return feedServiceImpl.findPopularFeedsWithUser(userId, size);
}

private List<PopularFeed> findPopularFeedsWithoutUser() {
return feedServiceImpl.findPopularFeedsWithoutUser();
private List<PopularFeed> findPopularFeedsWithoutUser(int size) {
return feedServiceImpl.findPopularFeedsWithoutUser(size);
}

private static List<PopularFeedGetResponse> mapToPopularFeedGetResponseList(List<PopularFeed> popularFeeds,
Long currentUserId) {
return popularFeeds.stream().filter(pf -> pf.getFeed().isVisibleTo(currentUserId))
.map(PopularFeedGetResponse::of).toList();
Map<Long, Novel> novelMap) {
return popularFeeds.stream()
.map(popularFeed -> {
Novel novel = novelMap.get(popularFeed.getFeed().getNovelId());
String novelImage = Optional.ofNullable(novel).map(Novel::getNovelImage).orElse(null);
String novelGenreImage = Optional.ofNullable(novel)
.flatMap(FeedFindApplication::getFirstNovelGenreImage)
.orElse(null);

return PopularFeedGetResponse.of(popularFeed, novelImage, novelGenreImage);
}).toList();
}

private static Optional<String> getFirstNovelGenreImage(Novel novel) {
return novel.getNovelGenres().stream()
.findFirst()
.map(novelGenre -> novelGenre.getGenre().getGenreImage());
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ public record PopularFeedGetResponse(
Integer likeCount,
Integer commentCount,
Boolean isSpoiler,
Boolean isPublic
Boolean isPublic,
String novelImage,
String novelGenreImage
) {

public static PopularFeedGetResponse of(PopularFeed popularFeed) {
public static PopularFeedGetResponse of(PopularFeed popularFeed, String novelImage, String novelGenreImage) {
return new PopularFeedGetResponse(
popularFeed.getFeed().getFeedId(),
popularFeed.getFeed().getFeedContent(),
popularFeed.getFeed().getLikes().size(),
popularFeed.getFeed().getComments().size(),
popularFeed.getFeed().getIsSpoiler(),
popularFeed.getFeed().getIsPublic()
popularFeed.getFeed().getIsPublic(),
novelImage,
novelGenreImage
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ public ResponseEntity<Void> unLikeFeed(@AuthenticationPrincipal User user,
}

@GetMapping("/popular")
public ResponseEntity<PopularFeedsGetResponse> getPopularFeeds(@AuthenticationPrincipal User user) {
public ResponseEntity<PopularFeedsGetResponse> getPopularFeeds(@AuthenticationPrincipal User user,
@RequestParam(name = "size", defaultValue = "9") int size) {
return ResponseEntity
.status(OK)
.body(feedFindApplication.getPopularFeeds(user));
.body(feedFindApplication.getPopularFeeds(user, size));
}

@GetMapping("/interest")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

public interface PopularFeedCustomRepository {

List<PopularFeed> findTodayPopularFeeds(Long userId);
List<PopularFeed> findTodayPopularFeeds(Long userId, int size);

List<PopularFeed> findOrderByPopularFeedIdDesc(int size);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

p3 : limit 갯수를 하드코딩 하는것보다는 변수로 박고 기본값을 9로 두는게 좋지 않을까 합니다!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

좋습니다~
그러면 해당 정책을 관리해야할 레이어는 API라고 보시나요?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

넵!

Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.websoso.WSSServer.feed.repository;

import static org.websoso.WSSServer.feed.domain.QFeed.feed;
import static org.websoso.WSSServer.feed.domain.QPopularFeed.popularFeed;
import static org.websoso.WSSServer.user.domain.QBlock.block;
import static org.websoso.WSSServer.user.domain.QUser.user;


import com.querydsl.jpa.impl.JPAQueryFactory;

import java.util.List;
import java.util.stream.Stream;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.websoso.WSSServer.feed.domain.PopularFeed;
Expand All @@ -18,7 +22,7 @@ public class PopularFeedCustomRepositoryImpl implements PopularFeedCustomReposit
private final JPAQueryFactory jpaQueryFactory;

@Override
public List<PopularFeed> findTodayPopularFeeds(Long userId) {
public List<PopularFeed> findTodayPopularFeeds(Long userId, int size) {
List<Long> blockingIds = jpaQueryFactory
.select(block.blockedId)
.from(block)
Expand All @@ -37,9 +41,25 @@ public List<PopularFeed> findTodayPopularFeeds(Long userId) {

return jpaQueryFactory
.selectFrom(popularFeed)
.where(popularFeed.feed.user.userId.notIn(blockIds))
.join(popularFeed.feed, feed)
.join(feed.user, user)
.where(user.userId.notIn(blockIds),
popularFeed.feed.isPublic.isTrue(),
popularFeed.feed.isHidden.isFalse())
.orderBy(popularFeed.popularFeedId.desc())
.limit(size)
.fetch();
}

@Override
public List<PopularFeed> findOrderByPopularFeedIdDesc(int size) {
return jpaQueryFactory
.selectFrom(popularFeed)
.join(popularFeed.feed, feed)
.where(feed.isPublic.isTrue(),
feed.isHidden.isFalse())
.orderBy(popularFeed.popularFeedId.desc())
.limit(9)
.limit(size)
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ public interface PopularFeedRepository extends JpaRepository<PopularFeed, Long>,

Boolean existsByFeed(Feed feed);

List<PopularFeed> findTop9ByOrderByPopularFeedIdDesc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -90,13 +89,13 @@ public Optional<FeedImage> findThumbnailFeedImageByFeedId(Long feedId) {
}

@Transactional(readOnly = true)
public List<PopularFeed> findPopularFeedsWithUser(Long userId) {
return popularFeedRepository.findTodayPopularFeeds(userId);
public List<PopularFeed> findPopularFeedsWithUser(Long userId, int size) {
return popularFeedRepository.findTodayPopularFeeds(userId, size);
}

@Transactional(readOnly = true)
public List<PopularFeed> findPopularFeedsWithoutUser() {
return popularFeedRepository.findTop9ByOrderByPopularFeedIdDesc();
public List<PopularFeed> findPopularFeedsWithoutUser(int size) {
return popularFeedRepository.findOrderByPopularFeedIdDesc(size);
}

@Transactional(readOnly = true)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface NovelCustomRepository {
Page<Novel> findFilteredNovels(Pageable pageable, List<Genre> genres, Boolean isCompleted, Float novelRatingStart, Float novelRatingEnd, List<Keyword> keywords);

List<Novel> findAutocompleteNovels(String searchQuery, int limitSize);

List<Novel> findAllByNovelIdInWithGenres(List<Long> novelIds);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.websoso.WSSServer.novel.repository;

import static org.websoso.WSSServer.domain.QGenre.genre;
import static org.websoso.WSSServer.domain.common.ReadStatus.WATCHED;
import static org.websoso.WSSServer.domain.common.ReadStatus.WATCHING;
import static org.websoso.WSSServer.library.domain.QUserNovel.userNovel;
Expand Down Expand Up @@ -103,8 +104,6 @@ public Page<Novel> findFilteredNovels(Pageable pageable, List<Genre> genres, Boo

@Override
public List<Novel> findAutocompleteNovels(String searchQuery, int limitSize) {


return jpaQueryFactory
.selectFrom(novel)
.leftJoin(novel.userNovels, userNovel)
Expand All @@ -115,6 +114,17 @@ public List<Novel> findAutocompleteNovels(String searchQuery, int limitSize) {
.fetch();
}

@Override
public List<Novel> findAllByNovelIdInWithGenres(List<Long> novelIds) {
return jpaQueryFactory
.selectDistinct(novel)
.from(novel)
.leftJoin(novel.novelGenres, novelGenre).fetchJoin()
.leftJoin(novelGenre.genre, genre).fetchJoin()
.where(novel.novelId.in(novelIds))
.fetch();
}

private BooleanExpression titleContainsQuery(String searchQuery) {
return getCleanedString(novel.title).containsIgnoreCase(searchQuery);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import static org.websoso.WSSServer.exception.error.CustomNovelError.NOVEL_NOT_FOUND;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.websoso.WSSServer.domain.Genre;
import org.websoso.WSSServer.novel.domain.PopularNovel;
import org.websoso.WSSServer.dto.platform.PlatformGetResponse;
import org.websoso.WSSServer.exception.exception.CustomNovelException;
import org.websoso.WSSServer.library.domain.Keyword;
Expand All @@ -20,7 +17,6 @@
import org.websoso.WSSServer.novel.repository.NovelGenreRepository;
import org.websoso.WSSServer.novel.repository.NovelPlatformRepository;
import org.websoso.WSSServer.novel.repository.NovelRepository;
import org.websoso.WSSServer.novel.repository.PopularNovelRepository;

@Service
@RequiredArgsConstructor
Expand All @@ -37,6 +33,15 @@ public Novel getNovelOrException(Long novelId) {
"novel with the given id is not found"));
}

@Transactional(readOnly = true)
public List<Novel> getNovelsWithGenresByIds(List<Long> novelIds) {
if (novelIds == null || novelIds.isEmpty()) {
return List.of();
}

return novelRepository.findAllByNovelIdInWithGenres(novelIds);
}

public Page<Novel> searchNovels(PageRequest pageRequest, String searchQuery) {
return novelRepository.findSearchedNovels(pageRequest, searchQuery);
}
Expand Down
Loading