diff --git a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java index d3c4ed7..b9d4db3 100644 --- a/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java +++ b/src/main/java/com/ajou/hertz/domain/user/controller/UserController.java @@ -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; @@ -18,6 +19,7 @@ import com.ajou.hertz.common.validator.PhoneNumber; 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.dto.response.UserEmailResponse; import com.ajou.hertz.domain.user.dto.response.UserExistenceResponse; import com.ajou.hertz.domain.user.dto.response.UserResponse; @@ -114,4 +116,19 @@ public ResponseEntity signUpV1( .created(URI.create("/users/" + userCreated.getId())) .body(UserResponse.from(userCreated)); } -} + + @Operation( + summary = "연락 수단 변경", + description = "연락 수단을 변경합니다.", + security = @SecurityRequirement(name = "access-token") + ) + @PutMapping(value = "/me/contact-link", headers = API_VERSION_HEADER_NAME + "=" + 1) + public UserResponse updateContactLinkV1( + @RequestBody @Valid UpdateContactLinkRequest updateContactLinkRequest, + @AuthenticationPrincipal UserPrincipal userPrincipal + ) { + UserDto userUpdated = userCommandService.updateContactLink(userPrincipal.getUserId(), + updateContactLinkRequest.getContactLink()); + return UserResponse.from(userUpdated); + } +} \ No newline at end of file diff --git a/src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java b/src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java new file mode 100644 index 0000000..d0c69f3 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/user/dto/request/UpdateContactLinkRequest.java @@ -0,0 +1,19 @@ +package com.ajou.hertz.domain.user.dto.request; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@AllArgsConstructor(access = AccessLevel.PRIVATE) +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Getter +public class UpdateContactLinkRequest { + + @Schema(description = "연락 수단", example = "https://new-contack-link") + @NotBlank + private String contactLink; + +} diff --git a/src/main/java/com/ajou/hertz/domain/user/entity/User.java b/src/main/java/com/ajou/hertz/domain/user/entity/User.java index f900e55..0be60e1 100644 --- a/src/main/java/com/ajou/hertz/domain/user/entity/User.java +++ b/src/main/java/com/ajou/hertz/domain/user/entity/User.java @@ -100,4 +100,9 @@ public static User create( profileImageUrl, birth, gender, phone, null ); } + + public void changeContactLink(String contactLink) { + this.contactLink = contactLink; + } + } diff --git a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java index 59bb57d..ff64dcf 100644 --- a/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java +++ b/src/main/java/com/ajou/hertz/domain/user/service/UserCommandService.java @@ -129,4 +129,19 @@ private String generateRandom16CharString() { .toString() .substring(0, 16); } + + /** + *연락 수단을 변경합니다. + * + * @param userId 유저의 ID + * @param contactLink 변경할 연락 수단 + * + *@return 변경된 유저 정보 + */ + public UserDto updateContactLink(Long userId, String contactLink) { + User user = userQueryService.getById(userId); + user.changeContactLink(contactLink); + return UserDto.from(user); + } + } diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java index 281631b..6e46499 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/controller/UserControllerTest.java @@ -212,6 +212,29 @@ public void securitySetUp() throws Exception { verifyEveryMocksShouldHaveNoMoreInteractions(); } + @Test + void 주어진_연락수단을_새로운_연락수단으로_변경한다() throws Exception { + // given + long userId = 1L; + String newContactLink = "https://new-contact-link.com"; + UserDto expectedResult = createUserDto(userId); + UserDetails testUser = createTestUser(userId); + given(userCommandService.updateContactLink(userId, newContactLink)).willReturn(expectedResult); + + // when & then + mvc.perform( + put("/api/users/me/contact-link") + .header(API_VERSION_HEADER_NAME, 1) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(newContactLink)) + .with(user(testUser)) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.contactLink").value(expectedResult.getContactLink())); + then(userCommandService).should().updateContactLink(userId, newContactLink); + verifyEveryMocksShouldHaveNoMoreInteractions(); + } + private void verifyEveryMocksShouldHaveNoMoreInteractions() { then(userCommandService).shouldHaveNoMoreInteractions(); then(userQueryService).shouldHaveNoMoreInteractions(); @@ -246,7 +269,7 @@ private UserDto createUserDto(long id) throws Exception { LocalDate.of(2024, 1, 1), Gender.ETC, "01012345678", - "https://contack-link", + "https://contact-link", LocalDateTime.of(2024, 1, 1, 0, 0) ); } @@ -258,4 +281,5 @@ private UserDto createUserDto() throws Exception { private UserDetails createTestUser(Long userId) throws Exception { return new UserPrincipal(createUserDto(userId)); } + } \ No newline at end of file diff --git a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java index 1e1dedd..148f3b3 100644 --- a/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java +++ b/src/test/java/com/ajou/hertz/unit/domain/user/service/UserCommandServiceTest.java @@ -29,6 +29,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; @@ -171,6 +172,39 @@ static Stream testDataForCreateNewUserWithKakao() throws Exception { assertThat(t).isInstanceOf(UserKakaoUidDuplicationException.class); } + @Test + void 주어진_유저_ID와_연락_수단으로_연락_수단을_변경한다() throws Exception { + // given + Long userId = 1L; + String contactLink = "https://new-contactLink"; + User user = createUser(userId, "$2a$abc123", "12345"); + given(userQueryService.getById(userId)).willReturn(user); + + // when + UserDto updatedUserDto = sut.updateContactLink(userId, contactLink); + + // then + then(userQueryService).should().getById(userId); + verifyEveryMocksShouldHaveNoMoreInteractions(); + assertThat(updatedUserDto.getContactLink()).isEqualTo(contactLink); + } + + @Test + void 주어진_유저_ID와_연락_수단으로_연락_수단을_변경한다_존재하지_않는_유저라면_예외가_발생한다() throws Exception { + // given + Long userId = 1L; + String contactLink = "https://new-contactLink"; + given(userQueryService.getById(userId)).willThrow(UserNotFoundByIdException.class); + + // when + Throwable t = catchThrowable(() -> sut.updateContactLink(userId, contactLink)); + + // then + then(userQueryService).should().getById(userId); + verifyEveryMocksShouldHaveNoMoreInteractions(); + assertThat(t).isInstanceOf(UserNotFoundByIdException.class); + } + private void verifyEveryMocksShouldHaveNoMoreInteractions() { then(userQueryService).shouldHaveNoMoreInteractions(); then(userRepository).shouldHaveNoMoreInteractions(); @@ -188,7 +222,7 @@ private static User createUser(Long id, String password, String kakaoUid, Gender LocalDate.of(2024, 1, 1), gender, "010-1234-5678", - null + "https://contactLink" ); }