From 5e95e44b16d3ba09fb2d061a27afe08e63967170 Mon Sep 17 00:00:00 2001 From: "tae.y" <0211ilyoil@gmail.com> Date: Tue, 5 Dec 2023 20:06:13 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=A8=20::=20feed=20=EC=9E=90=EC=84=B8?= =?UTF-8?q?=ED=9E=88=20=EB=B3=B4=EA=B8=B0=EC=97=90=20isLiked=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/comment/QueryCommentService.java | 12 ++++++++++- .../dto/response/FeedListResponse.java | 6 ++++-- .../user/feed/repository/LikeRepository.java | 3 +++ .../feed/service/QueryFeedListService.java | 21 ++++++++++++++++--- .../domain/user/user/facade/UserFacade.java | 8 +++++++ 5 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/asterbackend/domain/user/comment/service/comment/QueryCommentService.java b/src/main/java/com/example/asterbackend/domain/user/comment/service/comment/QueryCommentService.java index c33d2a2..f685ca3 100644 --- a/src/main/java/com/example/asterbackend/domain/user/comment/service/comment/QueryCommentService.java +++ b/src/main/java/com/example/asterbackend/domain/user/comment/service/comment/QueryCommentService.java @@ -6,6 +6,8 @@ import com.example.asterbackend.domain.user.feed.entity.Feed; import com.example.asterbackend.domain.user.feed.facade.FeedFacade; import com.example.asterbackend.domain.user.feed.presentation.dto.response.FeedListResponse; +import com.example.asterbackend.domain.user.feed.repository.LikeRepository; +import com.example.asterbackend.domain.user.user.facade.UserFacade; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -20,10 +22,18 @@ public class QueryCommentService { private final CommentRepository commentRepository; + private final LikeRepository likeRepository; + + private final UserFacade userFacade; + public FeedAndCommentsResponse queryComment(Long feedId) { Feed feed = feedFacade.currentFeed(feedId); - FeedListResponse feedResponse = new FeedListResponse(feed); + String currentUserId = userFacade.getUser(); + + boolean isLiked = likeRepository.existsByFeedAndStudentId(feed, currentUserId); + + FeedListResponse feedResponse = new FeedListResponse(feed,isLiked); List commentList = commentRepository.findAllByFeedId(feedId) .stream() diff --git a/src/main/java/com/example/asterbackend/domain/user/feed/presentation/dto/response/FeedListResponse.java b/src/main/java/com/example/asterbackend/domain/user/feed/presentation/dto/response/FeedListResponse.java index 0d4a058..73ab583 100644 --- a/src/main/java/com/example/asterbackend/domain/user/feed/presentation/dto/response/FeedListResponse.java +++ b/src/main/java/com/example/asterbackend/domain/user/feed/presentation/dto/response/FeedListResponse.java @@ -1,7 +1,6 @@ package com.example.asterbackend.domain.user.feed.presentation.dto.response; import com.example.asterbackend.domain.user.feed.entity.Feed; -import com.example.asterbackend.domain.user.user.entity.User; import lombok.AllArgsConstructor; import lombok.Getter; @@ -27,7 +26,9 @@ public class FeedListResponse { private String profileImgUrl; - public FeedListResponse(Feed feed) { + private boolean isLiked; + + public FeedListResponse(Feed feed, boolean isLiked) { this.id = feed.getId(); this.title = feed.getTitle(); this.content = feed.getContent(); @@ -36,6 +37,7 @@ public FeedListResponse(Feed feed) { this.likeCount = feed.getLikeCount(); this.username = feed.getUser().getUsername(); this.profileImgUrl = feed.getUser().getProfileImgUrl(); + this.isLiked = isLiked; } } diff --git a/src/main/java/com/example/asterbackend/domain/user/feed/repository/LikeRepository.java b/src/main/java/com/example/asterbackend/domain/user/feed/repository/LikeRepository.java index 8eea974..6204ae9 100644 --- a/src/main/java/com/example/asterbackend/domain/user/feed/repository/LikeRepository.java +++ b/src/main/java/com/example/asterbackend/domain/user/feed/repository/LikeRepository.java @@ -1,10 +1,13 @@ package com.example.asterbackend.domain.user.feed.repository; +import com.example.asterbackend.domain.user.feed.entity.Feed; import com.example.asterbackend.domain.user.feed.entity.Like; import org.springframework.data.jpa.repository.JpaRepository; public interface LikeRepository extends JpaRepository { boolean existsByFeedIdAndStudentId(Long feedId, String studentId); + boolean existsByFeedAndStudentId(Feed feed, String studentId); + void deleteByFeedIdAndStudentId(Long feedId, String studentId); } diff --git a/src/main/java/com/example/asterbackend/domain/user/feed/service/QueryFeedListService.java b/src/main/java/com/example/asterbackend/domain/user/feed/service/QueryFeedListService.java index be2cf3c..77bb270 100644 --- a/src/main/java/com/example/asterbackend/domain/user/feed/service/QueryFeedListService.java +++ b/src/main/java/com/example/asterbackend/domain/user/feed/service/QueryFeedListService.java @@ -1,11 +1,17 @@ package com.example.asterbackend.domain.user.feed.service; +import com.example.asterbackend.domain.user.feed.entity.Feed; import com.example.asterbackend.domain.user.feed.presentation.dto.response.FeedListResponse; import com.example.asterbackend.domain.user.feed.repository.FeedRepository; +import com.example.asterbackend.domain.user.feed.repository.LikeRepository; +import com.example.asterbackend.domain.user.user.entity.User; +import com.example.asterbackend.domain.user.user.facade.UserFacade; +import com.example.asterbackend.domain.user.user.repository.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @Service @@ -13,11 +19,20 @@ public class QueryFeedListService { private final FeedRepository feedRepository; + private final UserFacade userFacade; + private final LikeRepository likeRepository; public List queryFeedList() { - return feedRepository.findAll() - .stream() - .map(FeedListResponse::new) + + String currentUserId = userFacade.getUser(); + + List allFeeds = feedRepository.findAll(); + + return allFeeds.stream() + .map(feed -> { + boolean isLiked = likeRepository.existsByFeedAndStudentId(feed, currentUserId); + return new FeedListResponse(feed, isLiked); + }) .collect(Collectors.toList()); } diff --git a/src/main/java/com/example/asterbackend/domain/user/user/facade/UserFacade.java b/src/main/java/com/example/asterbackend/domain/user/user/facade/UserFacade.java index afce70c..9c987c3 100644 --- a/src/main/java/com/example/asterbackend/domain/user/user/facade/UserFacade.java +++ b/src/main/java/com/example/asterbackend/domain/user/user/facade/UserFacade.java @@ -10,6 +10,8 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; +import java.util.Optional; + @Component @RequiredArgsConstructor public class UserFacade { @@ -29,4 +31,10 @@ public User getCurrentUser() { return userRepository.findByStudentId(studentId) .orElseThrow(() -> UserNotFoundException.EXCEPTION); } + + public String getUser() { + return SecurityContextHolder.getContext().getAuthentication().getName(); + + } + }