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,20 @@ 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());
UserDto userDto = userQueryService.getDtoById(userPrincipal.getUserId());
Wo-ogie marked this conversation as resolved.
Show resolved Hide resolved
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
Wo-ogie marked this conversation as resolved.
Show resolved Hide resolved
@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 @@ -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,34 @@ 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);
UserDto updatedUserDto = createUserDto(userId);
given(userCommandService.updateContactLink(userId, newContactLink)).willReturn(updatedUserDto);
given(userQueryService.getDtoById(userId)).willReturn(updatedUserDto);

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(updateContactLinkRequest))
.with(user(userDetails))
)
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.contactLink").value(updatedUserDto.getContactLink()));

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

private void verifyEveryMocksShouldHaveNoMoreInteractions() {
then(userCommandService).shouldHaveNoMoreInteractions();
then(userQueryService).shouldHaveNoMoreInteractions();
Expand Down Expand Up @@ -246,7 +275,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 +287,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
sut.updateContactLink(userId, contactLink);

// then
then(userQueryService).should().getById(userId);
verifyEveryMocksShouldHaveNoMoreInteractions();
assertThat(user.getContactLink()).isEqualTo(contactLink);
}
Wo-ogie marked this conversation as resolved.
Show resolved Hide resolved

@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