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

이메일 중복 확인 기능 구현 #53

Merged
merged 10 commits into from
Feb 20, 2024
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sixgaezzang.sidepeek.skill.serivce;

import static sixgaezzang.sidepeek.common.ValidationUtils.validateMaxLength;
import static sixgaezzang.sidepeek.common.util.ValidationUtils.validateMaxLength;

import java.util.Objects;
import lombok.RequiredArgsConstructor;
Expand All @@ -24,7 +24,7 @@ public SkillSearchResponse searchByName(String keyword) {
}

validateMaxLength(keyword, KEYWORD_MAX_LENGTH,
"최대 " + KEYWORD_MAX_LENGTH + "자의 키워드로 검색할 수 있습니다.");
"최대 " + KEYWORD_MAX_LENGTH + "자의 키워드로 검색할 수 있습니다.");
Copy link
Member

Choose a reason for hiding this comment

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

요 부분은 제가 MAX_SKILL_NAME_LENGTH라는 상수 만들어서 적용했는데 머지하고 나서 제가 확인해볼게유! 코드 탭 정돈까지 해주셔서 감사합니당!!🥰

Copy link
Contributor Author

Choose a reason for hiding this comment

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

충돌해결하면서 세희님 코드로 변경했습니다! 감사합니당!!🥰


return SkillSearchResponse.from(skillRepository.findAllByNameContaining(keyword));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jakarta.validation.constraints.Size;
import java.net.URI;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -18,14 +19,17 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import sixgaezzang.sidepeek.users.domain.Provider;
import sixgaezzang.sidepeek.users.dto.SignUpRequest;
import sixgaezzang.sidepeek.users.dto.UserSearchResponse;
import sixgaezzang.sidepeek.users.dto.request.CheckEmailRequest;
import sixgaezzang.sidepeek.users.dto.request.SignUpRequest;
import sixgaezzang.sidepeek.users.dto.response.CheckDuplicateResponse;
import sixgaezzang.sidepeek.users.dto.response.UserSearchResponse;
import sixgaezzang.sidepeek.users.service.UserService;

@RestController
@RequestMapping("/users")
@Tag(name = "User", description = "User API")
@RequiredArgsConstructor
@Slf4j
Copy link
Member

Choose a reason for hiding this comment

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

혹시 요 클래스에서 로그 사용하는 코드가 있을까요! 제가 확인했을 땐 보이지가 않아서 여쭤봅니당!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

제가 디버그하려고 넣어놨다가 삭제하는 것을 까먹었네용!
감사합니다:)

public class UserController {

private final UserService userService;
Expand Down Expand Up @@ -58,4 +62,17 @@ public ResponseEntity<UserSearchResponse> searchByNickname(
.body(userService.searchByNickname(keyword));
}

@PostMapping("/email/check")
@Operation(summary = "이메일 중복 확인")
@ApiResponse(responseCode = "200", description = "이메일 중복 확인 성공")
@Parameter(name = "email", description = "이메일", example = "[email protected]")
public ResponseEntity<CheckDuplicateResponse> checkEmailDuplicate(
@RequestBody @Valid CheckEmailRequest request
) {
CheckDuplicateResponse response = userService.checkEmailDuplicate(request.email());

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

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

import jakarta.validation.constraints.Email;

public record CheckEmailRequest(
@Email
String email
) {

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

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sixgaezzang.sidepeek.users.dto.response;

public record CheckDuplicateResponse(
boolean isDuplicated
) {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sixgaezzang.sidepeek.users.dto;
package sixgaezzang.sidepeek.users.dto.response;

import java.util.List;
import sixgaezzang.sidepeek.users.domain.User;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sixgaezzang.sidepeek.users.dto;
package sixgaezzang.sidepeek.users.dto.response;

import lombok.Builder;
import sixgaezzang.sidepeek.users.domain.User;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sixgaezzang.sidepeek.users.service;

import static sixgaezzang.sidepeek.common.ValidationUtils.validateMaxLength;
import static sixgaezzang.sidepeek.common.util.ValidationUtils.validateEmail;
import static sixgaezzang.sidepeek.common.util.ValidationUtils.validateMaxLength;

import jakarta.persistence.EntityExistsException;
import java.util.Objects;
Expand All @@ -11,8 +12,9 @@
import sixgaezzang.sidepeek.users.domain.Password;
import sixgaezzang.sidepeek.users.domain.Provider;
import sixgaezzang.sidepeek.users.domain.User;
import sixgaezzang.sidepeek.users.dto.SignUpRequest;
import sixgaezzang.sidepeek.users.dto.UserSearchResponse;
import sixgaezzang.sidepeek.users.dto.request.SignUpRequest;
import sixgaezzang.sidepeek.users.dto.response.CheckDuplicateResponse;
import sixgaezzang.sidepeek.users.dto.response.UserSearchResponse;
import sixgaezzang.sidepeek.users.repository.UserRepository;

@Service
Expand Down Expand Up @@ -52,7 +54,7 @@ public UserSearchResponse searchByNickname(String keyword) {
}

validateMaxLength(keyword, KEYWORD_MAX_LENGTH,
"최대 " + KEYWORD_MAX_LENGTH + "자의 키워드로 검색할 수 있습니다.");
"최대 " + KEYWORD_MAX_LENGTH + "자의 키워드로 검색할 수 있습니다.");

return UserSearchResponse.from(userRepository.findAllByNicknameContaining(keyword));
}
Expand All @@ -69,4 +71,10 @@ private void verifyUniqueEmail(SignUpRequest request) {
}
}

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 @@ -19,7 +19,8 @@
import sixgaezzang.sidepeek.users.domain.Password;
import sixgaezzang.sidepeek.users.domain.Provider;
import sixgaezzang.sidepeek.users.domain.User;
import sixgaezzang.sidepeek.users.dto.SignUpRequest;
import sixgaezzang.sidepeek.users.dto.request.SignUpRequest;
import sixgaezzang.sidepeek.users.dto.response.CheckDuplicateResponse;
import sixgaezzang.sidepeek.users.repository.UserRepository;

@SpringBootTest
Expand Down Expand Up @@ -133,6 +134,48 @@ class 회원가입_테스트 {
}
}

@Nested
class 이메일_중복_확인_테스트 {

@Test
void 이메일이_중복되지_않은_경우_중복_확인에_성공한다() {
// when
CheckDuplicateResponse response = userService.checkEmailDuplicate(email);

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

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

// when
CheckDuplicateResponse response = userService.checkEmailDuplicate(duplicatedEmail);

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

@Test
void 이메일_형식이_올바르지_않은_경우_중복_확인에_실패한다() {
// given
String invalidEmail = "invalid-email";

// when
ThrowingCallable checkEmailDuplicate = () -> userService.checkEmailDuplicate(
invalidEmail);

// then
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(
checkEmailDuplicate)
.withMessage("이메일 형식이 올바르지 않습니다.");
}
}

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