Skip to content

Commit

Permalink
๐ŸŒจ :: feed ์ž์„ธํžˆ ๋ณด๊ธฐ์— isLiked ์ถ”๊ฐ€
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyoil2 committed Dec 5, 2023
1 parent 2b19c8a commit 5e95e44
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<QueryCommentResponse> commentList = commentRepository.findAllByFeedId(feedId)
.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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<Like, Long> {
boolean existsByFeedIdAndStudentId(Long feedId, String studentId);

boolean existsByFeedAndStudentId(Feed feed, String studentId);

void deleteByFeedIdAndStudentId(Long feedId, String studentId);
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
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
@RequiredArgsConstructor
public class QueryFeedListService {

private final FeedRepository feedRepository;
private final UserFacade userFacade;
private final LikeRepository likeRepository;

public List<FeedListResponse> queryFeedList() {
return feedRepository.findAll()
.stream()
.map(FeedListResponse::new)

String currentUserId = userFacade.getUser();

List<Feed> allFeeds = feedRepository.findAll();

return allFeeds.stream()
.map(feed -> {
boolean isLiked = likeRepository.existsByFeedAndStudentId(feed, currentUserId);
return new FeedListResponse(feed, isLiked);
})
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -29,4 +31,10 @@ public User getCurrentUser() {
return userRepository.findByStudentId(studentId)
.orElseThrow(() -> UserNotFoundException.EXCEPTION);
}

public String getUser() {
return SecurityContextHolder.getContext().getAuthentication().getName();

}

}

0 comments on commit 5e95e44

Please sign in to comment.