Skip to content

Commit

Permalink
Merge pull request #26 from Leets-Official/feat/#20/팔로워수-반환
Browse files Browse the repository at this point in the history
Feat #26 팔로워수 반환 및 프로필 조회시 팔로우 여부 반환
  • Loading branch information
hyxklee authored Nov 10, 2024
2 parents 52b2e64 + 9331160 commit de515d5
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 29 deletions.
49 changes: 37 additions & 12 deletions src/main/java/com/leets/X/domain/follow/service/FollowService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
Expand All @@ -23,7 +24,7 @@ public class FollowService {
private final UserService userService;

@Transactional
public void follow(Long userId, String email){
public void follow(Long userId, String email) {
User follower = userService.find(email);
User followed = userService.find(userId);

Expand All @@ -32,62 +33,86 @@ public void follow(Long userId, String email){
Follow follow = followRepository.save(Follow.of(follower, followed));

follower.addFollowing(follow);
follower.updateFollowingCount(countFollowing(follower));

followed.addFollower(follow);
followed.updateFollowerCount(countFollower(followed));
}

public List<FollowResponse> getFollowers(Long userId){
public List<FollowResponse> getFollowers(Long userId) {
User user = userService.find(userId);

List<Follow> followerList = user.getFollowerList();

return followerList.stream()
.map(follow -> {
return FollowResponse.from(follow.getFollower()); })
return FollowResponse.from(follow.getFollower());
})
.toList();
}

public List<FollowResponse> getFollowings(Long userId){
public List<FollowResponse> getFollowings(Long userId) {
User user = userService.find(userId);

List<Follow> followingList = user.getFollowingList();

return followingList.stream()
.map(follow -> {
return FollowResponse.from(follow.getFollowed()); })
return FollowResponse.from(follow.getFollowed());
})
.toList();
}

@Transactional
public void unfollow(Long userId, String email){
public void unfollow(Long userId, String email) {
User follower = userService.find(email);
User followed = userService.find(userId);

Follow follow = check(follower.getId(), followed.getId());

follower.removeFollowing(follow);
follower.decreaseFollowingCount();

followed.removeFollower(follow);
followed.decreaseFollowerCount();

followRepository.delete(follow);
}

public Follow find(Long followerId, Long followedId){
public Follow find(Long followerId, Long followedId) {
return followRepository.findByFollowerIdAndFollowedId(followerId, followedId)
.orElseThrow(FollowNotFoundException::new);
}

private Long countFollower(User user) {
return user.getFollowerList().stream()
.map(follow -> {
return FollowResponse.from(follow.getFollower());
})
.count();
}

private Long countFollowing(User user) {
return user.getFollowingList().stream()
.map(follow -> {
return FollowResponse.from(follow.getFollowed());
})
.count();
}

// 기존 팔로우 정보가 있는지, 나한테 요청을 하지 않는지 검증
private void validate(Long followerId, Long followedId){
if(followRepository.existsByFollowerIdAndFollowedId(followerId, followedId)){
private void validate(Long followerId, Long followedId) {
if (followRepository.existsByFollowerIdAndFollowedId(followerId, followedId)) {
throw new AlreadyFollowException();
}
if(followerId.equals(followedId)){
if (followerId.equals(followedId)) {
throw new InvalidFollowException();
}
}

// 팔로우 되어 있는지 확인
private Follow check(Long followerId, Long followedId){
if(!followRepository.existsByFollowerIdAndFollowedId(followerId, followedId)){
private Follow check(Long followerId, Long followedId) {
if (!followRepository.existsByFollowerIdAndFollowedId(followerId, followedId)) {
throw new InvalidUnfollowException();
}
return find(followerId, followedId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.leets.X.domain.user.dto.request.UserUpdateRequest;
import com.leets.X.domain.user.dto.response.UserProfileResponse;
import com.leets.X.domain.user.dto.response.UserSocialLoginResponse;
import com.leets.X.domain.user.service.LoginStatus;
import com.leets.X.domain.user.domain.enums.LoginStatus;
import com.leets.X.domain.user.service.UserService;
import com.leets.X.global.common.response.ResponseDto;
import io.swagger.v3.oas.annotations.Operation;
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/leets/X/domain/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class User extends BaseTimeEntity {

private String introduce;

private long followerCount = 0L;

private long followingCount = 0L;

@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<Post> posts = new ArrayList<>();
Expand Down Expand Up @@ -90,6 +93,24 @@ public void removeFollowing(Follow follow) {
this.followingList.remove(follow);
}

public void updateFollowerCount(long followerCount) {
this.followerCount = followerCount;
}

public void decreaseFollowerCount() {
if(this.followerCount > 0){
this.followerCount--;
}
}

public void updateFollowingCount(long followingCount) {
this.followingCount = followingCount;
}

public void decreaseFollowingCount() {
if(this.followingCount>0) {
this.followingCount--;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.leets.X.domain.user.service;
package com.leets.X.domain.user.domain.enums;

public enum LoginStatus {
LOGIN, REGISTER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ public record UserProfileResponse(
Boolean isMyProfile,
String name,
String customId,
LocalDateTime createAt,
Long followCount,
Long followerCount
) {
Long followerCount,
Long followingCount,
Boolean isFollowing,
LocalDateTime createdAt,
LocalDateTime updatedAt
) {
// 정적 팩토리 메서드
public static UserProfileResponse from(User user, Boolean isMyProfile) {
public static UserProfileResponse from(User user, Boolean isMyProfile, Boolean isFollowing) {
return UserProfileResponse.builder()
.userId(user.getId())
.isMyProfile(isMyProfile)
.name(user.getName())
.customId(user.getCustomId())
.createAt(user.getCreatedAt())
.followerCount(user.getFollowerCount())
.followingCount(user.getFollowingCount())
.isFollowing(isFollowing)
.createdAt(user.getCreatedAt())
.updatedAt(user.getUpdatedAt())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.leets.X.domain.user.dto.response;

import com.leets.X.domain.user.service.LoginStatus;
import com.leets.X.domain.user.domain.enums.LoginStatus;
import com.leets.X.global.auth.jwt.dto.JwtResponse;
import lombok.Builder;

Expand Down
25 changes: 17 additions & 8 deletions src/main/java/com/leets/X/domain/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.leets.X.domain.user.service;

import com.leets.X.domain.follow.domain.Follow;
import com.leets.X.domain.user.domain.User;
import com.leets.X.domain.user.dto.request.UserInitializeRequest;
import com.leets.X.domain.user.dto.request.UserUpdateRequest;
Expand All @@ -17,8 +18,10 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.leets.X.domain.user.service.LoginStatus.LOGIN;
import static com.leets.X.domain.user.service.LoginStatus.REGISTER;
import java.util.List;

import static com.leets.X.domain.user.domain.enums.LoginStatus.LOGIN;
import static com.leets.X.domain.user.domain.enums.LoginStatus.REGISTER;

@Slf4j
@Service
Expand Down Expand Up @@ -70,12 +73,9 @@ public void updateProfile(UserUpdateRequest dto, String email){
public UserProfileResponse getProfile(Long userId, String email){
User user = userRepository.findById(userId)
.orElseThrow(UserNotFoundException::new);
// 내 프로필이라면 true
if(user.getEmail().equals(email)){
return UserProfileResponse.from(user, true);
}
// 아니라면 false
return UserProfileResponse.from(user, false);
boolean isMyProfile = user.getEmail().equals(email);
boolean isFollowing = checkFollowing(user, email);
return UserProfileResponse.from(user, isMyProfile, isFollowing);
}
//
// @Transactional
Expand Down Expand Up @@ -107,6 +107,15 @@ private JwtResponse generateToken (String email){
.build();
}

// 팔로우 되어있는 유저라면 true
private boolean checkFollowing(User target, String email){
User source = find(email);
List<Follow> followerList = target.getFollowerList();

return followerList.stream()
.anyMatch(follow -> follow.getFollower().getId().equals(source.getId()));
}

/*
* userRepository에서 사용자를 검색하는 메서드
* 공통으로 사용되는 부분이 많기 때문에 별도로 분리
Expand Down

0 comments on commit de515d5

Please sign in to comment.