diff --git a/src/main/java/yerong/wedle/member/controller/MemberApiController.java b/src/main/java/yerong/wedle/member/controller/MemberApiController.java index b7765e5..c61174d 100644 --- a/src/main/java/yerong/wedle/member/controller/MemberApiController.java +++ b/src/main/java/yerong/wedle/member/controller/MemberApiController.java @@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.*; import yerong.wedle.department.dto.DepartmentResponse; import yerong.wedle.department.service.DepartmentService; +import yerong.wedle.member.dto.NicknameDuplicateResponse; import yerong.wedle.member.dto.NicknameRequest; import yerong.wedle.member.dto.NicknameResponse; import yerong.wedle.member.service.MemberService; @@ -47,6 +48,16 @@ public ResponseEntity updateNickname(@RequestBody NicknameRequ return ResponseEntity.ok(memberService.setNickname(nicknameRequest)); } + @Operation(summary = "닉네임 중복 체크", description = "닉네임의 중복 여부를 확인합니다") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "닉네임 중복 확인 성공") + }) + @GetMapping("/nickname/check") + public ResponseEntity checkNicknameDuplicate(@RequestParam String nickname) { + NicknameDuplicateResponse nicknameDuplicateResponse = memberService.checkNicknameDuplicate(nickname); + return ResponseEntity.ok(nicknameDuplicateResponse); + } + @GetMapping("/nickname") public ResponseEntity getNickname() { return ResponseEntity.ok(memberService.getNickname()); diff --git a/src/main/java/yerong/wedle/member/dto/NicknameDuplicateResponse.java b/src/main/java/yerong/wedle/member/dto/NicknameDuplicateResponse.java new file mode 100644 index 0000000..e036169 --- /dev/null +++ b/src/main/java/yerong/wedle/member/dto/NicknameDuplicateResponse.java @@ -0,0 +1,11 @@ +package yerong.wedle.member.dto; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class NicknameDuplicateResponse { + private final boolean isDuplicate; + private final String message; +} diff --git a/src/main/java/yerong/wedle/member/service/MemberService.java b/src/main/java/yerong/wedle/member/service/MemberService.java index 15a8e16..0c6c677 100644 --- a/src/main/java/yerong/wedle/member/service/MemberService.java +++ b/src/main/java/yerong/wedle/member/service/MemberService.java @@ -4,6 +4,7 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import yerong.wedle.member.domain.Member; +import yerong.wedle.member.dto.NicknameDuplicateResponse; import yerong.wedle.member.dto.NicknameRequest; import yerong.wedle.member.dto.NicknameResponse; import yerong.wedle.member.exception.ExistingNicknameException; @@ -56,4 +57,29 @@ private String getCurrentUserId() { return socialId; } + + public NicknameDuplicateResponse checkNicknameDuplicate(String nickname) { + String socialId = getCurrentUserId(); + Member member = memberRepository.findBySocialId(socialId) + .orElseThrow(MemberNotFoundException::new); + + String message = ""; + boolean isDuplicate = false; + + if (member.getNickname() != null && member.getNickname().equals(nickname)) { + message = "기존 닉네임과 동일합니다."; + isDuplicate = true; + } else if (isNicknameDuplicate(nickname)) { + message = "이미 존재하는 닉네임입니다."; + isDuplicate = true; + } else { + message = "사용 가능한 닉네임입니다."; + isDuplicate = false; + } + + return NicknameDuplicateResponse.builder() + .isDuplicate(isDuplicate) + .message(message) + .build(); + } }