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

내 연락수단 변경 API 구현 #85

Merged
merged 12 commits into from
Mar 21, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -114,4 +115,21 @@ public ResponseEntity<UserResponse> signUpV1(
.created(URI.create("/users/" + userCreated.getId()))
.body(UserResponse.from(userCreated));
}

@Operation(
summary = "연락 수단 변경",
description = "연락 수단을 변경합니다."
)
Wo-ogie marked this conversation as resolved.
Show resolved Hide resolved
@PutMapping(value = "/me/contact-link", headers = API_VERSION_HEADER_NAME + "=" + 1)
public ResponseEntity<Void> updateContactLinkV1(
@Parameter(
description = "변경하고자 하는 연락 수단 링크를 입력합니다.",
example = "https://example.com/1234"
) @RequestParam @NotBlank String contactLink,
@AuthenticationPrincipal UserPrincipal userPrincipal
) {
userCommandService.updateContactLink(userPrincipal.getUserId(), contactLink);
return ResponseEntity.ok().build();
Wo-ogie marked this conversation as resolved.
Show resolved Hide resolved
}

}
3 changes: 3 additions & 0 deletions src/main/java/com/ajou/hertz/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,7 @@ public static User create(
profileImageUrl, birth, gender, phone, null
);
}

public void setContactLink(String contactLink) {
}
Wo-ogie marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.ajou.hertz.domain.user.entity.User;
import com.ajou.hertz.domain.user.exception.UserEmailDuplicationException;
import com.ajou.hertz.domain.user.exception.UserKakaoUidDuplicationException;
import com.ajou.hertz.domain.user.exception.UserNotFoundByIdException;
import com.ajou.hertz.domain.user.exception.UserPhoneDuplicationException;
import com.ajou.hertz.domain.user.repository.UserRepository;

Expand Down Expand Up @@ -129,4 +130,18 @@ private String generateRandom16CharString() {
.toString()
.substring(0, 16);
}

/**
* 연락 수단을 변경합니다.
*
* @param userId 유저 ID
* @param contactLink 변경할 연락 수단
* @throws UserNotFoundByIdException 해당하는 유저를 찾을 수 없는 경우
*/
public void updateContactLink(Long userId, String contactLink) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new UserNotFoundByIdException(userId));
Wo-ogie marked this conversation as resolved.
Show resolved Hide resolved
user.setContactLink(contactLink);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.ajou.hertz.domain.user.controller.UserController;
import com.ajou.hertz.domain.user.dto.UserDto;
import com.ajou.hertz.domain.user.dto.request.SignUpRequest;
import com.ajou.hertz.domain.user.dto.request.UpdateContactLinkRequest;
import com.ajou.hertz.domain.user.service.UserCommandService;
import com.ajou.hertz.domain.user.service.UserQueryService;
import com.ajou.hertz.util.ReflectionUtils;
Expand Down Expand Up @@ -212,6 +213,28 @@ public void securitySetUp() throws Exception {
verifyEveryMocksShouldHaveNoMoreInteractions();
}

@Test
void 주어진_연락수단을_새로운_연락수단으로_변경한다() throws Exception {
// given
long userId = 1L;
String newContactLink = "https://new-contact-link.com";
UpdateContactLinkRequest updateContactLinkRequest = new UpdateContactLinkRequest(newContactLink);
UserDetails userDetails = createTestUser(userId);

Wo-ogie marked this conversation as resolved.
Show resolved Hide resolved
// when & then
mvc.perform(
put("/api/users/me/contact-link")
.header(API_VERSION_HEADER_NAME, 1)
//.contentType(MediaType.APPLICATION_JSON)
.param("contactLink", newContactLink)
.with(user(userDetails))
)
.andExpect(status().isOk());

then(userCommandService).should().updateContactLink(userId, newContactLink);
verifyEveryMocksShouldHaveNoMoreInteractions();
}

private void verifyEveryMocksShouldHaveNoMoreInteractions() {
then(userCommandService).shouldHaveNoMoreInteractions();
then(userQueryService).shouldHaveNoMoreInteractions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.mockito.BDDMockito.*;

import java.time.LocalDate;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

Expand All @@ -29,6 +30,7 @@
import com.ajou.hertz.domain.user.entity.User;
import com.ajou.hertz.domain.user.exception.UserEmailDuplicationException;
import com.ajou.hertz.domain.user.exception.UserKakaoUidDuplicationException;
import com.ajou.hertz.domain.user.exception.UserNotFoundByIdException;
import com.ajou.hertz.domain.user.exception.UserPhoneDuplicationException;
import com.ajou.hertz.domain.user.repository.UserRepository;
import com.ajou.hertz.domain.user.service.UserCommandService;
Expand Down Expand Up @@ -171,6 +173,39 @@ static Stream<Arguments> testDataForCreateNewUserWithKakao() throws Exception {
assertThat(t).isInstanceOf(UserKakaoUidDuplicationException.class);
}

@Test
void 주어진_유저_ID와_연락_수단으로_연락_수단을_변경한다() throws Exception {
// given
Long userId = 1L;
String contactLink = "https://contackLink";
Wo-ogie marked this conversation as resolved.
Show resolved Hide resolved
User user = createUser(userId, "$2a$abc123", "12345");
given(userRepository.findById(userId)).willReturn(Optional.of(user));

// when
sut.updateContactLink(userId, contactLink);

// then
then(userRepository).should().findById(userId);
verifyEveryMocksShouldHaveNoMoreInteractions();
assertThat(user.getContactLink()).isEqualTo(contactLink);
}

@Test
void 주어진_유저_ID와_연락_수단으로_연락_수단을_변경한다_존재하지_않는_유저라면_예외가_발생한다() throws Exception {
// given
Long userId = 1L;
String contactLink = "https://contackLink";
Wo-ogie marked this conversation as resolved.
Show resolved Hide resolved
given(userRepository.findById(userId)).willReturn(Optional.empty());

// when
Throwable t = catchThrowable(() -> sut.updateContactLink(userId, contactLink));

// then
then(userRepository).should().findById(userId);
verifyEveryMocksShouldHaveNoMoreInteractions();
assertThat(t).isInstanceOf(UserNotFoundByIdException.class);
}

private void verifyEveryMocksShouldHaveNoMoreInteractions() {
then(userQueryService).shouldHaveNoMoreInteractions();
then(userRepository).shouldHaveNoMoreInteractions();
Expand All @@ -188,7 +223,7 @@ private static User createUser(Long id, String password, String kakaoUid, Gender
LocalDate.of(2024, 1, 1),
gender,
"010-1234-5678",
null
"https://contackLink"
Wo-ogie marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand Down
Loading