diff --git a/src/main/java/com/depromeet/domain/follow/application/FollowService.java b/src/main/java/com/depromeet/domain/follow/application/FollowService.java index 791383809..ecb6eef42 100644 --- a/src/main/java/com/depromeet/domain/follow/application/FollowService.java +++ b/src/main/java/com/depromeet/domain/follow/application/FollowService.java @@ -38,6 +38,7 @@ public class FollowService { public void createFollow(FollowCreateRequest request) { final Member currentMember = memberUtil.getCurrentMember(); + validateSelfFollow(currentMember.getId(), request.targetId()); Member targetMember = getTargetMember(request.targetId()); boolean existMemberRelation = @@ -177,6 +178,12 @@ && isToday(record.getStartedAt())) return result; } + private void validateSelfFollow(Long expectedId, Long actualId) { + if (expectedId.equals(actualId)) { + throw new CustomException(ErrorCode.FOLLOW_SELF_NOT_ALLOWED); + } + } + private boolean isToday(LocalDateTime dateTime) { LocalDateTime today = LocalDateTime.now(); return dateTime.toLocalDate().isEqual(today.toLocalDate()); diff --git a/src/main/java/com/depromeet/global/error/exception/ErrorCode.java b/src/main/java/com/depromeet/global/error/exception/ErrorCode.java index f4a49109f..69d9f7906 100644 --- a/src/main/java/com/depromeet/global/error/exception/ErrorCode.java +++ b/src/main/java/com/depromeet/global/error/exception/ErrorCode.java @@ -49,6 +49,7 @@ public enum ErrorCode { FOLLOW_TARGET_MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "타겟 유저을 찾을 수 없습니다."), FOLLOW_ALREADY_EXIST(HttpStatus.BAD_REQUEST, "이미 팔로우 중인 회원입니다."), FOLLOW_NOT_EXIST(HttpStatus.BAD_REQUEST, "팔로우 중인 회원만 팔로우 취소가 가능합니다."), + FOLLOW_SELF_NOT_ALLOWED(HttpStatus.CONFLICT, "본인을 팔로우 할 수 없습니다."), // Image IMAGE_KEY_NOT_FOUND(HttpStatus.NOT_FOUND, "이미지 키를 찾을 수 없습니다."), diff --git a/src/test/java/com/depromeet/domain/follow/application/FollowServiceTest.java b/src/test/java/com/depromeet/domain/follow/application/FollowServiceTest.java index c64bbb0df..7fb5a3cf1 100644 --- a/src/test/java/com/depromeet/domain/follow/application/FollowServiceTest.java +++ b/src/test/java/com/depromeet/domain/follow/application/FollowServiceTest.java @@ -68,6 +68,19 @@ class 팔로우를_추가할_때 { .hasMessage(ErrorCode.MEMBER_NOT_FOUND.getMessage()); } + @Test + void 본인을_팔로우_할_경우_예외를_발생시킨다() { + FollowCreateRequest request = new FollowCreateRequest(1L); + memberRepository.save( + Member.createNormalMember( + Profile.createProfile("testNickname1", "testImageUrl1"))); + + // when, then + assertThatThrownBy(() -> followService.createFollow(request)) + .isInstanceOf(CustomException.class) + .hasMessage(ErrorCode.FOLLOW_SELF_NOT_ALLOWED.getMessage()); + } + @Test void 타겟회원이_존재하지_않는다면_예외를_발생시킨다() { // given