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 All @@ -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;
Expand Down Expand Up @@ -114,4 +116,19 @@ public ResponseEntity<UserResponse> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;

}
5 changes: 5 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,9 @@ public static User create(
profileImageUrl, birth, gender, phone, null
);
}

public void changeContactLink(String contactLink) {
this.contactLink = contactLink;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Wo-ogie marked this conversation as resolved.
Show resolved Hide resolved

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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)
.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();
Expand Down Expand Up @@ -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)
);
}
Expand All @@ -258,4 +281,5 @@ private UserDto createUserDto() throws Exception {
private UserDetails createTestUser(Long userId) throws Exception {
return new UserPrincipal(createUserDto(userId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -171,6 +172,39 @@ static Stream<Arguments> 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();
Expand All @@ -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"
);
}

Expand Down
Loading