- 23.12.08) TIL2023년 12월 08일 20시 29분 49초에 업로드 된 글입니다.작성자: oneseel
오늘 팔로우와 좋아요 기능 거의 완성했다.
1. 팔로우 목록
// 팔로우 목록 조회하기 (로그인한 유저가 팔로우 한 유저들의 목록) @GetMapping("/followers") public ResponseEntity<List<UserDTO>> getFollowers( @AuthenticationPrincipal UserDetailsImpl userDetails) { String username = userDetails.getUsername(); List<UserDTO> followers = followService.getFollowers(username); return ResponseEntity.ok(followers); }
// 팔로우 목록 조회하기 (로그인한 유저가 팔로우 한 유저들의 목록) public List<UserDTO> getFollowers(String username) { User user = checkUser(username); List<Follow> followers = followRepository.findByFollowing(user); List<UserDTO> followerDTOList = new ArrayList<>(); for (Follow follow : followers) { User follower = follow.getFollower(); UserDTO userDTO = new UserDTO(follower.getUsername()); followerDTOList.add(userDTO); } return followerDTOList; }
2. 팔로잉 목록
// 팔로잉 목록 조회하기 (로그인한 유저를 팔로우 한 유저들의 목록) @GetMapping("/followings") public ResponseEntity<List<UserDTO>> getFollowings( @AuthenticationPrincipal UserDetailsImpl userDetails) { String username = userDetails.getUsername(); List<UserDTO> followers = followService.getFollowings(username); return ResponseEntity.ok(followers); }
// 팔로잉 목록 조회하기 (로그인한 유저를 팔로우 한 유저들의 목록) public List<UserDTO> getFollowings(String username) { User user = checkUser(username); List<Follow> followings = followRepository.findByFollower(user); List<UserDTO> followingDTOList = new ArrayList<>(); for (Follow follow : followings) { User following = follow.getFollowing(); UserDTO userDTO = new UserDTO(following.getUsername()); followingDTOList.add(userDTO); } return followingDTOList; }
3. 수정한 것
1) 팔로우 할 때 자기 자신을 팔로우 할 수 있어서 아래와 같이 수정했다.
// 팔로우 하기 public Follow followUser(String username, Long followerId) { User user = checkUser(username); User follower = checkFollowerUser(followerId); // 자기 자신은 팔로우 하는 경우 if (user.getId().equals(follower.getId())) { throw new SelfFollowException(); } // 이미 팔로우 한 경우 if (isAlreadyFollow(user, follower)) { throw new DuplicatedFollowException(); } Follow follow = new Follow(user, follower); return followRepository.save(follow); }
2) 좋아요 증가, 감소 로직
- Post, Comment 엔터티에 likeCount필드를 넣고 아래와 같이 코드를 추가했다.
// 게시글 좋아요 하기 @Transactional public LikePost likePost(User loginUser, Long postId) { Post post = postRepository.findById(postId).orElseThrow(PostNotFoundException::new); if (likePostRepository.findByUserAndPost(loginUser, post).isPresent()) { throw new DuplicatedLikeException(); } LikePost likePost = LikePost.fromUserAndPost(loginUser, post); likePostRepository.save(likePost); post.setLikeCount(post.getLikeCount() + 1); postRepository.save(post); return likePost; }
// 게시글 좋아요 취소하기 @Transactional public void unLikePost(User loginUser, Long postId) { Post post = postRepository.findById(postId).orElseThrow(PostNotFoundException::new); LikePost likePost = likePostRepository.findByUserAndPost(loginUser, post) .orElseThrow(NotFoundLikeException::new); likePostRepository.deleteById(likePost.getId()); post.setLikeCount(post.getLikeCount() - 1); postRepository.save(post); }
'TIL' 카테고리의 다른 글
23.12.13) TIL (0) 2023.12.14 23.12.12) TIL (0) 2023.12.12 23.12.07) TIL (0) 2023.12.07 23.12.06) TIL (0) 2023.12.06 23.12.05) TIL (0) 2023.12.05 댓글