diff --git a/src/main/java/com/project/mapdagu/domain/friend/service/FriendService.java b/src/main/java/com/project/mapdagu/domain/friend/service/FriendService.java index 546cccf..44a711a 100644 --- a/src/main/java/com/project/mapdagu/domain/friend/service/FriendService.java +++ b/src/main/java/com/project/mapdagu/domain/friend/service/FriendService.java @@ -3,6 +3,8 @@ 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; +import com.project.mapdagu.domain.friendRequest.entity.FriendRequest; +import com.project.mapdagu.domain.friendRequest.repository.FriendRequestRepository; import com.project.mapdagu.domain.member.entity.Member; import com.project.mapdagu.domain.member.repository.MemberRepository; import com.project.mapdagu.error.ErrorCode; @@ -22,6 +24,7 @@ public class FriendService { private final MemberRepository memberRepository; private final FriendRepository friendRepository; + private final FriendRequestRepository friendRequestRepository; @Transactional(readOnly = true) public Slice searchMember(String email, String search, Pageable pageable) { @@ -33,6 +36,8 @@ public Slice searchMember(String email, String search, public void saveFriend(String email, Long friendId) { Member member = memberRepository.findByEmail(email).orElseThrow(() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)); Member friend = memberRepository.findById(friendId).orElseThrow(() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)); + FriendRequest friendRequest = friendRequestRepository.findByFromMemberAndToMember(friend, member).orElseThrow(() -> new BusinessException(ErrorCode.FRIEND_REQUEST_NOT_FOUND)); + friendRequestRepository.delete(friendRequest); friendRepository.save(new Friend(member, friend)); } } diff --git a/src/main/java/com/project/mapdagu/domain/friendRequest/repository/FriendRequestRepository.java b/src/main/java/com/project/mapdagu/domain/friendRequest/repository/FriendRequestRepository.java index 6dd17a1..0801aa7 100644 --- a/src/main/java/com/project/mapdagu/domain/friendRequest/repository/FriendRequestRepository.java +++ b/src/main/java/com/project/mapdagu/domain/friendRequest/repository/FriendRequestRepository.java @@ -1,7 +1,12 @@ package com.project.mapdagu.domain.friendRequest.repository; import com.project.mapdagu.domain.friendRequest.entity.FriendRequest; +import com.project.mapdagu.domain.member.entity.Member; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface FriendRequestRepository extends JpaRepository { + + Optional findByFromMemberAndToMember(Member fromMember, Member toMember); } diff --git a/src/main/java/com/project/mapdagu/error/ErrorCode.java b/src/main/java/com/project/mapdagu/error/ErrorCode.java index 754d048..8b06af0 100644 --- a/src/main/java/com/project/mapdagu/error/ErrorCode.java +++ b/src/main/java/com/project/mapdagu/error/ErrorCode.java @@ -17,6 +17,7 @@ public enum ErrorCode { MEMBER_NOT_FOUND(NOT_FOUND, "해당 회원을 찾을 수 없습니다"), FOOD_NOT_FOUND(NOT_FOUND, "해당 음식을 찾을 수 없습니다"), EVALUATION_NOT_FOUND(NOT_FOUND, "해당 평가를 찾을 수 없습니다"), + FRIEND_REQUEST_NOT_FOUND(NOT_FOUND, "해당 친구 요청을 찾을 수 없습니다"), WRONG_SEARCH(BAD_REQUEST, "검색어를 입력해야 합니다."), ALREADY_EXIST_EVALUATION(BAD_REQUEST, "이미 존재하는 평가입니다."),