Skip to content

Commit

Permalink
refactor: userId로 사용자 정보 조회 #101
Browse files Browse the repository at this point in the history
  • Loading branch information
chaewss committed Sep 9, 2023
1 parent 6431656 commit 776c43e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/docs/asciidoc/api-doc.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ include::{snippets}/user-controller-test/respond_201_when_sign_up_succeed/respon
include::{snippets}/user-controller-test/respond_201_when_sign_up_succeed/http-response.adoc[]

=== 1-5. 회원 정보 조회
==== Path Parameters
include::{snippets}/user-controller-test/respond_200_when_read_user_successfully/path-parameters.adoc[]
==== Sample Request
include::{snippets}/user-controller-test/respond_200_when_read_user_successfully/http-request.adoc[]
==== Response Fields
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/cvsgo/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public SuccessResponse<Boolean> checkNicknameExists(@PathVariable String nicknam
return SuccessResponse.from(userService.isDuplicatedNickname(nickname));
}

@GetMapping("/user")
public SuccessResponse<UserResponseDto> readUser(@LoginUser User user) {
return SuccessResponse.from(userService.readUser(user));
@GetMapping("/users/{userId}")
public SuccessResponse<UserResponseDto> readUser(@PathVariable Long userId) {
return SuccessResponse.from(userService.readUser(userId));
}

@PutMapping("/user")
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/cvsgo/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ public boolean isDuplicatedNickname(String nickname) {
/**
* 사용자 정보를 조회한다.
*
* @param user 로그인한 사용자
* @return 로그인 한 사용자 상세 정보
* @param userId 조회할 사용자 ID
* @return 사용자 상세 정보
*/
@Transactional(readOnly = true)
public UserResponseDto readUser(User user) {
public UserResponseDto readUser(Long userId) {
User user = userRepository.findById(userId).orElseThrow(() -> NOT_FOUND_USER);

long reviewLikeCount = reviewRepository.findAllByUser(user)
.stream().mapToLong(Review::getLikeCount).sum();

Expand Down
7 changes: 5 additions & 2 deletions src/test/java/com/cvsgo/controller/UserControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,20 @@ void respond_false_when_email_exists() throws Exception {
}

@Test
@DisplayName("로그인한 사용자 정보를 정상적으로 조회하면 HTTP 200을 응답한다")
@DisplayName("사용자 정보를 정상적으로 조회하면 HTTP 200을 응답한다")
void respond_200_when_read_user_successfully() throws Exception {
given(userService.readUser(any())).willReturn(getUser());

mockMvc.perform(get("/api/user")
mockMvc.perform(get("/api/users/{userId}", 1L)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print())
.andDo(document(documentIdentifier,
getDocumentRequest(),
getDocumentResponse(),
pathParameters(
parameterWithName("userId").description("조회할 회원 ID")
),
relaxedResponseFields(
fieldWithPath("data.id").type(JsonFieldType.NUMBER).description("회원 ID").optional(),
fieldWithPath("data.email").type(JsonFieldType.STRING).description("이메일"),
Expand Down
15 changes: 14 additions & 1 deletion src/test/java/com/cvsgo/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,26 @@ void should_return_false_when_nickname_does_not_exist() {
@Test
@DisplayName("사용자 정보를 조회한다")
void succeed_to_read_user() {
given(userRepository.findById(anyLong())).willReturn(Optional.of(user));
given(reviewRepository.findAllByUser(any())).willReturn(List.of(review1));

userService.readUser(user);
userService.readUser(user.getId());

then(userRepository).should(times(1)).findById(user.getId());
then(reviewRepository).should(times(1)).findAllByUser(any());
}

@Test
@DisplayName("사용자 정보 조회 시 해당하는 아이디를 가진 사용자가 없으면 NotFoundException이 발생한다")
void should_throw_NotFoundException_when_read_user_but_user_does_not_exist() {
final Long userId = 10000L;

given(userRepository.findById(anyLong())).willReturn(Optional.empty());

assertThrows(NotFoundException.class, () -> userService.readUser(userId));
then(userRepository).should(times(1)).findById(anyLong());
}

@Test
@DisplayName("회원을 정상적으로 수정한다")
void succeed_to_update_user() {
Expand Down

0 comments on commit 776c43e

Please sign in to comment.