Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

닉네임 중복 검사 기능 구현 #55

Merged
merged 9 commits into from
Feb 20, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import sixgaezzang.sidepeek.users.domain.Provider;
import sixgaezzang.sidepeek.users.dto.request.CheckEmailRequest;
import sixgaezzang.sidepeek.users.dto.request.CheckNicknameRequest;
import sixgaezzang.sidepeek.users.dto.request.SignUpRequest;
import sixgaezzang.sidepeek.users.dto.response.CheckDuplicateResponse;
import sixgaezzang.sidepeek.users.dto.response.UserSearchResponse;
Expand Down Expand Up @@ -75,4 +76,17 @@ public ResponseEntity<CheckDuplicateResponse> checkEmailDuplicate(
.body(response);
}

@PostMapping("/nickname/check")
@Operation(summary = "닉네임 중복 확인")
@ApiResponse(responseCode = "200", description = "닉네임 중복 확인 성공")
@Parameter(name = "nickname", description = "닉네임", example = "육개짱")
public ResponseEntity<CheckDuplicateResponse> checkNicknameDuplicate(
@RequestBody @Valid CheckNicknameRequest request
) {
CheckDuplicateResponse response = userService.checkNicknameDuplicate(request.nickname());

return ResponseEntity.ok()
.body(response);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class User extends BaseTimeEntity {

private static final int MAX_NICKNAME_LENGTH = 20;
public static final int MAX_NICKNAME_LENGTH = 20;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package sixgaezzang.sidepeek.users.dto.request;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;

public record CheckEmailRequest(
@Email
@NotBlank(message = "이메일을 입력해주세요.")
@Email(message = "이메일 형식이 올바르지 않습니다.")
String email
) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sixgaezzang.sidepeek.users.dto.request;

import static sixgaezzang.sidepeek.users.domain.User.MAX_NICKNAME_LENGTH;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public record CheckNicknameRequest(
@NotBlank(message = "닉네임을 입력해주세요.")
@Size(max = MAX_NICKNAME_LENGTH, message = "닉네임은 " + MAX_NICKNAME_LENGTH + "자 이하여야 합니다.")
String nickname
) {

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package sixgaezzang.sidepeek.users.dto.request;

import static sixgaezzang.sidepeek.users.domain.User.MAX_NICKNAME_LENGTH;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Length;
import jakarta.validation.constraints.Size;
import sixgaezzang.sidepeek.users.domain.Password;

public record SignUpRequest(
Expand All @@ -14,7 +16,7 @@ public record SignUpRequest(
@Pattern(regexp = Password.PASSWORD_REGXP, message = "비밀번호는 8자 이상이며 영문, 숫자, 특수문자를 포함해야 합니다.")
String password,
@NotBlank(message = "닉네임을 입력해주세요.")
@Length(max = 20, message = "닉네임은 20자 이하여야 합니다.")
@Size(max = MAX_NICKNAME_LENGTH, message = "닉네임은 " + MAX_NICKNAME_LENGTH + "자 이하여야 합니다.")
String nickname
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ public UserSearchResponse searchByNickname(String keyword) {
return UserSearchResponse.from(userRepository.findAllByNicknameContaining(keyword));
}

public CheckDuplicateResponse checkEmailDuplicate(String email) {
validateEmail(email, "이메일 형식이 올바르지 않습니다.");

boolean isExists = userRepository.existsByEmail(email);
return new CheckDuplicateResponse(isExists);
}

public CheckDuplicateResponse checkNicknameDuplicate(String nickname) {
validateMaxLength(nickname, User.MAX_NICKNAME_LENGTH,
"닉네임은 " + User.MAX_NICKNAME_LENGTH + "자 이하여야 합니다.");

boolean isExists = userRepository.existsByNickname(nickname);
return new CheckDuplicateResponse(isExists);
}

private void verifyUniqueNickname(SignUpRequest request) {
if (userRepository.existsByNickname(request.nickname())) {
throw new EntityExistsException("이미 사용 중인 닉네임입니다.");
Expand All @@ -70,11 +85,4 @@ private void verifyUniqueEmail(SignUpRequest request) {
throw new EntityExistsException("이미 사용 중인 이메일입니다.");
}
}

public CheckDuplicateResponse checkEmailDuplicate(String email) {
validateEmail(email, "이메일 형식이 올바르지 않습니다.");

boolean isExists = userRepository.existsByEmail(email);
return new CheckDuplicateResponse(isExists);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,50 @@ class 이메일_중복_확인_테스트 {
}
}

@Nested
class 닉네임_중복_확인_테스트 {

@Test
void 닉네임이_중복되지_않은_경우_중복_확인에_성공한다() {
// when
CheckDuplicateResponse response = userService.checkNicknameDuplicate(nickname);

// then
assertThat(response.isDuplicated()).isFalse();
}

@Test
void 닉네임이_중복된_경우_중복_확인에_성공한다() {
// given
String duplicatedNickname = nickname;
User user = createUser(email, password, duplicatedNickname);
userRepository.save(user);

// when
CheckDuplicateResponse response = userService.checkNicknameDuplicate(
duplicatedNickname);

// then
assertThat(response.isDuplicated()).isTrue();
}

@Test
void 닉네임이_최대_길이를_초과하는_경우_중복_확인에_실패한다() {
// given
String longNickname = faker.lorem()
.characters(User.MAX_NICKNAME_LENGTH + 1);
Comment on lines +209 to +210
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


// when
ThrowingCallable checkNicknameDuplicate = () -> userService.checkNicknameDuplicate(
longNickname);

// then
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(
checkNicknameDuplicate)
.withMessage("닉네임은 " + User.MAX_NICKNAME_LENGTH + "자 이하여야 합니다.");
}
}

private User createUser(String email, String password, String nickname) {
return User.builder()
.email(isBlank(email) ? this.email : email)
Expand Down
Loading