Skip to content

Commit

Permalink
feat: 친구 목록 조회 로직 구현 (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeong-hyeok committed Sep 28, 2023
1 parent 5fd0563 commit 3969e38
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@

import com.project.mapdagu.domain.friend.entity.Friend;
import com.project.mapdagu.domain.member.entity.Member;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface FriendRepository extends JpaRepository<Friend, Long> {

Optional<Friend> findByMemberAndFriend(Member member, Member friend);
}

@Query("select f.friend from Friend f " +
" where f.member.id =:memberId ")
List<Member> findAllFriendByMemberId(@Param("memberId") Long memberId);

@Query("select f.member from Friend f " +
" where f.friend.id =:friendId ")
List<Member> findAllMemberByFriendId(@Param("friendId") Long friendId);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.project.mapdagu.domain.friend.service;

import com.project.mapdagu.domain.friend.dto.response.FriendGetAllResponseDto;
import com.project.mapdagu.domain.friend.dto.response.FriendSearchResponseDto;
import com.project.mapdagu.domain.friend.entity.Friend;
import com.project.mapdagu.domain.friend.repository.FriendRepository;
Expand All @@ -13,9 +14,15 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

@Service
@Slf4j
@Transactional
Expand Down Expand Up @@ -55,4 +62,16 @@ public void deleteFriend(String email, Long friendId) {
friendRepository.delete(findFriend);
}
}

public Slice<FriendGetAllResponseDto> getFriends(String email, Pageable pageable) {
Member member = memberRepository.findByEmail(email).orElseThrow(() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND));
List<Member> friends1 = friendRepository.findAllFriendByMemberId(member.getId());
List<Member> friends2 = friendRepository.findAllMemberByFriendId(member.getId());
List<Member> friendList = new ArrayList<>();
friendList.addAll(friends1);
friendList.addAll(friends2);
friendList.sort(Comparator.comparing(Member::getLevel).reversed());
Slice<Member> response = new SliceImpl<>(friendList, pageable, false);
return response.map(m -> FriendGetAllResponseDto.from(m));
}
}

0 comments on commit 3969e38

Please sign in to comment.