Skip to content
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
13 changes: 10 additions & 3 deletions src/main/java/com/codeit/todo/repository/FollowRepository.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.codeit.todo.repository;


import com.codeit.todo.domain.Follow;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface FollowRepository extends JpaRepository<Follow, Integer> {

@Query(
"SELECT COUNT(f) > 0 " +
"from Follow f " +
Expand All @@ -17,5 +15,14 @@ public interface FollowRepository extends JpaRepository<Follow, Integer> {
)
boolean existsByFollower_FollowerIdAndFollowee_FolloweeId(@Param("userId")int userId, @Param("targetUserId")int targetUserId);

@Query("SELECT COUNT(*) FROM Follow f " +
"WHERE f.followee.userId = :userId ")
int countByFollower(@Param("userId") int userId);

@Query("SELECT COUNT(*) FROM Follow f " +
"WHERE f.follower.userId = :userId ")
int countByFollowee(@Param("userId") int userId);


}

2 changes: 2 additions & 0 deletions src/main/java/com/codeit/todo/service/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public interface UserService {
UpdatePasswordResponse updatePassword(int userId, UpdatePasswordRequest passwordRequest);

ReadTargetUserResponse findTargetUserProfile(int userId, int targetUserId);

ReadMyPageResponse findUserInfoAndFollows(int userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import com.codeit.todo.common.exception.user.SignUpException;
import com.codeit.todo.common.exception.user.UpdatePasswordException;
import com.codeit.todo.common.exception.user.UserNotFoundException;
import com.codeit.todo.domain.Complete;
import com.codeit.todo.domain.Todo;
import com.codeit.todo.domain.User;
import com.codeit.todo.repository.CompleteRepository;
import com.codeit.todo.repository.FollowRepository;
import com.codeit.todo.repository.GoalRepository;
import com.codeit.todo.repository.UserRepository;
Expand All @@ -30,7 +27,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.SignatureException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -97,11 +93,10 @@ public String login(LoginRequest loginRequest){
}
}

@Transactional(readOnly = true)
@Override
public ReadUserResponse findUserInfo(int userId) {
User user = userRepository.findById(userId)
.orElseThrow(()-> new UserNotFoundException(String.valueOf(userId), "User"));

User user = getUser(userId);
return ReadUserResponse.from(user);
}

Expand Down Expand Up @@ -158,6 +153,15 @@ public ReadTargetUserResponse findTargetUserProfile(int userId, int targetUserId

}

@Override
public ReadMyPageResponse findUserInfoAndFollows(int userId) {

int followerCount = followRepository.countByFollower(userId);
int followeeCount = followRepository.countByFollowee(userId);

return ReadMyPageResponse.from(followerCount, followeeCount);
}

private User getUser(int userId){
User user = userRepository.findById(userId)
.orElseThrow(()-> new UserNotFoundException(String.valueOf(userId), "User"));
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/codeit/todo/web/controller/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.codeit.todo.web.dto.request.auth.UpdatePasswordRequest;
import com.codeit.todo.web.dto.request.auth.UpdatePictureRequest;
import com.codeit.todo.web.dto.response.Response;
import com.codeit.todo.web.dto.response.auth.ReadMyPageResponse;
import com.codeit.todo.web.dto.response.auth.ReadTargetUserResponse;
import com.codeit.todo.web.dto.response.auth.UpdatePasswordResponse;
import com.codeit.todo.web.dto.response.auth.UpdatePictureResponse;
Expand Down Expand Up @@ -76,7 +77,7 @@ public Response<UpdatePictureResponse> updateUserProfilePicture(
}

@Operation(
summary = "비밀번호 변정",
summary = "비밀번호 변경",
description = "기존 비밀번호를 확인하고, 새로운 비밀번호로 변경"
)
@ApiResponses(value = {
Expand All @@ -103,4 +104,16 @@ public Response<ReadTargetUserResponse> getTargetUserProfile(
int userId = customUserDetails.getUserId();
return Response.ok( userService.findTargetUserProfile(userId, targetUserId) );
}


@Operation(summary = "마이페이지 가져오기", description = "유저 정보, 팔로워 팔로이 수 가져오기")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "조회 성공")
})
@GetMapping(value = "/mypage")
public Response<ReadMyPageResponse> getMyPage(@AuthenticationPrincipal CustomUserDetails customUserDetails){
int userId = customUserDetails.getUserId();
return Response.ok( userService.findUserInfoAndFollows(userId));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.codeit.todo.web.dto.response.auth;

import com.codeit.todo.domain.User;
import lombok.Builder;

@Builder
public record ReadMyPageResponse(
int followerCount,
int followeeCount
) {
public static ReadMyPageResponse from(int followerCount, int followeeCount){
return ReadMyPageResponse.builder()
.followerCount(followerCount)
.followeeCount(followeeCount)
.build();
}
}
Loading