diff --git a/src/main/java/com/project/mapdagu/domain/friend/controller/FriendController.java b/src/main/java/com/project/mapdagu/domain/friend/controller/FriendController.java index 356454c..0596501 100644 --- a/src/main/java/com/project/mapdagu/domain/friend/controller/FriendController.java +++ b/src/main/java/com/project/mapdagu/domain/friend/controller/FriendController.java @@ -52,7 +52,8 @@ public ResponseEntity searchMember(@AuthenticationPrincipal Us responses = { @ApiResponse(responseCode = "204", description = "친구 추가 성공") , @ApiResponse(responseCode = "401", description = "인증에 실패했습니다.") - , @ApiResponse(responseCode = "404", description = "해당 회원을 찾을 수 없습니다.", content = @Content(schema = @Schema(implementation = ErrorResponse.class))) + , @ApiResponse(responseCode = "400", description = "이미 상대방과 친구입니다.") + , @ApiResponse(responseCode = "404", description = "1. 해당 회원을 찾을 수 없습니다. \t\n 2. 해당 친구 요청을 찾을 수 없습니다.", content = @Content(schema = @Schema(implementation = ErrorResponse.class))) }) @PostMapping("/{memberId}") public ResponseEntity saveFriend(@AuthenticationPrincipal UserDetails loginUser, @PathVariable Long memberId) { 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 932e2f8..224ea02 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 @@ -36,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)); + if (friendRepository.findByMemberAndFriend(member, friend).isPresent() || friendRepository.findByMemberAndFriend(friend, member).isPresent()) + throw new BusinessException(ErrorCode.ALREADY_EXIST_FRIEND); 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/error/ErrorCode.java b/src/main/java/com/project/mapdagu/error/ErrorCode.java index 4643aff..80063a5 100644 --- a/src/main/java/com/project/mapdagu/error/ErrorCode.java +++ b/src/main/java/com/project/mapdagu/error/ErrorCode.java @@ -25,7 +25,8 @@ public enum ErrorCode { ALREADY_LOGOUT_MEMBER(BAD_REQUEST, "이미 로그아웃한 회원입니다"), EMAIL_SEND_ERROR(BAD_REQUEST, "이메일 인증 코드 전송을 실패했습니다."), ALREADY_EXIST_EMAIL(BAD_REQUEST, "이미 존재하는 이메일입니다."), - ALREADY_EXIST_USERNAME(BAD_REQUEST, "이미 존재하는 사용자 이름입니다."); + ALREADY_EXIST_USERNAME(BAD_REQUEST, "이미 존재하는 사용자 이름입니다."), + ALREADY_EXIST_FRIEND(BAD_REQUEST, "이미 상대방과 친구입니다."); private final int code; private final String message;