Skip to content

Commit

Permalink
feat: 기존 회원 리뷰 조회 API 로직 변경 (#218)
Browse files Browse the repository at this point in the history
* feat: userId 에 해당하는 유저 리뷰 조회 API 로직 변경 (#217)

* test: 통합테스트 수정 (#217)

* test: 단위테스트 수정 (#217)
  • Loading branch information
choidongkuen authored Feb 13, 2024
1 parent 9eac8c5 commit 1a7052c
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ public void registerReview(
userService.registerReview(meetingId, getCurrentUserId(), request);
}

@GetMapping("/reviews")
@GetMapping("/{userId}/reviews")
@ResponseStatus(HttpStatus.OK)
public List<UserReviewsResponse> getUserReviews() {
return userService.getUserReviews(getCurrentUserId());
public UserReviewsResponse getUserReviews(@PathVariable("userId") Long userId) {
return userService.getUserReviews(userId);
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/net/teumteum/user/domain/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.List;
import java.util.Optional;
import net.teumteum.core.security.Authenticated;
import net.teumteum.user.domain.response.UserReviewsResponse;
import net.teumteum.user.domain.response.UserReviewResponse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -15,7 +15,8 @@ public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByAuthenticatedAndOAuthId(@Param("authenticated") Authenticated authenticated,
@Param("oAuthId") String oAuthId);

@Query("select new net.teumteum.user.domain.response.UserReviewsResponse(r,count(r)) "
+ "from users u join u.reviews r where u.id = :userId group by r")
List<UserReviewsResponse> countUserReviewsByUserId(@Param("userId") Long userId);
@Query("select new net.teumteum.user.domain.response.UserReviewResponse(r, count(r)) "
+ "from users u join u.reviews r where u = :user group by r")
List<UserReviewResponse> countUserReviewsByUser(@Param("user") User user);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.teumteum.user.domain.response;

import net.teumteum.user.domain.Review;

public record UserReviewResponse(
Review review,
long count
) {

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.teumteum.user.domain.response;

import net.teumteum.user.domain.Review;
import java.util.List;

public record UserReviewsResponse(
Review review,
long count
List<UserReviewResponse> reviews
) {

public static UserReviewsResponse of(List<UserReviewResponse> reviews) {
return new UserReviewsResponse(reviews);
}
}
6 changes: 4 additions & 2 deletions src/main/java/net/teumteum/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ public void registerReview(Long meetingId, Long currentUserId, ReviewRegisterReq
});
}

public List<UserReviewsResponse> getUserReviews(Long userId) {
return userRepository.countUserReviewsByUserId(userId);
public UserReviewsResponse getUserReviews(Long userId) {
var user = getUser(userId);

return UserReviewsResponse.of(userRepository.countUserReviewsByUser(user));
}

public FriendsResponse findFriendsByUserId(Long userId) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/net/teumteum/integration/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ ResponseSpec deleteMeeting(String accessToken, Long meetingId) {
.exchange();
}

ResponseSpec getUserReviews(String accessToken) {
ResponseSpec getUserReviews(Long userId, String accessToken) {
return webTestClient
.get()
.uri("/users/reviews")
.uri("/users/" + userId + "/reviews")
.header(HttpHeaders.AUTHORIZATION, accessToken)
.exchange();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,12 @@ void Get_user_reviews() {
// given
var existUser = repository.saveAndGetUser();

var userId = existUser.getId();

securityContextSetting.set(existUser.getId());

// when
var expected = api.getUserReviews(VALID_TOKEN);
var expected = api.getUserReviews(userId, VALID_TOKEN);

// then
Assertions.assertThat(expected.expectStatus().isOk()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.teumteum.user.domain.request.UserRegisterRequest;
import net.teumteum.user.domain.request.UserWithdrawRequest;
import net.teumteum.user.domain.response.UserRegisterResponse;
import net.teumteum.user.domain.response.UserReviewResponse;
import net.teumteum.user.domain.response.UserReviewsResponse;
import net.teumteum.user.service.UserService;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -238,27 +239,25 @@ void Register_reviews_with_400_bad_request() throws Exception {
class Get_user_reviews_api_unit {

@Test
@DisplayName("로그인한 회원 id 에 해당하는 회원 리뷰와 200 OK을 반환한다.")
@DisplayName("user id 에 해당하는 회원 리뷰와 200 OK을 반환한다.")
void Get_user_reviews_with_200_ok() throws Exception {
// given
var userId = 1L;

given(securityService.getCurrentUserId()).willReturn(userId);

given(userService.getUserReviews(anyLong()))
.willReturn(List.of(new UserReviewsResponse(별로에요, 2L),
new UserReviewsResponse(최고에요, 3L)));
.willReturn(UserReviewsResponse.of(List.of(new UserReviewResponse(별로에요, 2L),
new UserReviewResponse(최고에요, 3L))));

// when & then
mockMvc.perform(get("/users/reviews")
mockMvc.perform(get("/users/{userId}/reviews", 1)
.with(csrf())
.header(AUTHORIZATION, VALID_ACCESS_TOKEN))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$", notNullValue()))
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].count", is(2)))
.andExpect(jsonPath("$[0].review").value("별로에요"));
.andExpect(jsonPath("$.reviews", notNullValue()))
.andExpect(jsonPath("$.reviews", hasSize(2)))
.andExpect(jsonPath("$.reviews[0].count", is(2)))
.andExpect(jsonPath("$.reviews[0].review").value("별로에요"));
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/test/java/net/teumteum/unit/user/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import net.teumteum.user.domain.request.UserRegisterRequest;
import net.teumteum.user.domain.request.UserWithdrawRequest;
import net.teumteum.user.domain.response.UserRegisterResponse;
import net.teumteum.user.domain.response.UserReviewsResponse;
import net.teumteum.user.domain.response.UserReviewResponse;
import net.teumteum.user.service.UserService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -251,23 +251,24 @@ class Get_user_reviews_api_unit {
void Return_user_reviews_with_200_ok() {
// given
var userId = 1L;
var existUser = UserFixture.getIdUser();

var response = List.of(new UserReviewsResponse(최고에요, 2L)
, new UserReviewsResponse(별로에요, 3L));
var response = List.of(new UserReviewResponse(최고에요, 2L)
, new UserReviewResponse(별로에요, 3L));

given(userRepository.countUserReviewsByUserId(anyLong())).willReturn(response);
given(userRepository.findById(anyLong())).willReturn(Optional.of(existUser));
given(userRepository.countUserReviewsByUser(any(User.class))).willReturn(response);

// when
var result = userService.getUserReviews(userId);

// then
assertThat(result).hasSize(2);
assertThat(result.get(0).review()).isEqualTo(최고에요);
assertThat(result.get(0).count()).isEqualTo(2L);
assertThat(result.get(1).review()).isEqualTo(별로에요);
assertThat(result.get(1).count()).isEqualTo(3L);
assertThat(result.reviews()).hasSize(2);
assertThat(result.reviews().get(0).review()).isEqualTo(최고에요);
assertThat(result.reviews().get(0).count()).isEqualTo(2L);
assertThat(result.reviews().get(1).review()).isEqualTo(별로에요);

verify(userRepository, times(1)).countUserReviewsByUserId(anyLong());
verify(userRepository, times(1)).countUserReviewsByUser(any(User.class));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import jakarta.persistence.EntityManager;
import java.util.Optional;
import net.teumteum.core.config.AppConfig;
import net.teumteum.user.domain.response.UserReviewsResponse;
import net.teumteum.user.domain.response.UserReviewResponse;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -85,13 +85,14 @@ void Count_user_reviews_by_user_id() {
entityManager.clear();

// when
var result = userRepository.countUserReviewsByUserId(id);
var result = userRepository.countUserReviewsByUser(existUser);

// then
Assertions.assertThat(result)
.isNotEmpty()
.hasSize(3)
.extracting(UserReviewsResponse::review, UserReviewsResponse::count)
.extracting(UserReviewResponse::review,
UserReviewResponse::count)
.contains(
Assertions.tuple(최고에요, 3L),
Assertions.tuple(별로에요, 1L),
Expand Down

0 comments on commit 1a7052c

Please sign in to comment.