Skip to content

Commit

Permalink
Merge pull request #49 from FlouD-2024/mypage
Browse files Browse the repository at this point in the history
Feat: 내 친구 리스트 조회시 페이징 처리
  • Loading branch information
Kang1221 authored Mar 12, 2024
2 parents 1e9285e + 60f4b67 commit 2615ebb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/main/java/floud/demo/controller/MyPageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public ApiResponse<?> getMyCommunity(@RequestHeader(value="Authorization") Strin
}

@GetMapping("/friend")
public ApiResponse<?> getFriendList(@RequestHeader(value="Authorization") String authorizationHeader){
return myPageService.getFriendList(authorizationHeader);
public ApiResponse<?> getFriendList(@RequestHeader(value="Authorization") String authorizationHeader,
@PageableDefault(size = 8) Pageable pageable){
return myPageService.getFriendList(authorizationHeader, pageable);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package floud.demo.dto.mypage;

import floud.demo.dto.PageInfo;
import floud.demo.dto.mypage.dto.MyFriend;
import floud.demo.dto.mypage.dto.MyWaiting;
import lombok.Builder;
Expand All @@ -12,4 +13,5 @@
public class MypageFriendListResponseDto {
private List<MyWaiting> waitingList;
private List<MyFriend> myFriendList;
private PageInfo frinedPageInfo;
}
5 changes: 2 additions & 3 deletions src/main/java/floud/demo/repository/FriendshipRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ public interface FriendshipRepository extends JpaRepository<Friendship, Long> {
@Query(value = "SELECT * FROM friendship f " +
"WHERE f.friendship_status = 'ACCEPT' " +
"AND (f.to_user = :users_id OR f.from_user = :users_id)", nativeQuery = true)
List<Friendship> findAllByUsersId(Long users_id);

Page<Friendship> findAllByUsersIdAndPage(Pageable pageable, Long users_id);
@Query(value = "SELECT * FROM friendship f " +
"WHERE f.friendship_status = 'ACCEPT' " +
"AND (f.to_user = :users_id OR f.from_user = :users_id)", nativeQuery = true)
Page<Friendship> findAllByUsersIdAndPage(Pageable pageable, Long users_id);
List<Friendship> findAllByUsersId( Long users_id);

//현재 친구 관계가 있는 지 확인
@Query(value = "SELECT * FROM friendship f " +
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/floud/demo/service/MyPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ public ApiResponse<?> getMyCommunity(String authorizationHeader, Pageable pageab
* 친구 관리
**/
@Transactional
public ApiResponse<?> getFriendList(String authorizationHeader){
public ApiResponse<?> getFriendList(String authorizationHeader, Pageable pageable){
//Get user
Users users = authService.findUserByToken(authorizationHeader);

//Find Friend List
MypageFriendListResponseDto responseDto = findFriends(users);
MypageFriendListResponseDto responseDto = findFriends(pageable, users);

return ApiResponse.success(Success.GET_FRIEND_LIST_SUCCESS,responseDto);
}
Expand Down Expand Up @@ -168,9 +168,9 @@ private List<MyPost> setMyPosts(Page<Community> communityList){
.build()).toList();
}

private MypageFriendListResponseDto findFriends(Users me){
private MypageFriendListResponseDto findFriends(Pageable pageable, Users me){
List<Friendship> waitingFriends = friendshipRepository.findAllByWaitingToUser(me.getId());
List<Friendship> acceptedFriends = friendshipRepository.findAllByUsersId(me.getId());
Page<Friendship> acceptedFriends = friendshipRepository.findAllByUsersIdAndPage(pageable, me.getId());

List<MyWaiting> myWaitingList = waitingFriends.stream()
.map(friendship -> buildMyWaiting(friendship, me))
Expand All @@ -183,6 +183,7 @@ private MypageFriendListResponseDto findFriends(Users me){
return MypageFriendListResponseDto.builder()
.waitingList(myWaitingList)
.myFriendList(myFriendList)
.frinedPageInfo(setFriendPageInfo(acceptedFriends))
.build();
}

Expand Down Expand Up @@ -224,4 +225,13 @@ private PageInfo setPageInfo(Page<Community> postPage){
.build();
}

private PageInfo setFriendPageInfo(Page<Friendship> friendshipPage){
return PageInfo.builder()
.last(!friendshipPage.hasNext())
.nowPage(friendshipPage.getNumber())
.totalPages(friendshipPage.getTotalPages())
.totalElements(friendshipPage.getTotalElements())
.build();
}

}

0 comments on commit 2615ebb

Please sign in to comment.