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

[feat] : 회원가입 api 분리 #165

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import dev.handsup.auth.jwt.JwtAuthorization;
import dev.handsup.common.dto.PageResponse;
import dev.handsup.user.domain.User;
import dev.handsup.user.dto.request.JoinUserRequest;
import dev.handsup.user.dto.request.JoinUserCredentialsRequest;
import dev.handsup.user.dto.request.JoinUserProfileRequest;
import dev.handsup.user.dto.response.EmailAvailabilityResponse;
import dev.handsup.user.dto.response.JoinUserResponse;
import dev.handsup.user.dto.response.UserBuyHistoryResponse;
Expand All @@ -39,13 +40,25 @@ public class UserApiController {
private final UserService userService;

@NoAuth
@PostMapping("/api/users")
@Operation(summary = "회원가입 API", description = "회원가입을 한다")
@PostMapping("/api/users/step1")
@Operation(summary = "회원가입 API(Email, Pw)", description = "회원가입을 한다")
@ApiResponse(useReturnTypeSchema = true)
public ResponseEntity<JoinUserResponse> join(
final @Valid @RequestBody JoinUserRequest request
public ResponseEntity<JoinUserResponse> joinStep1(
final @Valid @RequestBody JoinUserCredentialsRequest request
) {
Long userId = userService.join(request);
Long userId = userService.joinCredentials(request);
JoinUserResponse response = JoinUserResponse.from(userId);
return ResponseEntity.ok(response);
}

@NoAuth
@PostMapping("/api/users/step2")
@Operation(summary = "회원가입 API(프로필)", description = "회원가입을 한다")
@ApiResponse(useReturnTypeSchema = true)
public ResponseEntity<JoinUserResponse> joinStep2(
final @Valid @RequestBody JoinUserProfileRequest request
) {
Long userId = userService.joinProfile(request);
JoinUserResponse response = JoinUserResponse.from(userId);
return ResponseEntity.ok(response);
}
Expand Down
22 changes: 17 additions & 5 deletions api/src/test/java/dev/handsup/common/support/ApiTestSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
import dev.handsup.support.DatabaseCleanerExtension;
import dev.handsup.support.TestContainerSupport;
import dev.handsup.user.domain.User;
import dev.handsup.user.dto.request.JoinUserRequest;
import dev.handsup.user.dto.request.JoinUserCredentialsRequest;
import dev.handsup.user.dto.request.JoinUserProfileRequest;
import dev.handsup.user.repository.UserRepository;
import jakarta.annotation.PostConstruct;
import jakarta.servlet.http.Cookie;
Expand Down Expand Up @@ -78,9 +79,13 @@ public void setUpUser() throws Exception {
return;
}

JoinUserRequest joinUserRequest = JoinUserRequest.of(
JoinUserCredentialsRequest joinUserCredentialRequest = JoinUserCredentialsRequest.of(
user.getEmail(),
user.getPassword()
);

JoinUserProfileRequest joinUserProfileRequest = JoinUserProfileRequest.of(
user.getEmail(),
user.getPassword(),
user.getNickname(),
user.getAddress().getSi(),
user.getAddress().getGu(),
Expand All @@ -93,9 +98,16 @@ public void setUpUser() throws Exception {

mockMvc.perform(
MockMvcRequestBuilders
.post("/api/users")
.post("/api/users/step1")
.contentType(APPLICATION_JSON)
.content(toJson(joinUserCredentialRequest))
);

mockMvc.perform(
MockMvcRequestBuilders
.post("/api/users/step2")
.contentType(APPLICATION_JSON)
.content(toJson(joinUserRequest))
.content(toJson(joinUserProfileRequest))
);

LoginRequest loginRequest = LoginRequest.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
import dev.handsup.review.repository.ReviewLabelRepository;
import dev.handsup.review.repository.ReviewRepository;
import dev.handsup.user.domain.User;
import dev.handsup.user.dto.request.JoinUserRequest;
import dev.handsup.user.dto.request.JoinUserCredentialsRequest;
import dev.handsup.user.dto.request.JoinUserProfileRequest;
import dev.handsup.user.repository.UserRepository;
import dev.handsup.user.repository.UserReviewLabelRepository;

Expand All @@ -65,10 +66,34 @@ class UserApiControllerTest extends ApiTestSupport {

@Test
@DisplayName("[[회원가입 API] 회원이 등록되고 회원 ID를 응답한다]")
void joinUserTest() throws Exception {
JoinUserRequest joinUserRequest = JoinUserRequest.of(
void joinStep1Test() throws Exception {
/*
Step1
*/
JoinUserCredentialsRequest joinUserCredentialRequest = JoinUserCredentialsRequest.of(
"[email protected]",
user.getPassword()
);

// when
ResultActions actions = mockMvc.perform(
post("/api/users/step1")
.contentType(APPLICATION_JSON)
.content(toJson(joinUserCredentialRequest))
);

// then
actions.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print())
.andExpect(jsonPath("$.userId").exists());

assertThat(userRepository.findByEmail(user.getEmail())).isPresent();

/*
Step2
*/
JoinUserProfileRequest joinUserProfileRequest = JoinUserProfileRequest.of(
"[email protected]",
user.getPassword(),
user.getNickname(),
user.getAddress().getSi(),
user.getAddress().getGu(),
Expand All @@ -77,10 +102,10 @@ void joinUserTest() throws Exception {
List.of(1L)
);
// when
ResultActions actions = mockMvc.perform(
post("/api/users")
actions = mockMvc.perform(
post("/api/users/step2")
.contentType(APPLICATION_JSON)
.content(toJson(joinUserRequest))
.content(toJson(joinUserProfileRequest))
);

// then
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/dev/handsup/user/domain/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
@Getter
@NoArgsConstructor(access = PROTECTED)
public class Address {
@Column(name = "si", nullable = false)
@Column(name = "si")
private String si;

@Column(name = "gu", nullable = false)
@Column(name = "gu")
private String gu;

@Column(name = "dong", nullable = false)
@Column(name = "dong")
private String dong;

@Builder
Expand Down
21 changes: 9 additions & 12 deletions core/src/main/java/dev/handsup/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class User extends TimeBaseEntity {
@Column(name = "password", nullable = false)
private String password;

@Column(name = "nickname", nullable = false)
@Column(name = "nickname")
private String nickname;

@Column(name = "score", nullable = false)
Expand Down Expand Up @@ -70,6 +70,7 @@ private User(
this.profileImageUrl = profileImageUrl;
}

//==테스트용 생성자==//
private User(
Long id,
String email,
Expand All @@ -87,21 +88,13 @@ private User(
this.profileImageUrl = profileImageUrl;
}

//==테스트용 생성자==//

public static User of(
String email,
String password,
String nickname,
Address address,
String profileImageUrl
String password
) {
return User.builder()
.email(email)
.password(password)
.nickname(nickname)
.address(address)
.profileImageUrl(profileImageUrl)
.build();
}

Expand All @@ -124,8 +117,6 @@ private void validateUser(
) {
Assert.hasText(email, getNotEmptyMessage("User", "email"));
Assert.hasText(password, getNotEmptyMessage("User", "password"));
Assert.hasText(nickname, getNotEmptyMessage("User", "nickname"));
Assert.notNull(address, getNotNullMessage("User", "address"));
// 이메일 패턴 검증
String emailRegex = "^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z]){0,19}"
+ "@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z]){0,19}[.][a-zA-Z]{2,3}$";
Expand All @@ -148,4 +139,10 @@ public void operateScore(int evaluationScore) {
score = 200;
}
}

public void updateProfile(String nickname, Address address, String profileImageUrl) {
this.nickname = nickname;
this.address = address;
this.profileImageUrl = profileImageUrl;
}
}
10 changes: 3 additions & 7 deletions core/src/main/java/dev/handsup/user/dto/UserMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import dev.handsup.review.domain.UserReviewLabel;
import dev.handsup.user.domain.Address;
import dev.handsup.user.domain.User;
import dev.handsup.user.dto.request.JoinUserRequest;
import dev.handsup.user.dto.request.JoinUserCredentialsRequest;
import dev.handsup.user.dto.response.UserBuyHistoryResponse;
import dev.handsup.user.dto.response.UserReviewLabelResponse;
import dev.handsup.user.dto.response.UserReviewResponse;
Expand All @@ -20,17 +20,13 @@
@NoArgsConstructor(access = PRIVATE)
public class UserMapper {

public static User toUser(JoinUserRequest request, EncryptHelper encryptHelper) {
public static User toUser(JoinUserCredentialsRequest request, EncryptHelper encryptHelper) {
String password = request.password();
String encryptedPassword = encryptHelper.encrypt(password);
Address address = Address.of(request.si(), request.gu(), request.dong());

return User.of(
request.email(),
encryptedPassword,
request.nickname(),
address,
request.profileImageUrl()
encryptedPassword
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.handsup.user.dto.request;

import static lombok.AccessLevel.*;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.Builder;

@Builder(access = PRIVATE)
public record JoinUserCredentialsRequest(

@NotBlank(message = "email 은 필수입니다.")
@Pattern(regexp = "^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z]){0,19}"
+ "@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z]){0,19}[.][a-zA-Z]{2,3}$",
message = "이메일 주소 양식을 확인해주세요.")
String email,

@NotBlank(message = "password 는 필수입니다.")
String password
) {
public static JoinUserCredentialsRequest of(
String email,
String password
) {
return JoinUserCredentialsRequest.builder()
.email(email)
.password(password)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
import lombok.Builder;

@Builder(access = PRIVATE)
public record JoinUserRequest(
public record JoinUserProfileRequest(

@NotBlank(message = "email 은 필수입니다.")
@Pattern(regexp = "^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z]){0,19}"
+ "@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z]){0,19}[.][a-zA-Z]{2,3}$",
message = "이메일 주소 양식을 확인해주세요.")
String email,

@NotBlank(message = "password 는 필수입니다.")
String password,

@NotBlank(message = "nickname 은 필수입니다.")
String nickname,

Expand All @@ -37,19 +34,17 @@ public record JoinUserRequest(

List<Long> productCategoryIds
) {
public static JoinUserRequest of(
public static JoinUserProfileRequest of(
String email,
String password,
String nickname,
String si,
String gu,
String dong,
String profileImageUrl,
List<Long> productCategoryIds
) {
return JoinUserRequest.builder()
return JoinUserProfileRequest.builder()
.email(email)
.password(password)
.nickname(nickname)
.si(si)
.gu(gu)
Expand Down
23 changes: 19 additions & 4 deletions core/src/main/java/dev/handsup/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import dev.handsup.common.exception.NotFoundException;
import dev.handsup.common.exception.ValidationException;
import dev.handsup.review.repository.ReviewRepository;
import dev.handsup.user.domain.Address;
import dev.handsup.user.domain.User;
import dev.handsup.user.dto.UserMapper;
import dev.handsup.user.dto.request.JoinUserRequest;
import dev.handsup.user.dto.request.JoinUserCredentialsRequest;
import dev.handsup.user.dto.request.JoinUserProfileRequest;
import dev.handsup.user.dto.response.EmailAvailabilityResponse;
import dev.handsup.user.dto.response.UserBuyHistoryResponse;
import dev.handsup.user.dto.response.UserProfileResponse;
Expand Down Expand Up @@ -65,19 +67,32 @@ public User getUserByEmail(String email) {
}

@Transactional
public Long join(JoinUserRequest request) {
public Long joinCredentials(JoinUserCredentialsRequest request) {
validateDuplicateEmail(request.email());
User user = UserMapper.toUser(request, encryptHelper);
User savedUser = userRepository.save(user); // 저장된 유저 확인

return savedUser.getId();
}

@Transactional
public Long joinProfile(JoinUserProfileRequest request) {
User user = getUserByEmail(request.email());

user.updateProfile(
request.nickname(),
Address.of(request.si(), request.gu(), request.dong()),
request.profileImageUrl()
);

request.productCategoryIds().forEach(productCategoryId -> {
ProductCategory productCategory = getProductCategoryById(productCategoryId);
preferredProductCategoryRepository.save(
PreferredProductCategory.of(savedUser, productCategory)
PreferredProductCategory.of(user, productCategory)
);
});

return savedUser.getId();
return user.getId();
}

public EmailAvailabilityResponse isEmailAvailable(String email) {
Expand Down
Loading
Loading