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

[FEAT] 작품정보 조회 API(피드탭) 구현 #128

Merged
merged 14 commits into from
Aug 7, 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 @@ -29,6 +29,7 @@ public class SecurityConfig {
"/actuator/health",
"/novels/{novelId}",
"/novels/{novelId}/info",
"/novels/{novelId}/feeds",
"/soso-picks",
"/novels/popular",
"/feeds",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.websoso.WSSServer.domain.User;
import org.websoso.WSSServer.dto.novel.NovelGetResponseBasic;
import org.websoso.WSSServer.dto.novel.NovelGetResponseFeedTab;
import org.websoso.WSSServer.dto.novel.NovelGetResponseInfoTab;
import org.websoso.WSSServer.dto.popularNovel.PopularNovelsGetResponse;
import org.websoso.WSSServer.service.FeedService;
import org.websoso.WSSServer.service.NovelService;
import org.websoso.WSSServer.service.UserService;

Expand All @@ -26,6 +29,7 @@ public class NovelController {

private final NovelService novelService;
private final UserService userService;
private final FeedService feedService;

@GetMapping("/{novelId}")
public ResponseEntity<NovelGetResponseBasic> getNovelInfoBasic(Principal principal, @PathVariable Long novelId) {
Expand All @@ -47,6 +51,20 @@ public ResponseEntity<NovelGetResponseInfoTab> getNovelInfoInfoTab(@PathVariable
.body(novelService.getNovelInfoInfoTab(novelId));
}

@GetMapping("/{novelId}/feeds")
public ResponseEntity<NovelGetResponseFeedTab> getFeedsByNovel(Principal principal,
@PathVariable Long novelId,
@RequestParam("lastFeedId") Long lastFeedId,
@RequestParam("size") int size) {
User user = principal == null
? null
: userService.getUserOrException(Long.valueOf(principal.getName()));

return ResponseEntity
.status(OK)
.body(feedService.getFeedsByNovel(user, novelId, lastFeedId, size));
}

@PostMapping("/{novelId}/is-interest")
public ResponseEntity<Void> registerAsInterest(Principal principal,
@PathVariable("novelId") Long novelId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.websoso.WSSServer.dto.novel;

import java.util.List;
import org.websoso.WSSServer.dto.feed.FeedInfo;

public record NovelGetResponseFeedTab(
Boolean isLoadable,
List<FeedInfo> feeds
) {
public static NovelGetResponseFeedTab of(Boolean isLoadable, List<FeedInfo> feeds) {
return new NovelGetResponseFeedTab(
isLoadable,
feeds
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public interface FeedCategoryRepository extends JpaRepository<FeedCategory, Long
+ "AND (:lastFeedId = 0 OR fc.feed.feedId < :lastFeedId) "
+ "AND fc.feed.isHidden = false "
+ "AND (:lastFeedId IS NULL "
+ "OR (fc.feed.user.userId NOT IN (SELECT b.blockingId FROM Block b WHERE b.blockedId = :userId)) "
+ "AND fc.feed.user.userId NOT IN (SELECT b.blockedId FROM Block b WHERE b.blockingId = :userId)) "
+ "OR (fc.feed.user.userId NOT IN (SELECT b.blockingId FROM Block b WHERE b.blockedId = :userId) "
+ "AND fc.feed.user.userId NOT IN (SELECT b.blockedId FROM Block b WHERE b.blockingId = :userId))) "
+ "ORDER BY fc.feed.feedId DESC")
Slice<Feed> findFeedsByCategory(Category category, Long lastFeedId, Long userId, PageRequest pageRequest);

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/websoso/WSSServer/repository/FeedRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ public interface FeedRepository extends JpaRepository<Feed, Long>, FeedCustomRep
+ "(:lastFeedId = 0 OR f.feedId < :lastFeedId) "
+ "AND f.isHidden = false "
+ "AND (:userId IS NULL "
+ "OR (f.user.userId NOT IN (SELECT b.blockingId FROM Block b WHERE b.blockedId = :userId)) "
+ "AND f.user.userId NOT IN (SELECT b.blockedId FROM Block b WHERE b.blockingId = :userId)) "
+ "OR (f.user.userId NOT IN (SELECT b.blockingId FROM Block b WHERE b.blockedId = :userId) "
+ "AND f.user.userId NOT IN (SELECT b.blockedId FROM Block b WHERE b.blockingId = :userId))) "
+ "ORDER BY f.feedId DESC")
Slice<Feed> findFeeds(Long lastFeedId, Long userId, PageRequest pageRequest);

@Query(value = "SELECT f FROM Feed f WHERE "
+ "(:lastFeedId = 0 OR f.feedId < :lastFeedId) "
+ "AND f.novelId = :novelId "
+ "AND f.isHidden = false "
+ "AND (:userId IS NULL "
+ "OR (f.user.userId NOT IN (SELECT b.blockingId FROM Block b WHERE b.blockedId = :userId) "
+ "AND f.user.userId NOT IN (SELECT b.blockedId FROM Block b WHERE b.blockingId = :userId))) "
+ "ORDER BY f.feedId DESC")
Slice<Feed> findFeedsByNovelId(Long novelId, Long lastFeedId, Long userId, PageRequest pageRequest);
}
15 changes: 15 additions & 0 deletions src/main/java/org/websoso/WSSServer/service/FeedService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.websoso.WSSServer.dto.feed.FeedInfo;
import org.websoso.WSSServer.dto.feed.FeedUpdateRequest;
import org.websoso.WSSServer.dto.feed.FeedsGetResponse;
import org.websoso.WSSServer.dto.novel.NovelGetResponseFeedTab;
import org.websoso.WSSServer.dto.user.UserBasicInfo;
import org.websoso.WSSServer.exception.exception.CustomFeedException;
import org.websoso.WSSServer.repository.FeedRepository;
Expand Down Expand Up @@ -189,4 +190,18 @@ private Slice<Feed> findFeedsByCategoryLabel(String category, Long lastFeedId, L
return feedCategoryService.getFeedsByCategoryLabel(category, lastFeedId, userId, pageRequest);
}

public NovelGetResponseFeedTab getFeedsByNovel(User user, Long novelId, Long lastFeedId, int size) {
Long userIdOrNull = user == null
? null
: user.getUserId();

Slice<Feed> feeds = feedRepository.findFeedsByNovelId(novelId, lastFeedId, userIdOrNull,
PageRequest.of(DEFAULT_PAGE_NUMBER, size));

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

return NovelGetResponseFeedTab.of(feeds.hasNext(), feedGetResponses);
}
}
Loading